使用 OpenSSL.NET 和现有密钥解密 RSA
我有以下代码使用 OpenSSL.Net 生成 OpenSSL RSA 公钥和私钥。但是,我似乎找不到使用给定私钥解密数据的方法。我知道如果我调用生成密钥,然后调用相应的方法来加密和解密数据,它就可以正常工作。但是,如果我尝试从给定公钥的外部源解密某些内容,我该如何使用该密钥进行解密。
注意:请不要给出不使用 OpenSSL.NET 的示例。 Microsoft 加密提供程序比 OpenSSL 慢得多,无法满足我的速度要求。
谢谢!
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;
}
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.
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.
Thanks!
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终通过 OpenSSL.NET 的托管包装器上的对象浏览器弄清楚了这一点。这有效:
I ended up figuring it out through the object browser on the Managed Wrapper for OpenSSL.NET. This works:
我发现了这个,并认为这是我祈祷的答案。但是,使用 openssl (1.0.0e Mac os x) 生成 pub/priv 密钥,我无法从加密文本中再次获取明文 - 是否缺少 GetBytes/Baseencode 类型步骤?你遇到过这个吗?
编辑:我一发布,就遇到了一个使用 UTF8 编码而不是 ASCII 的不同示例,它有效!
Ben
I found this and thought it was the answer to my prayers. However using openssl (1.0.0e Mac os x) to generate pub/priv keys, i cannot get the clear text back again from the encrypted text - is there a GetBytes/Baseencode type step I'm missing? did you come across this?
EDIT: no sooner had i posted i then came across a different example using UTF8 encoding not ASCII, which works!
Ben