import java
import java
DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
private Random r;
public RSA()
r=new Random();
p=BigInteger.probablePrime(bitlength,r);
q=BigInteger.probablePrime(bitlength,r);
N=p.multiply(q);
phi=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e=BigInteger.probablePrime(bitlength/2,r);
while(phi.gcd(e).compareTo(BigInteger.ONE)>0&&e.compareTo(phi)<0)
e.add(BigInteger.ONE);
d=e.modInverse(phi);
this.e=e;
this.d=d;
this.N=N;
String testString;
testString=in.readLine();
System.out.println("Encrypting string:"+testString);
System.out.println("string in bytes:"+bytesToString(testString.getBytes()));
byte[] encrypted=rsa.encrypt(testString.getBytes());
byte[] decrypted=rsa.decrypt(encrypted);
System.out.println("Dcrypting Bytes:"+bytesToString(decrypted));
for(byte b:encrypted)
test+=Byte.toString(b);
return test;
public byte[]encrypt(byte[]message)
return(new BigInteger(message)).modPow(e,N).toByteArray();
public byte[]decrypt(byte[]message)
{
return(new BigInteger(message)).modPow(d,N).toByteArray();