我需要使用 RSA 1.5 算法加密字符串。我已获得私钥。然而,我一生都无法弄清楚如何将这个密钥添加到班级中。看来密钥必须是 RSAParameter 结构类型。然而,这需要一组我尚未给出的值,例如模数、指数、P、Q 等。我拥有的只是私钥。有人可以帮忙吗?
您应该知道Bouncycastle C# 库 http://www.bouncycastle.org/csharp/。特别是有两个非常有用的类:Org.BouncyCastle.OpenSsl.PemReader
它将从您拥有的 openssl 样式密钥转换为 bouncycastle 密钥对象,并且Org.BouncyCastle.Security.DotNetUtilities
,这会将 bouncycastle 键转换为 .NETRSAParameters
object.
这是一小段未经测试的代码,展示了如何使用它
using System;
using System.IO;
using System.Security.Cryptography;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
namespace RSAOpensslToDotNet
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader("../../privatekey.pem");
PemReader pr = new PemReader(sr);
AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject();
RSAParameters rsa = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)