我必须实现 RSA 算法来签署文档并检查是否有人更改了它。这是正确的吗?在不知道是否可以的情况下,我无法进入预制作阶段...
private BigInteger n;
private BigInteger e;
private BigInteger d;
private final static SecureRandom random = new SecureRandom();
private BigInteger getRandomInteger(int lengthInBits){
return BigInteger.probablePrime(lengthInBits/2, random);
}
public RSACypher(int lengthInBits) {
BigInteger phi;
do {
BigInteger p = getRandomInteger( lengthInBits );
BigInteger q = getRandomInteger( lengthInBits );
phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
n = p.multiply(q);
e = BigInteger.probablePrime(lengthInBits - 1, random);
d = e.modInverse(phi);
} while ( ( n.bitCount() > (lengthInBits/2) ) || ( e.gcd( phi ).intValue() != 1 ) );
}
提前致谢
最佳答案
根据 RSA 规范编写单元测试... 您可以从已经编写的测试中获得灵感,例如 this one ,但是编写自己的测试会让你对算法知识更有信心(这是我的观点;-))
关于java - 检查我的 Java RSA 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20043133/