开发者

Porting code from c++ to c# - Encrypting with RSA PKCS#1 private key

I'm trying to port this piece of code from c++ to 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);

...

So far here is my code:

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);

EDIT: I'm not ever sure if it's t开发者_如何学Gohe class I should refer to.

Thanks


RSAParameters (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters.aspx) can be fed to the RSACryptoServiceProvider class using the ImportParameters method. You can encode the key within the RSAParameters structure.


Fixed it by adding:

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-----"

And finally, I used OpenSsl.NET as library. This post originally solved my problem: Decrypting RSA using OpenSSL.NET with Existing Key

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜