我从网上找到了一个C#版本的MD5WithRSA的签名的方法,但是校验这个签名的时候怎么用呢?有点蒙。
源码是
private static char[] bcdLookup = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /// <summary> /// 返回MD5WithRSA的签名字符串 /// </summary> /// <param name="fileName">pfx证书文件的路径</param> /// <param name="password">pfx证书密码</param> /// <param name="strdata">待签名字符串</param> /// <param name="encoding">字符集,默认为ISO-8859-1</param> /// <returns>返回MD5WithRSA的签名字符串</returns> public static string SignData(string fileName, string password, string strdata, string encoding = "UTF-8") { X509Certificate2 objx5092; if (string.IsNullOrWhiteSpace(password)) { objx5092 = new X509Certificate2(fileName); }else { objx5092 = new X509Certificate2(fileName, password); } RSACryptoServiceProvider rsa = objx5092.PrivateKey as RSACryptoServiceProvider; byte[] data = Encoding.GetEncoding(encoding).GetBytes(strdata); byte[] hashvalue = rsa.SignData(data, "MD5");//为证书采用MD5withRSA 签名 return bytesToHexStr(hashvalue);///将签名结果转化为16进制字符串 } /// <summary> /// 将签名结果转化为16进制字符串 /// </summary> /// <param name="bcd">签名结果的byte数字</param> /// <returns>16进制字符串</returns> private static string bytesToHexStr(byte[] bcd) { StringBuilder s = new StringBuilder(bcd.Length * 2); for (int i = 0; i < bcd.Length; i++) { s.Append(bcdLookup[(bcd[i] >> 4) & 0x0f]); s.Append(bcdLookup[bcd[i] & 0x0f]); } return s.ToString(); }
校验的时候再调用一次啊..2次返回值一样.原文就验证通过.