使用MD5WithRSA來簽名和驗籤(.NET)

FrankYou發表於2015-07-21
 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         }

 

相關文章