[英]Decrypt message with private key RSA
我有一個問題,如何從Windows 2008 Server中找到私鑰。
首先,我使用公開密鑰對數據加密,該數據是從URL HTTPS中提取的,如下所示:
public static string Encrypt(string Data)
{
try
{
var Crypto = new RSACryptoServiceProvider(2048);
var RsaKeyInfo = Crypto.ExportParameters(false);
RsaKeyInfo.Modulus = PublicKeyByte();
Crypto.ImportParameters(RsaKeyInfo);
var bytesData = Encoding.Unicode.GetBytes(Data);
var bytesCypherText = Crypto.Encrypt(bytesData, false);
var cypherText = Convert.ToBase64String(bytesCypherText);
return cypherText;
}
catch (Exception ex)
{
return null;
}
}
private static byte[] PublicKeyByte()
{
Uri u = new Uri("https:\\domain.com");
ServicePoint sp = ServicePointManager.FindServicePoint(u);
string groupName = Guid.NewGuid().ToString();
HttpWebRequest req = HttpWebRequest.Create(u) as HttpWebRequest;
req.ConnectionGroupName = groupName;
using (WebResponse resp = req.GetResponse())
{
}
sp.CloseConnectionGroup(groupName);
return sp.Certificate.GetPublicKey(); ;
}
現在我不知道如何在C#中提取私鑰來解密消息? 我想知道更多有關此的信息
謝謝,
我通過使用System.Security.Cryptography.X509Certificates進行加密和解密來提取證書文件.PFX和im來解決此問題:
public static string Encrypt(string data)
{
try
{
var path = @"certificate.pfx";
var password = "test";
var collection = new X509Certificate2Collection();
collection.Import(path, password, X509KeyStorageFlags.PersistKeySet);
var certificate = collection[0];
var publicKey = certificate.PublicKey.Key as RSACryptoServiceProvider;
var bytesData = Convert.FromBase64String(data);
var encryptedData = publicKey.Encrypt(bytesData, false);
var cypherText = Convert.ToBase64String(encryptedData);
return cypherText;
}
catch (Exception ex)
{
return null;
}
}
public static string Decrypt(string data)
{
try
{
var path = @"certificate.pfx";
var password = "test";
var collection = new X509Certificate2Collection();
collection.Import(path, password, X509KeyStorageFlags.PersistKeySet);
var certificate = collection[0];
var privateKey = certificate.PrivateKey as RSACryptoServiceProvider;
var bytesData = Convert.FromBase64String(data);
var dataByte = privateKey.Decrypt(bytesData, false);
return Convert.ToBase64String(dataByte);
}
catch (Exception ex)
{
return "";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.