I worked on a code for an RSA algorithm and it returns the incorrect number, which happens to be huge. I am sure I coded everything right except for one line I was not sure about. I did not know how to solve for the private key in the RSA, and just winged it (I saw someone code
d = e.modInverse(m);
where d is the private key, e is the public key, and m is (p-1)*(q-1). I dont understand how the modInverse method works though. long story short, how do you actually solve for the 'd' without having 2 unknowns in the same equation (I saw some equations given that said:
d = 1/(e % m);
I refrained from posting results just because the number returned is about as big as the encrypted message.
package encryptionalgorithms;
import java.math.BigInteger;
import java.util.*;
/**
*
* @author YAZAN Sources:
* http://introcs.cs.princeton.edu/java/78crypto/RSA.java.html
* http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html
* http://www.youtube.com/watch?v=ejppVhOSUmA
*/
public class EncryptionAlgorithms {
private static BigInteger p, q, n, m, e, r, a, b, d, encrypt, decrypt, message, userN, userE, userD;
private static BigInteger one = new BigInteger("1");
private static BigInteger badData = new BigInteger("-1");
private static BigInteger zero = new BigInteger("0");
public static void main(String[] args) {
PKE();
}
public static void PKE() { //Private Key Encryption
Scanner input = new Scanner(System.in);
Random rand1 = new Random(System.nanoTime());
Random rand2 = new Random(System.nanoTime() * 16); //to create a second obscure random number
p = BigInteger.probablePrime(1024, rand1);
q = BigInteger.probablePrime(1024, rand2);
n = p.multiply(q); // n = p * q
m = (p.subtract(one)).multiply(q.subtract(one)); // m = (p-1) * (q-1)
e = new BigInteger("65537"); //must be a prime. GCD(e,m)=1 //65537 = 2^16 + 1 // will have to make an algorith for this later
d = e.modInverse(m); //weakest link <============
// System.out.println("Public Keys:");
// System.out.println("e = " + e + " and n = " + n);
// System.out.println("Private Keys:");
// System.out.println("d = " + d + " and n = " + n);
System.out.println("please enther the message to be encrypted");
BigInteger mes = new BigInteger(input.next());
BigInteger ans = encrypt(mes, n, e);
decrypt(ans, n, d);
}
public static BigInteger encrypt(BigInteger num, BigInteger n, BigInteger e) {
encrypt = num.modPow(e, n);
System.out.println("encrypted: " + encrypt);
return encrypt;
}
public static BigInteger decrypt(BigInteger enc, BigInteger n, BigInteger d) {
decrypt = enc.modPow(d, n);
System.out.println("decrypted: " + decrypt);
return decrypt;
}
}
and as a variant to the line in question, I tried:
d = one.divide(e.mod(m));
and I still got incorrect results.
解决方案
haha, you are going to kick yourself. You did everything correct, except for this teeny-weeny bug:
decrypt(ans, n, e);
should be
decrypt(ans, n, d);
In general, you could probably do a better job with variable names and class concepts such as instance variables. Kudos to you for posting a complete working example.