Java-公钥-私钥加密-如何在RSA中计算私钥

Java-公钥-私钥加密-如何在RSA中计算私钥,java,rsa,biginteger,public-key-encryption,Java,Rsa,Biginteger,Public Key Encryption,我为一个RSA算法编写了一个代码,它返回了错误的数字,这恰好是一个巨大的数字。我确信我把所有的东西都编对了,除了一行我不确定的东西。我不知道如何解决RSA中的私钥问题,只是对它进行了修改(我看到有人在编写代码) d=e.modInverse(m) 其中d是私钥,e是公钥,m是(p-1)*(q-1)。我不明白modInverse方法是如何工作的。长话短说,在同一个等式中没有两个未知数的情况下,如何实际求解“d” d=1/(e%m) 我没有发布结果,只是因为返回的数字和加密邮件一样大 package

我为一个RSA算法编写了一个代码,它返回了错误的数字,这恰好是一个巨大的数字。我确信我把所有的东西都编对了,除了一行我不确定的东西。我不知道如何解决RSA中的私钥问题,只是对它进行了修改(我看到有人在编写代码)

d=e.modInverse(m)

其中d是私钥,e是公钥,m是(p-1)*(q-1)。我不明白modInverse方法是如何工作的。长话短说,在同一个等式中没有两个未知数的情况下,如何实际求解“d”

d=1/(e%m)

我没有发布结果,只是因为返回的数字和加密邮件一样大

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;
    }
}
包加密算法;
导入java.math.biginger;
导入java.util.*;
/**
*
*@作者亚赞来源:
* 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
*/
公共类加密算法{
私有静态大整数p,q,n,m,e,r,a,b,d,加密,解密,消息,userN,userE,userD;
私有静态BigInteger one=新的BigInteger(“1”);
私有静态BigInteger badData=新的BigInteger(“-1”);
私有静态BigInteger零=新的BigInteger(“0”);
公共静态void main(字符串[]args){
PKE();
}
public static void PKE(){//私钥加密
扫描仪输入=新扫描仪(System.in);
Random rand1=新的Random(System.nanoTime());
Random rand2=新随机数(System.nanoTime()*16);//创建第二个模糊随机数
p=BigInteger.probablePrime(1024,rand1);
q=BigInteger.probablePrime(1024,rand2);
n=p.multiply(q);//n=p*q
m=(p.减法(一))。乘(q.减法(一));//m=(p-1)*(q-1)
e=new BigInteger(“65537”);//必须是素数。GCD(e,m)=1//65537=2^16+1//以后必须对此进行算法计算

d=e.modInverse(m);//最薄弱的环节哈哈,你要踢自己了。你做的一切都是对的,除了这个小虫子:

    decrypt(ans, n, e);
应该是

    decrypt(ans, n, d);

一般来说,您可能会在变量名和类概念(如实例变量)方面做得更好。非常感谢您发布了一个完整的工作示例。

lol谢谢。但现在,无论您输入的原始消息是什么,它都将返回1作为解密消息