将代码从c++移植到使用RSA pkcs# 1私钥加密

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

本文关键字:RSA pkcs# 加密 私钥 代码 c++      更新时间:2023-10-16

我正在尝试将这段代码从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);

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

谢谢

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

添加:

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

最后,我使用了OpenSsl。. NET作为库。这篇文章最初解决了我的问题:使用OpenSSL解密RSA。. NET中已有键