C# .NET私钥Rsa加密
我需要使用RSA 1.5算法加密字符串。我已获得一个私钥。但是,我一辈子都不知道如何将这个键添加到类中。似乎钥匙需要是RSAPERAMETER stuct类型。然而,这需要一组我没有给出的值,如模数、指数、P、Q等。。我只有私钥。有人能帮忙吗?我想这就是你想要的:C# .NET私钥Rsa加密,c#,.net,cryptography,rsa,C#,.net,Cryptography,Rsa,我需要使用RSA 1.5算法加密字符串。我已获得一个私钥。但是,我一辈子都不知道如何将这个键添加到类中。似乎钥匙需要是RSAPERAMETER stuct类型。然而,这需要一组我没有给出的值,如模数、指数、P、Q等。。我只有私钥。有人能帮忙吗?我想这就是你想要的: // Import ASymmetric RSA Key from a system file. public static RSAParameters ImportRSAKey(String fileName)
// Import ASymmetric RSA Key from a system file.
public static RSAParameters ImportRSAKey(String fileName)
{
// Create a stream to a the specified system file.
Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
// Extract/Deserialize the key from the file.
IFormatter soapFormatter = new SoapFormatter();
RSAParameters rsaParameter =
(RSAParameters) soapFormatter.Deserialize(fileStream);
// Close the file stream.
fileStream.Close();
return rsaParameter;
}
要生成新密钥,可以使用方法
请参阅以下内容:
您应该知道。特别是有两个非常有用的类:
Org.BouncyCastle.OpenSsl.PemReader
,它将从您必须的OpenSsl样式密钥转换为BouncyCastle密钥对象;以及Org.BouncyCastle.Security.DotNetUtilities
,它将BouncyCastle密钥转换为.NETrsaparters
对象
下面是一些未经测试的代码,展示了如何使用它
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);
}
}
}
你的钥匙是什么?显然,你可能不想一字不差地提供,但你能再描述一下吗?把你试过的贴出来。老实说,我怀疑您根本不了解RSA是如何工作的。我不知道你们使用的是什么课程,所以我不能给出建议。阅读以下内容:我收到的密钥看起来像------开始RSA私钥------MIIadfdafCXdfawIBAAKBgQCIgynd6pvlCF=-----结束RSA私钥------开始公钥------jaz+wadfaidaqab------结束公钥------这是我所有的..谢谢!!这正是我所需要的。我已经在很多地方找过了。。。谢谢我需要用Java完成这件事。当我使用pr.readObject()时,它返回PEMKeyPair,并在我将其转换为AsymmetricipherKeyPair时引发异常。为了我的生命,我不明白为什么。请帮忙!为我引发InvalidCastException。@mikerobi:您确定您的密钥文件包含私钥吗?