程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#加密算法總結,

C#加密算法總結,

編輯:C#入門知識

C#加密算法總結,


C#加密算法總結

 

MD5加密

 1         /// <summary>
 2         /// MD5加密
 3         /// </summary>
 4         /// <param name="strPwd">原字符串</param>
 5         /// <returns>加密後字符串</returns>
 6         public static string GetMD5(string strPwd)
 7         {
 8             //MD5 對象創建的兩種方式
 9             //MD5 md5 = MD5.Create();
10             MD5 md5 = new MD5CryptoServiceProvider();
11             //將輸入的密碼轉換成字節數組
12             byte[] bPwd = Encoding.UTF8.GetBytes(strPwd);    
13             //計算指定字節數組的哈希值
14             byte[] bMD5 = md5.ComputeHash(bPwd);
15             //釋放加密服務提供類的所有資源
16             md5.Clear();   
17             StringBuilder sbMD5Pwd = new StringBuilder();
18             for (int i = 0; i < bMD5.Length; i++)
19             {
20                 //將每個字節數據轉換為2位的16進制的字符
21                 sbMD5Pwd.Append(bMD5[i].ToString("x2"));
22             }
23             return sbMD5Pwd.ToString();
24         }

 

SHA1加密

 1         /// <summary>
 2         /// 使用 SHA1 加密算法來加密
 3         /// </summary>
 4         /// <param name="sourceString">原字符串</param>
 5         /// <returns>加密後字符串</returns>
 6         public static string SHA1_Encrypt(string sourceString)
 7         {
 8             byte[] StrRes = Encoding.UTF8.GetBytes(sourceString);
 9             HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
10             StrRes = iSHA.ComputeHash(StrRes);
11             StringBuilder EnText = new StringBuilder();
12             foreach (byte iByte in StrRes)
13             {
14                 EnText.AppendFormat("{0:x2}", iByte);
15             }
16             return EnText.ToString();
17         }

 

SHA256加密

 1         /// <summary>
 2         /// SHA256 加密
 3         /// </summary>
 4         /// <param name="sourceString">原字符串</param>
 5         /// <returns>加密後字符串</returns>
 6         public static string SHA256_Encrypt(string sourceString)
 7         {
 8             byte[] data = Encoding.UTF8.GetBytes(sourceString);
 9             SHA256 shaM = SHA256.Create();
10             byte[] result = shaM.ComputeHash(data);
11             StringBuilder EnText = new StringBuilder();
12             foreach (byte iByte in result)
13             {
14                 EnText.AppendFormat("{0:x2}", iByte);
15             }
16             return EnText.ToString();
17         }

 

SHA384加密

 1         /// <summary>
 2         /// SHA384 加密
 3         /// </summary>
 4         /// <param name="sourceString">原字符串</param>
 5         /// <returns>加密後字符串</returns>
 6         public static string SHA384_Encrypt(string sourceString)
 7         {
 8             byte[] data = Encoding.UTF8.GetBytes(sourceString);
 9             SHA384 shaM = SHA384.Create();
10             byte[] result = shaM.ComputeHash(data);
11             StringBuilder EnText = new StringBuilder();
12             foreach (byte iByte in result)
13             {
14                 EnText.AppendFormat("{0:x2}", iByte);
15             }
16             return EnText.ToString();
17         }

 

SHA512加密

 

 1         /// <summary>
 2         /// SHA512_加密
 3         /// </summary>
 4         /// <param name="sourceString">原字符串</param>
 5         /// <returns>加密後字符串</returns>
 6         public static string SHA512_Encrypt(string sourceString)
 7         {
 8             byte[] data = Encoding.UTF8.GetBytes(sourceString);
 9             SHA512 shaM = new SHA512Managed();
10             byte[] result = shaM.ComputeHash(data);
11             StringBuilder EnText = new StringBuilder();
12             foreach (byte iByte in result)
13             {
14                 EnText.AppendFormat("{0:x2}", iByte);
15             }
16             return EnText.ToString();
17         }

 

 

 

 

AES加密、解密

 

 1         /// <summary>
 2         /// AES加密
 3         /// </summary>
 4         /// <param name="encryptStr">加密字符串</param>
 5         /// <param name="encryptKey">密鑰</param>
 6         /// <returns>密碼</returns>
 7         public static string AESEncrypt(string encryptStr, string encryptKey)
 8         {
 9             if (string.IsNullOrWhiteSpace(encryptStr))
10                 return string.Empty;
11 
12             encryptKey = StringHelper.SubString(encryptKey, 32);
13             encryptKey = encryptKey.PadRight(32, ' ');
14 
15             //分組加密算法
16             SymmetricAlgorithm des = Rijndael.Create();
17             byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptStr);//得到需要加密的字節數組 
18             //設置密鑰及密鑰向量
19             des.Key = Encoding.UTF8.GetBytes(encryptKey);
20             des.IV = _aeskeys;
21             byte[] cipherBytes = null;
22             using (MemoryStream ms = new MemoryStream())
23             {
24                 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
25                 {
26                     cs.Write(inputByteArray, 0, inputByteArray.Length);
27                     cs.FlushFinalBlock();
28                     cipherBytes = ms.ToArray();//得到加密後的字節數組
29                     cs.Close();
30                     ms.Close();
31                 }
32             }
33             return Convert.ToBase64String(cipherBytes);
34         }
35 
36         /// <summary>
37         /// AES解密
38         /// </summary>
39         /// <param name="decryptStr">解密字符串</param>
40         /// <param name="decryptKey">密鑰</param>
41         /// <returns>原碼</returns>
42         public static string AESDecrypt(string decryptStr, string decryptKey)
43         {
44             if (string.IsNullOrWhiteSpace(decryptStr))
45                 return string.Empty;
46 
47             decryptKey = StringHelper.SubString(decryptKey, 32);
48             decryptKey = decryptKey.PadRight(32, ' ');
49 
50             byte[] cipherText = Convert.FromBase64String(decryptStr);
51 
52             SymmetricAlgorithm des = Rijndael.Create();
53             des.Key = Encoding.UTF8.GetBytes(decryptKey);
54             des.IV = _aeskeys;
55             byte[] decryptBytes = new byte[cipherText.Length];
56             using (MemoryStream ms = new MemoryStream(cipherText))
57             {
58                 using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
59                 {
60                     cs.Read(decryptBytes, 0, decryptBytes.Length);
61                     cs.Close();
62                     ms.Close();
63                 }
64             }
65             return Encoding.UTF8.GetString(decryptBytes).Replace("\0", "");//將字符串後尾的'\0'去掉
66         }

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved