RSA私钥生成问题

Yaz*_*zra 2 java rsa

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

privateKey = (one.mod(phi)).divide(publicKey); //phi 为 (p-1)*(q-1), 公钥 = 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我的 println 显示:

1 1 1

p = 1168365160056205663400547738577640317979441865597162403279508554316350394030097554863933064379861516609389615858550427676 99921699290213154508261609587992784967007973771920952681452331230679204007231749877985338995375377055530874576754671566809579 320008488587254143483816119529551389602238180023655212196118671

q = 1184458412436609597836554395692632600888757808803183407307493938267294628787870526033431858443811132829143134040102650214 60834832031349936675446265322019791433625601618768573332861024101362551263559309901437229668893444819854637447915924234885201 092336165962514242782328851534270944932858310655258614396326481

产品=

Φ=

公钥=65537

私钥 = 0

Dan*_*her 5

你不想分开,

\n\n
privateKey = (one.mod(phi)).divide(publicKey); //phi is (p-1)*(q-1), publicKey = 65537\n
Run Code Online (Sandbox Code Playgroud)\n\n

你需要模逆,所以使用BigInteger.modInverse

\n\n
privateKey = publicKey.modInverse(phi);\n
Run Code Online (Sandbox Code Playgroud)\n\n

得到一个值使得

\n\n
privateKey * publicKey \xe2\x89\xa1 1 (mod phi)\n
Run Code Online (Sandbox Code Playgroud)\n