2015-08-28 18 views
1

我想用RSA私鑰簽名的簽名,並與SHA-256 但即時編碼它總是有這種錯誤Python的RSA私有密鑰簽名錯誤

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

編碼爲「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')