java - RSA数字签名失败

标签 java cryptography rsa biginteger modular-arithmetic

我正在尝试实现 RSA 盲数字签名方案,使用 BigInteger 类生成大素数。 Samantha 生成公钥、私钥,选择一条消息,对其进行屏蔽,然后对其进行签名,然后 Victor 验证签名。

问题:只要我使用 BigInteger 类中的模幂方法 modPow,一切都会完美运行(验证算法返回 true每次)。然而,我构建了一个自定义类,在其中我自己实现了几种代数算法;当我使用 modExp 方法切换 modPow 调用时,我不断从验证算法获得错误返回(大约 50-60% 的时间),即使我不应该这样做。如果我不使用大的随机整数,而是设置小的硬编码数字用于测试目的,我会得到正确的结果。

问题:因此,我很确定我的 modExp 方法有问题,但是我似乎无法找出我是否做错了,即使多次更改算法后。有什么问题吗?

到目前为止我的代码:

RSA_test() -- 用于预计算步骤和测试的方法

public static void RSA_test(){

    // The Signer (Samantha) picks p and q, 1024 bit primes
    Random rng = new SecureRandom();
    BigInteger p = BigInteger.probablePrime(1024, rng);
    BigInteger q = BigInteger.probablePrime(1024, rng);
  /*BigInteger p = BigInteger.valueOf(7);
    BigInteger q = BigInteger.valueOf(13);*/

    // The RSA modulus is computed
    BigInteger n = p.multiply(q);

    // phi(n) is computed
    BigInteger phiN = (p.subtract(BigInteger.ONE)
                      .multiply(q.subtract(BigInteger.ONE)));

    // Samantha chooses her message, m
    BigInteger m = new BigInteger("22");

    // Samantha computes her public exponent
    BigInteger v;
    while(true){
        v = new BigInteger(phiN.bitLength(), rng);
        if(v.compareTo(BigInteger.ONE) > 0 &&
           v.compareTo(phiN) < 0 &&
           ModularArithmetic.gcd(v, phiN).equals(BigInteger.ONE))
            break;
    }
    // v = BigInteger.valueOf(5);

    // Samantha generates the blinding factor and masks her message
    BigInteger r;
    while(true){
        r = new BigInteger(512, rng);
        if(ModularArithmetic.gcd(r, n).equals(BigInteger.ONE))
            break;
    }
    // r = BigInteger.valueOf(10);

    BigInteger mBlinded = m.multiply(ModularArithmetic.modExp(r, v, n));

    // Samantha signs her message
    BigInteger SBlinded = Cryptography.RSASignature(mBlinded, n, phiN, v);

    // Samantha removes the blinding factor, obtaining S
    BigInteger S = SBlinded.multiply(ModularArithmetic.modInv(r, n));

    // Victor verifies the signature
    boolean result = Cryptography.RSAVerification(S, m, n, v);

    String s = (result == true) ? "The signature has been verified" : "The signature has not been verified";
    System.out.println(s);
}

由于签名和验证方式与问题无关,我确信它们是正确的,所以我将省略它们。另外,这是我的 modExp 方法:

public static BigInteger modExp(BigInteger base, BigInteger exponent, BigInteger modulus){

    if(exponent.equals(BigInteger.ZERO))
        return (modulus.equals(BigInteger.ONE)) ? BigInteger.ZERO : BigInteger.ONE;
    if(base.equals(BigInteger.ONE))
        return (modulus.equals(BigInteger.ONE)) ? BigInteger.ZERO : BigInteger.ONE;
    if(exponent.equals(BigInteger.ONE))
        return base.mod(modulus);
    if(modulus.equals(BigInteger.ONE))
        return BigInteger.ZERO;
    // The case when base does not have a multiplicative inverse
    if((modulus.compareTo(BigInteger.ZERO) <= 0) || 
       ((exponent.compareTo(BigInteger.ZERO) < 0 && !(gcd(base,modulus).compareTo(BigInteger.ONE) == 0))))
        throw new ArithmeticException("BigInteger: modulus not positive");

    BigInteger result = BigInteger.ONE;
    while(exponent.compareTo(BigInteger.ZERO) > 0){
        if(exponent.testBit(0))
            result = (result.multiply(base).mod(modulus));
        exponent = exponent.shiftRight(1);
        base = (base.multiply(base)).mod(modulus);
    }

    return result.mod(modulus);
}

最佳答案

除了检查 gcd(base, modulus) == 1 之外,您无法正确处理负指数。以下代码片段显示了一种正确的方法。

if (exponent.signum() < 0 && gcd(base,modulus).equals(BigInteger.ONE)) {
    return modExp(base.modInverse(modulus), exponent.negate(), modulus);
}

观察signum()方法对于将大整数与零进行比较可能更方便。

关于java - RSA数字签名失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47061259/

相关文章:

java - 与 FLAG_LAYOUT_NO_LIMITS 相反

javascript - 在 PHP 可读的 JavaScript 中创建 PEM key 对

java - 移动窗口时OpenGL停止绘制lwjgl

java - 如何识别来自PDA/桌面/服务器的Web请求?

java - 让 BufferedImage 使用更少的 RAM?

c - 有没有办法用只有 pem 公钥文件或 pem 私钥文件的 RSA 进行加密和解密?

javascript - forge.js 复制我用 rsa 所做的事情,但用 aes

c++ - 更改为 C++ 时卡在 while 循环中

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

java - 如何编写包含签名的 .RSA 文件(使用 Java)