关于C#rsa解密的问题,代码里面引用了BouncyCastle.CS文件,但是解出来的结果乱码
static void Main(string[] args)
{
var rsaHelper = new RsaHelper();
var test1 =
"i0s9lv0QuefmmXWkWxqIwJMcK1/uOjTTIpc0K2YWAwFGuMSbQ651Zfl4i7MY/Qi/vG8+J7SRyb3UWiS5CNP3M3MI2tC6l2CVAGv0godSMaeWbMqj9g17KwJr9r3EYtPvc8lRi8ec3qS8plkoPUCoz3PtsK LZ9hBlAXKD3f8qGDI=";
var key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxtkuwBI6novN6IKoP3AUe7vQnT +3NqYy0SJqGTf4u5sWuKj6jwpvBSf17DoBGAXPVfzQjtl3xNymn/87c7qzDzollBH1dXAdg5olwp0tAUVEWDKuCgaFGjsqnrTYxTvOqTf1J08mIQf3Tngkw1WuNBdADUiBsTlgpO9k+ffIL4wIDAQAB";
var source = rsaHelper.DecryptByPublibKey(test1, key);
Console.WriteLine("通过公钥解密后的原文是:{0}", source);
Console.ReadKey();
}
/// <summary>
/// 公钥解密(key公钥解密)- 必须对应私钥加密
/// </summary>
/// <param name="source">加密后的数据(密文)</param>
/// <param name="publicKey">公钥</param>
/// <returns></returns>
public string DecryptByPublibKey(string source, string publicKey)
{
var publicInfoByte = Convert.FromBase64String(publicKey);
Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入
AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(SubjectPublicKeyInfo.GetInstance(pubKeyObj));
//开始解密
IAsymmetricBlockCipher cipher = new RsaEngine();
cipher.Init(false, pubKey);
//解密已加密的数据
byte[] encryptedData = Convert.FromBase64String(source);
encryptedData = cipher.ProcessBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(encryptedData, 0, encryptedData.Length);
}
哈哈~都解决了....
您好,请问是怎么解决的