我需要在 python 中加密文本并在 C# 中解密。在python中,我有这个代码:
我在 Python 中有这段代码:
def genKey():
rsa = RSA.gen_key(2048, 65537)
rsa.save_key('c:/temp/priv-key.pem', callback=passwordCallback)
rsa.save_pub_key('c:/temp/pub-key.pem')
def encrypt():
varkey = readkey('c:/temp/pub-key.pem')
bio = BIO.MemoryBuffer(varkey)
rsa = RSA.load_pub_key_bio(bio)
encrypted = rsa.public_encrypt('My Text Here.', RSA.pkcs1_oaep_padding)
f = open("c:/temp/cript.txt", "w")
f.write(encrypted)
f.close()
此代码使用 M2Crypto。
就像我说的,我想解密 C# 中生成的结果。下面是我的代码:
static void Main(string[] args)
{
string text = GetText();
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
Byte[] payload = encoding.GetBytes(text);
byte[] b = System.IO.File.ReadAllBytes(@"C:\temp\priv-key.pem");
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
OpenSSL.Core.BIO bio = new OpenSSL.Core.BIO(b);
OpenSSL.Crypto.CryptoKey key = OpenSSL.Crypto.CryptoKey.FromPrivateKey(bio, "mypassword");
RSA rsa = key.GetRSA();
byte[] result = rsa.PrivateDecrypt(payload, RSA.Padding.OAEP);
}
问题是这一行:
byte[] result = rsa.PrivateDecrypt(payload, RSA.Padding.OAEP);
执行的时候出现这个错误:
error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error
error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed
密码学和 C# 的大师可以帮助我吗?