1 /// <summary> 2 /// 数字签名并返回BASE64编码的字符串 3 /// </summary> 4 /// <param name="contentForSign">待签名内容</param> 5 /// <param name="privateKeyFile">私钥文件路径</param> 6 /// <returns></returns> 7 internal static string Sign(string contentForSign, string privateKeyFile) 8 { 9 var rsa = CcbRsaCryptProvider.GetInstanceForSign(privateKeyFile); 10 var signData = rsa.SignData(Encoding.UTF8.GetBytes(contentForSign), new MD5CryptoServiceProvider()); 11 return Convert.ToBase64String(signData); 12 } 13 14 /// <summary> 15 /// 验证签名 16 /// </summary> 17 /// <param name="contentForSign">待签名内容</param> 18 /// <param name="signedData">签名内容</param> 19 /// <param name="publicKeyFile">公钥文件路径</param> 20 /// <returns>验证结果</returns> 21 internal static bool VerifySign(string contentForSign, string signedData, string publicKeyFile) 22 { 23 var rsaPub = CcbRsaCryptProvider.GetInstanceForVerifySign(publicKeyFile); 24 return rsaPub.VerifyData(Encoding.UTF8.GetBytes(contentForSign), new MD5CryptoServiceProvider(), Convert.FromBase64String(signedData)); 25 }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/15849.html