I found this rsa code on net. I have problem with code, it is not working as expected for the larger text. However it decrypts fine for a few lines text. Can anyone point out the mistake it the code or logic please. Thanks
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
/**
*
* @author Mohtashim
*/
public class RSA {
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger phi;
private BigInteger e;
private BigInteger d;
private int bitlength = 1024;
private int blocksize = 128; //blocksize in byte
private Random r;
public RSA() {
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
long startTime= System.nanoTime();
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
long endTime= System.nanoTime();
System.out.println((endTime-startTime)/1000);
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);
System.out.println("p : " + p);
System.out.println("q : " + q);
System.out.println("phiN : " + N);
//System.out.println("gcd : " + gcd);
System.out.println("e : " + e);
System.out.println("d : " + d);
}
private static String bytesToString(byte[] encrypted) {
String test = "";
for (byte b : encrypted) {
test += Byte.toString(b);
}
return test;
}
public RSA(BigInteger e, BigInteger d, BigInteger N) {
this.e = e;
this.d = d;
this.N = N;
}
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();
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
RSA objRSA;
objRSA = new RSA();
int intC=0;
byte[] encrypted = objRSA.encrypt("hello bhai jan kia haal hein aaq k hyl".getBytes());
String decrypted = new String (objRSA.decrypt(encrypted));
System.out.println("encrypted: "+ encrypted);
System.out.println("decrypted: "+ decrypted);
}
}