使用 m2crypto 验证 RSACryptoServiceProvider 消息签名

发布于 2024-12-24 18:46:02 字数 1145 浏览 4 评论 0原文

我收到一条用 .Net RSACryptoServiceProvider 签名的消息,然后将该消息发送到 Python 软件尝试使用 m2crypto(基于 openssl 的库)验证签名。

我有公共证书,它是消息签名所用的私钥对。

在.Net软件发送sing之前,它使用ByteArrayToString将其转换,因为符号是用以下代码生成的:

  //Convert plain text into a byte array to sign.
   byte[] data = new UTF8Encoding().GetBytes(phrase);
   SHA1Managed sha1 = new SHA1Managed();
   byte[] hashBytes = sha1.ComputeHash(data); 

   byte[] sig = csp.SignData(hashBytes, CryptoConfig.MapNameToOID("SHA1")); 

所以发送到接收者的签名是

ByteArrayToString(sig)

Python部分是:

   def verify(message, signature, cert_path):
      msg = hashlib.sha1(message).hexdigest()
      certificate = M2Crypto.X509.load_cert(cert_path)
      pubkey = certificate.get_pubkey()
      pubkey.reset_context(md='sha1')
      pubkey.verify_init()
      pubkey.verify_update(msg)
      sgn = signature.decode('base64')
      is_verified = pubkey.verify_final(sgn)

我无法得到验证...我认为可能有Python 部分中的签名编码存在问题,但也许有人可以在这里看到任何其他错误?

当我使用我自己的证书(用 openssl 生成)并从 Python 本身对消息进行签名时,对签名进行编码,发送它,然后解码并验证一切是否正常工作......

我将不胜感激任何评论!

I have got a message signed with .Net RSACryptoServiceProvider, the message is then send to
Python software which tries to verify the sign with m2crypto (openssl-based lib).

I've got public cert that is the pair for the private key the message was signed with.

Before .Net software sends the sing it converts it using ByteArrayToString, because the sign is generated with the following code:

  //Convert plain text into a byte array to sign.
   byte[] data = new UTF8Encoding().GetBytes(phrase);
   SHA1Managed sha1 = new SHA1Managed();
   byte[] hashBytes = sha1.ComputeHash(data); 

   byte[] sig = csp.SignData(hashBytes, CryptoConfig.MapNameToOID("SHA1")); 

So the signature sended to the receiver is

ByteArrayToString(sig)

The Python part is:

   def verify(message, signature, cert_path):
      msg = hashlib.sha1(message).hexdigest()
      certificate = M2Crypto.X509.load_cert(cert_path)
      pubkey = certificate.get_pubkey()
      pubkey.reset_context(md='sha1')
      pubkey.verify_init()
      pubkey.verify_update(msg)
      sgn = signature.decode('base64')
      is_verified = pubkey.verify_final(sgn)

And I can not get the verification... I think there might be an issue with signature encoding in the Python part, but maybe someone can see any other bugs here?

When I use my own certificate (generated with openssl) and sign the message from the Python itself, encode the signature, send it, then decode and verify everything is working fine...

I would appreciate any comments!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素罗衫 2024-12-31 18:46:02

我无法保证我的答案,因为我从未使用过 m2crypto,但我认为该消息被散列了两次。在

sha1.ComputeHash(数据)

csp.SignData(hashBytes, CryptoConfig.MapNameToOID("SHA1"))

MSDN 文档< /a> about SignData 指定:

使用以下方法计算指定字节数组的哈希值
指定哈希算法,并对结果哈希值进行签名。

因此,我认为您不应该在生成签名之前对消息进行预哈希处理。

I cannot guarantee my answer since I never used m2crypto but I think that the message is hashed twice. In

sha1.ComputeHash(data)

and

csp.SignData(hashBytes, CryptoConfig.MapNameToOID("SHA1"))

The MSDN documentation about SignData specifies:

Computes the hash value of the specified byte array using the
specified hash algorithm, and signs the resulting hash value.

Therefore I don't think you should pre-hash the message before producing the signature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文