[英]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#: 我正在尝试将这段代码从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);
...
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 the 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. 可以使用ImportParameters方法将RSAParameters(http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rsaparameters.aspx)馈送到RSACryptoServiceProvider类。 You can encode the key within the RSAParameters structure.
您可以在RSAParameters结构内对密钥进行编码。
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. 最后,我使用OpenSsl.NET作为库。 This post originally solved my problem: Decrypting RSA using OpenSSL.NET with Existing Key
这篇文章最初解决了我的问题: 使用带有现有密钥的OpenSSL.NET解密RSA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.