【问题标题】:pycryptodome equivalent C# RSA.SignHash?pycryptodome 等效 C# RSA.SignHash?
【发布时间】:2021-06-15 18:23:58
【问题描述】:

您好,我正在尝试使用 python https://pycryptodome.readthedocs.io/ 将一些 RSA 加密代码从 C# 应用程序移植到 python

但我的问题是我卡在这条线上

using System.Security.Cryptography;
...
 
result = RSA.SignHash(testData, CryptoConfig.MapNameToOID("SHA256"));

使用 python 我正在尝试这个

with open('key.pem', 'r') as f:
    private_key = RSA.import_key(f.read())
    h = SHA256.new(bytes(testData))
    signature = pkcs1_15.new(private_key).sign(h)

我尝试使用示例字节数组对这两个函数进行单元测试

testData = bytes(b'\x5b\xd4\x08\xae\x4f\x80\x5d\x77\xc4\x4e\xc8\x23\x63\x82\x79\x17\xad\x75\x3a\x2b\x2f\xa6\x80\xb1\x31\x74\x19\x8c\x29\x80\x38\x49')

但长度为 128 的结果输出(结果和签名变量)不一样。

我错过了什么?

【问题讨论】:

  • @Topaco 看起来 pycryptodome 只提供了一种方法来签署哈希而不是直接签署数据,或者我错过了一些明显的东西? pycryptodome.readthedocs.io/en/latest/src/signature/… 我无法修改正在使用的 C# 程序,所以我只能复制它已经在做的事情。

标签: python c# rsa python-cryptography


【解决方案1】:

通过将 pythonlibrary 从“python cryptodome”切换到“rsa”解决了我的问题 https://stuvel.eu/python-rsa-doc/usage.html#signing-and-verification

上面链接中有趣的代码:

(1)message = 'Go left at the blue tree'.encode()
(2)hash = rsa.compute_hash(message, 'SHA-1')
(3)signature = rsa.sign_hash(hash, privkey, 'SHA-1')

这个库将“sign”函数分成两部分,所以我基本上跳过了第 (2) 步,它对我有用。

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多