Java RSA私钥生成问题

Java RSA私钥生成问题,java,rsa,Java,Rsa,我正在用Java做一个RSA项目。我相信我唯一的问题是我的私钥(d)返回0。计算它的代码是: privateKey=(1.mod(phi)).divide(publicKey)//φ为(p-1)*(q-1),公钥=65537 这将返回0,这并没有真正意义,因为虽然1/publicKey很小,但并不是太小以至于不能用BigInteger表示,对吗?或者我应该用bigDecimal来代替bigInteger 无论如何,这是我的代码的其余部分,也许我的问题在别处,有人可以发现它: package en

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

privateKey=(1.mod(phi)).divide(publicKey)//φ为(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;
    }
}
包加密算法;
导入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
* http://stackoverflow.com/questions/5818842/problems-encrypting-a-string-using-rsa-algorithm-in-java
*/
公共类加密算法{
私有静态大整数p,q,乘积,phi,公钥,r,a,b,私钥,加密,解密,消息,userN,userE,userD;
私有静态BigInteger one=新的BigInteger(“1”);
私有静态BigInteger badData=新的BigInteger(“-1”);
私有静态BigInteger零=新的BigInteger(“0”);
公共静态void main(字符串[]args){
系统输出打印项次(1%5);
系统输出打印项次(1%7);
系统输出打印项次(1%15);
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);
积=p.乘(q);//n=p*q
φ=(p.减法(一))。乘(q.减法(一));//m=(p-1)*(q-1)
publicKey=new BigInteger(“65537”);//必须是素数。GCD(e,m)=1//65537=2^16+1//以后必须对此进行算法计算

privateKey=(one.mod(phi)).divide(publicKey);//您不想分割的最薄弱环节

privateKey = (one.mod(phi)).divide(publicKey); //phi is (p-1)*(q-1), publicKey = 65537
您需要模逆,因此请使用:

得到这样一个值

privateKey * publicKey ≡ 1 (mod phi)

你不想分裂

privateKey = (one.mod(phi)).divide(publicKey); //phi is (p-1)*(q-1), publicKey = 65537
您需要模逆,因此请使用:

得到这样一个值

privateKey * publicKey ≡ 1 (mod phi)

BigInteger
所能容纳的最小正数是1。如果期望值在0和1之间,那么您需要使用
BigDecimal
。我只是在使用我在网上找到的一个公式。我以为私钥应该是一个整数,但我不知道如何操作它。您可以显示
系统的输出吗.println
s正在显示,可能只需编辑问题并将其作为注释放在每行后面?添加,1代表我在开始时所做的模。你可以忽略它们。
biginger
所能容纳的最小正数是1。如果你期望介于0和1之间,那么你需要使用
BigDecimal
。i我只是在使用我在网上找到的一个公式。我以为私钥应该是一个整数,但我不知道如何操作它。你可以显示你的
系统。out.println
s显示的内容,也许只需编辑问题并将其作为注释放在每行后面?添加,1代表我在be中所做的模gining.u可以忽略它们,但是1%phi=1,因为1%x总是=1。所以publicKey*privateKey必须等于1?它是有效的:)。但我不明白它为什么有效though@yazan您不需要
publicKey*privateKey=1
[只有当两者都为±1时才可能],您需要
publicKey*privateKey=1+someFactor*phi
。然后
c^(publicKey*privateKey)=c^1*(c^phi)^(someFactor)
,并且
c^phi
≡ 1(mod n)
(除非
p
q
中的一个除法
c
,但在任何情况下都有
c^(公钥*私钥)≡ c(mod n)
)。好吧。那么也许我误解了同余符号相对于等号的含义。你能解释一下吗?@yazan
a≡ b(mod c)
表示
c
划分
a
b
之间的差异,即存在
k
,使得
(a-b)=k*c
。但是1%phi=1,因为1%x总是=1。所以publicKey*privateKey必须等于1?它有效:)。但我不明白为什么它有效though@yazan您不需要
publicKey*privateKey=1
[只有当两者都为±1时才可能],您需要
publicKey*privateKey=1+someFactor*phi
。然后
c^(publicKey*privateKey)=c^1*(c^phi)^(someFactor)
,并且
c^phi
≡ 1(mod n)
(除非
p
q
中的一个除法
c
,但在任何情况下都有
c^(公钥*私钥)≡ c(mod n)
)。好吧。那么也许我误解了同余符号相对于等号的含义。你能解释一下吗?@yazan
a≡ b(mod c)
表示
c
a
b
之间的差异分开,即存在
k
,使得
(a-b)=k*c