java - 我怎样才能快速 ((A^z1 * y^z2) mod P) mod Q

标签 java cryptography bouncycastle gost3410

BigIntegerValue.pow(IntegerValue)

java上的指数是整数,但我有大整数值。

我尝试验证签名 GOST 3410,我得到了这个代码 pow,但它太长了..

有什么想法吗?为了获得 P 和 Q,我使用了 bouncy CaSTLe.. 但我不知道如何在 bouncy caSTLe 上进行验证,因为不知道如何查看该值.. 谢谢。

    public static BigInteger pow_manual(BigInteger x, BigInteger y) {
    if (y.compareTo(BigInteger.ZERO) < 0) {
        throw new IllegalArgumentException();
    }
    BigInteger z = x; // z will successively become x^2, x^4, x^8, x^16, x^32...
    BigInteger result = BigInteger.ONE;
    byte[] bytes = y.toByteArray();
    for (int i = bytes.length - 1; i >= 0; i--) {
        byte bits = bytes[i];
        for (int j = 0; j < 8; j++) {
            if ((bits & 1) != 0) {
                result = result.multiply(z);
            }
            // short cut out if there are no more bits to handle:
            if ((bits >>= 1) == 0 && i == 0) {
                return result;
            }
            z = z.multiply(z);
        }
    }
    return result;
}

最佳答案

您可以使用BigInteger类的专门设计 modPow方法

  ((A^z1 * y^z2) mod P) mod Q == ((((A^z1) mod P) * ((y^z2) mod P)) mod P) mod Q

可以放

  BigInteger A = ...
  BigInteger y = ...
  BigInteger z1 = ...
  BigInteger z2 = ...
  BigInteger P = ...
  BigInteger Q = ...

  BigInteger result = (A.modPow(z1, P).multiply(y.modPow(z2, P))).mod(P).mod(Q);

关于java - 我怎样才能快速 ((A^z1 * y^z2) mod P) mod Q,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284672/

相关文章:

使用 BouncyCaSTLe 使用 SHA-256 和 ECDSA 的 C# 签名数据每次都会产生不同的签名

java - 即使函数签名抛出它,编译器也会提示未处理的 IOException

java - Android如何在标签更改时停止刷新 fragment

java - 输入重定向到键盘输入

java - 如何读取.pem文件来获取私钥和​​公钥

hash - 如何使用氧化钠 crate 对字符串进行哈希处理?

node.js - 我如何使用 nodejs 中的 Diffie-Hellman api 实际加密某些内容?

java - 使用 Bouncy CaSTLe 解密 PEM 私钥 (RSA)

c - 在伽罗华域算法中优化 y = x*x

java - 我放了 security.provider.1=org.bouncycaSTLe.jce.provider.BouncyCaSTLeProvider 但在 SSL 握手期间没有使用它