gpt4 book ai didi

java - RSA私钥生成问题

转载 作者:行者123 更新时间:2023-11-30 07:16:48 26 4
gpt4 key购买 nike

我正在用 Java 开发一个 RSA 项目。我相信我遇到的唯一问题是我的私钥 (d) 返回 0。计算它的代码:

privateKey = (one.mod(phi)).divide(publicKey); //phi is (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 正在显示:

11个1

p = 116836516005620566340054773857764031797944186559716240327950855431635039403009755486393306437986151660938961585855042767699921699290213154508261609587992784967007973771920952681452331230679204007231749877985338995375377055530874576754671566809579320008488587254143483816119529551389602238180023655212196118671

q = 118445841243660959783655439569263260088875780880318340730749393826729462878787052603343185844381113282914313404010265021460834832031349936675446265322019791433625601618768573332861024101362551263559309901437229668893444819854637447915924234885201092336165962514242782328851534270944932858310655258614396326481

product = 13838799426264186334752007568314388817999080436393046698822718780049594131958339754285122812528703328831770507600346387196178828551045467705258918741851385458709465331658531514221228970233180703647270287552837753141448222019771566054259160450349111420609315305712076175614925637533331760969764529465148575554436909984186174819968350087737759092473439388442979008215287412729873605743307221889589355321080925515130587917789192951804915620770181045927041436295136776421205411332865156021777271758200515616012524267222882737764344732490495401547779959227857738842827307140880624719496653055925158630393545977988735826751

phi = 13838799426264186334752007568314388817999080436393046698822718780049594131958339754285122812528703328831770507600346387196178828551045467705258918741851385458709465331658531514221228970233180703647270287552837753141448222019771566054259160450349111420609315305712076175614925637533331760969764529465148575554201627626936893293844639874310731800586619421002944427156587163471509103461510413799852863038713660571277312927923885162644159089448617954743333561385124200020571835942175630007463916426158760345221464487800314073495522857104983376877184157533077326498172757372494358574525589233590623533902867064162143381600

公钥 = 65537

私钥=0

最佳答案

你不想分开,

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)

关于java - RSA私钥生成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16652439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com