python等效于.NET中的RSA.SignData

时间:2017-04-17 14:41:18

标签: python rsa digital-signature pycrypto

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(PrivateKey);

string data = "SAMPLE TEXT.";

byte[] signedData = rsa.SignData(Encoding.UTF8.GetBytes(data), new
SHA1CryptoServiceProvider());
string signedString = Convert.ToBase64String(signedData);

上面的代码使用从xml加载的PrivateKey对字符串进行签名。

你能帮我在python中重写它吗?

我自己编写了这段代码,但它产生了不同的签名!

from Crypto.PublicKey import RSA
from Crypto.Util.number import bytes_to_long, long_to_bytes
from hashlib import sha1
import base64

msg = "SAMPLE TEXT."
hashed = sha1(msg.encode('utf-8')).digest()
pubkey = RSA.construct((m,e))
encrypted = pubkey.encrypt(hashed, b'X')[0]
print("Signature: {0}".format(base64.b64encode(encrypted)))

1 个答案:

答案 0 :(得分:0)

我知道Python中没有内置RSA库,但请查看支持this one (just named rsa)signing

public/packs