2
我知道这个主题有很多线索,但是我找不到一个实际上能够一致地回答我的问题的线程。Java - 公钥 - 私钥加密 - 如何计算RSA中的私钥 - UNSOLVED
我工作的RSA算法的代码,它返回不正确的数字,这恰好是巨大的。我确信我编码的一切正确,除了一条线我不确定。我不知道如何解决在RSA私钥,只是有翅(我看见有人代码
d = e.modInverse(M);
其中d是私人key,e是公钥,m是(p-1)*(q-1)。我不明白modInverse方法的工作原理。长话短说,你怎么实际解决'd'没有2在相同的方程未知数(我看到给出一些方程,所述:
d = 1 /(E%米);
我不会因为返回的数字大约与加密邮件一样大而发布结果。
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;
}
}
和作为变体到所讨论的线路,我试图:
d = one.divide(e.mod(M));
我仍然得到不正确的结果。
感谢您提供的任何帮助
哈哈谢谢。但现在无论您输入的是什么原始消息,它总是会返回1作为解密消息 – YazanLpizra
@ yazan:它适用于我。 –