我正在用 Java 开发一个 RSA 项目。我相信我唯一的问题是我的私钥(d)返回 0。计算它的代码:
privateKey = (one.mod(phi)).divide(publicKey); //phi 是 (p-1)*(q-1), publicKey = 65537
这返回 0,这实际上没有意义,因为虽然 1/publicKey 很小,但不是那么小以至于不能用 BigInteger 表示,对吗?还是我应该为此使用 bigDecimal 而不是 bigInteger?
无论如何,这是我的其余代码,也许我的问题在其他地方,有人可以发现它:
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
* http://stackoverflow.com/questions/5818842/problems-encrypting-a-string-using-rsa-algorithm-in-java
*/
public class EncryptionAlgorithms {
private static BigInteger p, q, product, phi, publicKey, r, a, b, privateKey, 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) {
System.out.println(1%5);
System.out.println(1%7);
System.out.println(1%15);
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);
product = p.multiply(q); // n = p * q
phi = (p.subtract(one)).multiply(q.subtract(one)); // m = (p-1) * (q-1)
publicKey = new BigInteger("65537"); //must be a prime. GCD(e,m)=1 //65537 = 2^16 + 1 // will have to make an algorith for this later
privateKey = (one.mod(phi)).divide(publicKey); //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("p = " + p);
System.out.println("q = " + q);
System.out.println("product = " + product);
System.out.println("phi = " + phi);
System.out.println("public key = " + publicKey);
System.out.println("private key = " + privateKey);
System.out.println("");
System.out.println("please enther the message to be encrypted");
BigInteger mes = new BigInteger(input.next());
BigInteger ans = encrypt(mes, product, publicKey);
decrypt(ans, product, privateKey);
}
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;
}
}
编辑:
我的 println 显示:
1 1 1
p = 116836516005620566340054773857764031797944186559716240327950855431635039403009755486393306437986151660938961585855042767699921699290213154508261609587992784967007973771920952681452331230679204007231749877985338995375377055530874576754671566809579320008488587254143483816119529551389602238180023655212196118671
q = 118445841243660959783655439569263260088875780880318340730749393826729462878787052603343185844381113282914313404010265021460834832031349936675446265322019791433625601618768573332861024101362551263559309901437229668893444819854637447915924234885201092336165962514242782328851534270944932858310655258614396326481
产品 =
φ =
公钥 = 65537
私钥 = 0