java - 检查我的 Java RSA 算法

标签 java rsa

我必须实现 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/

相关文章:

java - Jung2图库可以遍历有向图吗

java - Spring 交易 : What will happen if I don't give @Transaction annotation on method

java - 如何在 OSGi 中通过 HttpService 发布 JAX-WS

python - pysftp Paramiko PasswordRequiredException:私钥文件已加密

algorithm - 算法中的 RSA-2048 位

ios - 从模数/指数获取 SecKeyRef

java - 仅使用 String 方法拆分具有多个定界符的字符串

java - 将 DATETIME 插入数据库时​​如何修复错误

java - 使用 Ruby on Rails 比较 Android Market 响应中的签名数据和签名

encryption - 用C进行简单的加密实现