RSAenryption java
我的任务是: 创建一个接受密钥对的构造函数,而不是随机生成一个新的集合。这些密钥对将包括公钥和私钥的模和指数 我做错了什么?我知道上面说不要使用“随机”,但我还能怎么做呢RSAenryption java,java,rsa,Java,Rsa,我的任务是: 创建一个接受密钥对的构造函数,而不是随机生成一个新的集合。这些密钥对将包括公钥和私钥的模和指数 我做错了什么?我知道上面说不要使用“随机”,但我还能怎么做呢 private BigInteger key; private BigInteger p; private BigInteger q; private BigInteger eulerTotient; private BigInteger e; private BigInteger n; private
private BigInteger key;
private BigInteger p;
private BigInteger q;
private BigInteger eulerTotient;
private BigInteger e;
private BigInteger n;
private BigInteger d;
public Ramdom rdm1;
public RSAEncryption()
{
p = BigInteger.probablePrime(256, rdm1);
q = BigInteger.probablePrime(255, rdm1);
//Calculating an n value by multiplying p and q
n = p.multiply(q);
//Turning the number one into a BigInteger for assistance
BigInteger one = BigInteger.valueOf(1);
//Euler totient is (p -1) * (q-1)
eulerTotient = p.subtract(one).multiply(q.subtract(one));
//e is some number between 1 and the totient
e = BigInteger.probablePrime(511, rdm1);
this.e = e;
//d is the private key and calculated as inverse of e modulous totient
d = (e.modInverse(eulerTotient));
this.d = d;
}
正如我在评论中所写的,问题在于BigInteger没有按照请求获取密钥对;相反,它生成值,例如p=biginger.probablePrime256,rdm1;。构造函数需要接受参数,而不是生成新值。因此,您需要编写rsa加密,以便它接受两个参数p和q,而不是使用biginger.probablePrime生成它们 以下是一些未经测试的示例代码,可以帮助您重写构造函数:
public RSAEncryption(BigInteger p, BigInteger q) // note the parameters
{
/**
* I deleted the assignments here. Because you get p and q as parameters,
* theres's no need for probablePrime()
*/
//Calculating an n value by multiplying p and q
n = p.multiply(q);
//Turning the number one into a BigInteger for assistance
BigInteger one = BigInteger.valueOf(1);
//Euler totient is (p -1) * (q-1)
eulerTotient = p.subtract(one).multiply(q.subtract(one));
//e is some number between 1 and the totient
e = BigInteger.probablePrime(511, rdm1);
this.e = e;
//d is the private key and calculated as inverse of e modulous totient
d = (e.modInverse(eulerTotient));
this.d = d;
}
您可以创建一个直接设置n、d和e的构造函数。其他值是中间值,因此您可能不应该将它们存储在字段中,除非以后必须实现中国剩余定理私钥计算,然后您应该存储p、q和一些其他值 这就是:
public RSAEncryption(BigInteger n, BigInteger d, BigInteger e) {
this.n = n;
this.d = d;
this.e = e;
}
如果调用此构造函数,任何其他字段都将设置为null
上述内容足以设置由私钥n、d和公钥n、e组成的密钥对。当然不需要通过n两次
这只有在您必须自己实现RSA计算时才有用。如果您想使用Java JCA/JCE,那么您可以简单地传递由RSAPublicKey和RSAPrivateKey组成的消息,或者只传递公钥和私钥对象本身。您的问题是什么?您的函数没有按照要求获取密钥对;相反,它生成值,例如p=biginger.probablePrime256,rdm1;。你的函数需要接受参数,而不是生成新的值。我基本上是在为它生成键。我需要使用模和指数作为参数。我有点像是在构造函数中生成的,没有参数。我只是对整件事感到困惑。你可能想看看。简而言之,您需要编写rsa加密,这样它就可以接受两个参数p和q,而不是使用biginger.probablePrime.ahh生成它们,我认为这是有意义的。我应该使用参数中给定的值进行数学运算。谢谢我发布了一个我认为会有帮助的答案。