'Verifiable Canonical Generation of the Generator g' 的 Python 代码,FIPS 186-4,返回 False

标签 python cryptography

我已经在 Crypto StackExchage 上问过这个问题并且已经有人回复了。我已经合并了答案中建议的更改,但它仍然返回 false。

更多背景信息:我正在查看 FIPS 186-4第43页有一个算法,A.2.3 可验证的生成器g的规范生成,用于生成生成器。我编写了一个 Python 代码(见下文)来编码该算法。但是,对于我从 NIST Test Vectors 获取的测试数据,它总是返回 false (测试向量、FIPS-186-4、DSA。我还将整个文件发布在 gist )。

enter image description here

from Crypto.Hash import SHA256


P = 0xff600483db6abfc5b45eab78594b3533d550d9f1bf2a992a7a8daa6dc34f8045ad4e6e0c429d334eeeaaefd7e23d4810be00e4cc1492cba325ba81ff2d5a5b305a8d17eb3bf4a06a349d392e00d329744a5179380344e82a18c47933438f891e22aeef812d69c8f75e326cb70ea000c3f776dfdbd604638c2ef717fc26d02e17
Q = 0xe21e04f911d1ed7991008ecaab3bf775984309c3
domain_parameter_seed = b'180180ee2f0ae4a7b3a1ab1b8414228913ef2911'
ggen = b'6767656e'
index = b'79'
G = 0xc52a4a0ff3b7e61fdf1867ce84138369a6154f4afa92966e3c827e25cfa6cf508b90e5de419e1337e07a2e9e2a3cd5dea704d175f8ebf6af397d69e110b96afb17c7a03259329e4829b0d03bbc7896b15b4ade53e130858cc34d96269aa89041f409136c7242a38895c9d5bccad4f389af1d7a4bd1398bd072dffa896233397a



def compute_gen():
    k = (P - 1) // Q
    for count in range(1, 0xffff):
        U = domain_parameter_seed + ggen + index + count.to_bytes(2, 'big')
        W = int.from_bytes(SHA256.new(U).digest(), 'big')
        g = pow(W, k, P)
        print(g)
        if g != 1:
            break
 
    return g


print (compute_gen() == G)

最佳答案

domain_parameter_seedggenindex 必须使用 bytes.fromhex() 转换为类字节对象:

U = bytes.fromhex((domain_parameter_seed + ggen + index).decode('utf8')) + count.to_bytes(2, 'big')

通过此更改,代码可以正常工作。

关于 'Verifiable Canonical Generation of the Generator g' 的 Python 代码,FIPS 186-4,返回 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70257030/

相关文章:

python - 提取gurobi多维变量值并形成一个numpy数组

javascript - 在 Facebook 应用程序中自动完成的替代方法

python - 使用 ssl 模块的 HTTPS 代理隧道

c# - RSACryptoServiceProvider 不产生一致的输出

c - PAM pam_sm_close_session 没有启动

python - matplotlib barplot 的最后一个栏被填充了一半

python - 我可以在 Django 的 View 中声明变量时将变量传递到表单中吗

cryptography - Javacard 在 APDU 中发送 RSA 公钥

c - 如何在C中使用RSA加密多个文件

security - TLS/SSL 握手中泄露了哪些信息(证书等)?