MD5加密

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ExamManagementsystemOffline.Unitity
 8 {
 9     using System.Security.Cryptography;
10 
11     public class SecurityHelper
12     {
13         /// <summary>
14         /// MD5加密
15         /// </summary>
16         /// <param name="message"></param>
17         /// <returns></returns>
18         public static string MD5Compute(string Message)
19         {
20             //建立MD5對象
21             MD5 mD5 = MD5.Create();
22 
23             //將編碼後的字節數組進行哈希計算,而後保存到加密數組中
24             byte[] mD5Bytes = mD5.ComputeHash(Encoding.UTF8.GetBytes(Message));
25 
26             //實例化StringBuilder 對象
27             StringBuilder stringBuilderMD5 = new StringBuilder();
28 
29             //遍歷加密數組,將數組元素以16位序列保存到stringBuilderMD5中
30             for (int i = 0; i < mD5Bytes.Length; i++)
31             {
32                 stringBuilderMD5.Append(mD5Bytes[i].ToString("X2"));
33             }
34 
35             //將stringBuilderMD5對象轉成字符串,並返回該值
36             return stringBuilderMD5.ToString();
37         }
38     }
39 }
相關文章
相關標籤/搜索