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:它適用於我。 –