[英]How to implement RSA encrypt and decrypt in Java
我有公钥和私钥生成工作。 我的下一步是创建另外2种方法-加密和解密。 我只是不确定如何实现加密和解密。 我有一些想法,但是似乎没有什么好的解决方案。 有什么见解吗?
public class RSA
{
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();
// prime numbers
private BigInteger p;
private BigInteger q;
// modulus
private BigInteger n;
// totient
private BigInteger t;
// public key
private BigInteger e;
// private key
private BigInteger d;
/**
* Constructor for objects of class RSA
*/
public RSA(int N)
{
p = BigInteger.probablePrime(N/2, random);
q = BigInteger.probablePrime(N/2, random);
// initialising modulus
n = p.multiply(q);
// initialising t by euclid's totient function (p-1)(q-1)
t = (p.subtract(one)).multiply(q.subtract(one));
// initialising public key ~ 65537 is common public key
e = new BigInteger("65537");
}
public int generatePrivateKey()
{
d = e.modInverse(t);
return d.intValue();
}
}
不确定您要问什么,但是对于RSA加密,如果您的模数为n位宽,那么您的公钥也将为n位宽。 一个简单的int
无效。
也可以看看
我对Java RSA加密的谦卑尝试:
http://david.tribble.com/src/java/tribble/crypto/RSACipher.java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.