我正在使用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
产物= 13838799426264186334752007568314388817999080436393046698822718780049594131958339754285122812528703328831770507600346387196178828551045467705258918741851385458709465331658531514221228970233180703647270287552837753141448222019771566054259160450349111420609315305712076175614925637533331760969764529465148575554436909984186174819968350087737759092473439388442979008215287412729873605743307221889589355321080925515130587917789192951804915620770181045927041436295136776421205411332865156021777271758200515616012524267222882737764344732490495401547779959227857738842827307140880624719496653055925158630393545977988735826751
披= 13838799426264186334752007568314388817999080436393046698822718780049594131958339754285122812528703328831770507600346387196178828551045467705258918741851385458709465331658531514221228970233180703647270287552837753141448222019771566054259160450349111420609315305712076175614925637533331760969764529465148575554201627626936893293844639874310731800586619421002944427156587163471509103461510413799852863038713660571277312927923885162644159089448617954743333561385124200020571835942175630007463916426158760345221464487800314073495522857104983376877184157533077326498172757372494358574525589233590623533902867064162143381600
公钥= 65537
私钥= 0
答案 0 :(得分:4)
你不想分开,
privateKey = (one.mod(phi)).divide(publicKey); //phi is (p-1)*(q-1), publicKey = 65537
您需要模块化逆,因此请使用BigInteger.modInverse
:
privateKey = publicKey.modInverse(phi);
获取一个值
privateKey * publicKey ≡ 1 (mod phi)