import java.math.*; import java.util.*; /*This program implements the RSA public key cryptosystem. It was written for educational purposes. Nevertheless, it really works: No-one can break it. written by Sieuwert van Otterloo 2000 smotterl@cs.uu.nl go.to/sieuwert */ /*the public key is the public part of the key. Anyone can have it. It can only encrypt data, not decrypt.*/ class publickey{ BigInteger n,e; String owner; /*make a public key. Do not do it yourself, but make a public key from a private key.*/ publickey(String iowner,BigInteger in,BigInteger ie){ owner=iowner; n=in; e=ie; } /*read the key back from a string.*/ publickey(String from){ StringTokenizer st=new StringTokenizer(from," "); owner=st.nextToken(); n=readBI(st.nextToken()); e=readBI(st.nextToken()); } /*use the key to encrypt a 'message' m. m should be a number from 1 to n (n not included). use makemessage to convert your message to a BigInteger. */ BigInteger encrypt(BigInteger m){ return m.modPow(e,n); } /*make a string from this key.*/ public String toString(){ return owner+" "+printBI(n)+" "+printBI(e); } /*help methods for reading and writing:*/ final static int radix=36; static String printBI(BigInteger b){ return b.toString(radix); } static BigInteger readBI(String s){ return new BigInteger(s,radix); } /* these methods convert an arbitrary message, in the form of an array of bytes, to a message suitable for encryption. To do this random bits are added (this is needed to make cracking of the system harder), and it is converted to a BigInteger.*/ BigInteger makemessage(byte[] input){ /*to understand this part of the program, read the description of the BigInteger constructor (in the standard java help). */ if(input.length>128 || input.length*8+24>=n.bitLength()) return new BigInteger("0"); //error! message to long. byte[] paddedinput=new byte[n.bitLength()/8-1]; for(int i=0;i