码迷,mamicode.com
首页 > 编程语言 > 详细

C#加密算法总结

时间:2015-07-29 13:51:04      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

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         }

 

 

 

C#加密算法总结

标签:

原文地址:http://www.cnblogs.com/ben121011/p/4685676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!