Python RSA私钥签名错误

时间:2015-08-28 04:12:18

标签: python rsa digital-signature

我想使用rsa私钥签名,并使用sha-256对其进行编码 但我总是有这个错误

TypeError: Unicode-objects must be encoded before hashing

以下是代码段:

private_key = rsa.PrivateKey.load_pkcs1(open('private.pem', 'rb').read())
signature = rsa.sign(sign, private_key, 'SHA-256')

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

编码为“utf-8”应该适合你。

private_key = rsa.PrivateKey.load_pkcs1(open('private_key.pem', 'rb').read())
signature = rsa.sign(sign.encode("utf-8"), private_key, 'SHA-256')