RSA签名和验证数据

         private const string PubKey = "BgIAAACkAABSU0ExAAQAAAEAAQAxg/L6l3AyA+Zd7Hm7ESCcS4CcgY8PvwE2arRvTbfI4+z6zPJaDkOyho9X6owIwceN9zXH5XNsAmEVYBkgmwdX5r5g8iymKywYSaVpt1FDUHRkGY39NuDVsbVJ/1DW15TTVeYLalbtOOb0h61pnaW9FgQNaYMMZSWPO8UZ9hPepw==";

         private const string PriKey = "BwIAAACkAABSU0EyAAQAAAEAAQAxg/L6l3AyA+Zd7Hm7ESCcS4CcgY8PvwE2arRvTbfI4+z6zPJaDkOyho9X6owIwceN9zXH5XNsAmEVYBkgmwdX5r5g8iymKywYSaVpt1FDUHRkGY39NuDVsbVJ/1DW15TTVeYLalbtOOb0h61pnaW9FgQNaYMMZSWPO8UZ9hPep+flgoHIqqmP8wwg4XY1KWHUXequmSryPkIpgcSs9EEVGZbxI8ai8i+OlpNH/1a6lmjyQzMzIK1sJ/5WubRFjMon+6iPki2vxviqxQeODX/yNRPglWsTLjzPiVxrPaNan++rAKWBBodsspaU+MZyYt4QT/AXzTi62OB2srB23SrUc89OqR9T7fhnFkfj/AymDqgEEI2JliQPGttj9ycx2e/bCllPISn5BP3Vcq6slUMk05YM/4+BVB/goTCrdXsbJAEoCtGAfkRBzldhHFtw5/vd49anfedXLFQcpkfGJbMVaO9deS6HyMBZ+XqS/6zxUJL2m3n6gKL5bgoCtqqdSYGAR4Hdrt0lWTfnscXauHDZ12FdzSp3egTDSROmEXHoogNhibz3Q0t6nNpwcoBjxCwZnz2Ej1hW84UpTnYN8ixAQbksoAprGCshxUo7x6NAXx/FaO8vxrwPliI8pAcDOJ3Z/JUKhfquRjINsg4DYTxxhEtpynBznRhrKpyifggiRvqeBITpcLpgP83dhEdz8Bf18MJw7lX9usEiX5XighQ0iABVtxTu7dBgIHWNTH6gVUWqe87yguFrfj4OA++CBYM=";
static void Main(string[] args)
{
var aa = "";
var e = SignData(aa,PriKey);
var dd = VerifySerialNum(aa, e, PubKey);
} /// <summary>
/// RSA签名数据
/// </summary>
/// <param name="input">要签名的数据</param>
/// <param name="priKey">base64编码的私钥</param>
/// <returns>签名后数据的base64字符串</returns>
public static string SignData(string input,string priKey)
{
try
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(Convert.FromBase64String(priKey));
var data = Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(rsa.SignData(data, new SHA1CryptoServiceProvider()));
}
}
catch (Exception)
{
return string.Empty;
}
} /// <summary>
/// 验证数据
/// </summary>
/// <param name="input">签名前数据</param>
/// <param name="serialNum">SHA1签名后数据base64编码字符串</param>
/// <param name="pubKey">base64编码的公钥</param>
/// <returns></returns>
public static bool VerifySerialNum(string input, string serialNum, string pubKey)
{
try
{
//导入公钥
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(Convert.FromBase64String(pubKey));
return rsa.VerifyData(Encoding.UTF8.GetBytes(input), new SHA1CryptoServiceProvider(),
Convert.FromBase64String(serialNum));
}
}
catch (Exception)
{
return false;
}
}
上一篇:分享一个通用的分页SQL


下一篇:Time Out 访问数据库超时处理 .NET