2

我正在尝试将这段代码从 c++ 移植到 c#:

...
strPrivateKey = "someBase64EncodedPrivateKey";
long sizeKey = DecodeBase64(strPrivateKey, pKey);
const unsigned char* _pKey = pKey;
d2i_RSAPrivateKey(&pRSA, &_pKey, sizeKey);
...

RSA_private_encrypt(sizeOfMessage, pMessage, pSignature, pRSA, RSA_PKCS1_PADDING);

...

到目前为止,这是我的代码:

var strPrivateKey = "someBase64EncodedPrivateKey";
var bytes = Convert.FromBase64String(strPrivateKey);

var rsa = new RSACryptoServiceProvider();

// How to set the private key to the rsa object?!

byte[] someDataToEncrypt = /* Set the data to encrypt */;
var encryptedData = rsa.Encrypt(someDataToEncrypt, false);

编辑:我不确定这是否是我应该参考的课程。

谢谢

4

2 回答 2

0

通过添加修复它:

At the begin of the private key: "-----BEGIN RSA PRIVATE KEY-----\r\n"
After each line of my private key : "\r\n"
At the end of my private key: "-----END RSA PRIVATE KEY-----"

最后,我使用 OpenSsl.NET 作为库。这篇文章最初解决了我的问题: Decrypting RSA using OpenSSL.NET with Existing Key

于 2011-08-29T20:20:18.660 回答
0

可以使用ImportParameters方法将 RSAParameters (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx) 提供给 RSACryptoServiceProvider 类。您可以在 RSAParameters 结构中对密钥进行编码。

于 2011-05-06T22:36:50.430 回答