62,025
社区成员
发帖
与我相关
我的任务
分享
private string Decrypt(string encryptedData, string privateKey)
{
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(privateKey);
byte[] _encryptedData = hexStrToByte(encryptedData);
byte[] decryptedData = RSA.Decrypt(_encryptedData, false);
return Encoding.UTF8.GetString(decryptedData);
}
}
private string BytesToHexString(byte[] input)
{
StringBuilder hexString = new StringBuilder(64);
for (int i = 0; i < input.Length; i++)
{
hexString.Append(String.Format("{0:X2}", input[i]));
}
return hexString.ToString();
}
/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public static byte[] HexStringToBytes(string hex)
{
if (hex.Length == 0)
{
return new byte[] { 0 };
}
if (hex.Length % 2 == 1)
{
hex = "0" + hex;
}
byte[] result = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length / 2; i++)
{
result[i] = byte.Parse(hex.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return result;
}