使用OpenSSL.NET与现有密钥解密RSA [英] Decrypting RSA using OpenSSL.NET with Existing Key
问题描述
我有以下代码,使用OpenSSL.Net生成OpenSSL RSA公钥和私钥。但是,我似乎找不到使用给定私钥解密数据的方法。我知道如果我调用生成的密钥,然后相应的方法加密和解密数据,它工作正常。但是,如果我正在尝试从给定公钥的外部源解密某些东西,那么如何使用该密钥进行解密。
I have the following code below to generate an OpenSSL RSA public and private key using OpenSSL.Net. However, I can't seem to find a way to decrypt data with a given private key. I know if I call generate keys and then the corresponding methods to encrypt and decrypt the data it works fine. However, if I am trying to decrypt something from an external source given a public key, how can I decrypt using that key.
注意:请不要举例说明不要使用OpenSSL.NET。 Microsoft加密提供商比OpenSSL慢得多,不符合我的速度要求。
Note: Please do not give examples that don't use OpenSSL.NET. The Microsoft Cryptographic providers are far slower than OpenSSL and do not meet my speed requirements.
谢谢!
public class AsymmetricKeyResult
{
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
public AsymmetricKeyResult(string publicKey, string privateKey)
{
this.PublicKey = publicKey;
this.PrivateKey = privateKey;
}
}
public static AsymmetricKeyResult GenerateAsymmetricKeys(int keyLength)
{
RSA rsa = new RSA();
rsa.GenerateKeys(keyLength, 0x10021, null, null);
AsymmetricKeyResult kResult = new AsymmetricKeyResult(rsa.PublicKeyAsPEM, rsa.PrivateKeyAsPEM);
return kResult;
}
推荐答案
我最终弄清楚了通过OpenSSL.NET的Managed Wrapper的对象浏览器。这个工作原理:
I ended up figuring it out through the object browser on the Managed Wrapper for OpenSSL.NET. This works:
public static byte[] AsymmetricEncrypt(string publicKeyAsPem, byte[] payload)
{
CryptoKey d = CryptoKey.FromPublicKey(publicKeyAsPem, null);
RSA rsa = d.GetRSA();
byte[] result = rsa.PublicEncrypt(payload, RSA.Padding.PKCS1);
rsa.Dispose();
return result;
}
public static byte[] AsymmetricDecrypt(string privateKeyAsPem, byte[] payload)
{
CryptoKey d = CryptoKey.FromPrivateKey(privateKeyAsPem, null);
RSA rsa = d.GetRSA();
byte[] result = rsa.PrivateDecrypt(payload, RSA.Padding.PKCS1);
rsa.Dispose();
return result;
}
这篇关于使用OpenSSL.NET与现有密钥解密RSA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!