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 }
DES加密
1 /// <summary> 2 /// DES加密字符串 3 /// </summary> 4 /// <param name="encryptString">待加密的字符串</param> 5 /// <param name="encryptKey">加密密钥,要求为8位</param> 6 /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> 7 public string EncryptDES(string encryptString, string encryptKey) 8 { 9 try 10 { 11 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); 12 byte[] rgbIV = Keys; 13 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 14 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); 15 MemoryStream mStream = new MemoryStream(); 16 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 17 cStream.Write(inputByteArray, 0, inputByteArray.Length); 18 cStream.FlushFinalBlock(); 19 return Convert.ToBase64String(mStream.ToArray()); 20 } 21 catch 22 { 23 return encryptString; 24 } 25 } 26 27 /// <summary> 28 /// DES解密字符串 29 /// </summary> 30 /// <param name="decryptString">待解密的字符串</param> 31 /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> 32 /// <returns>解密成功返回解密后的字符串,失败返源串</returns> 33 public string DecryptDES(string decryptString, string decryptKey) 34 { 35 try 36 { 37 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); 38 byte[] rgbIV = Keys; 39 byte[] inputByteArray = Convert.FromBase64String(decryptString); 40 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); 41 MemoryStream mStream = new MemoryStream(); 42 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 43 cStream.Write(inputByteArray, 0, inputByteArray.Length); 44 cStream.FlushFinalBlock(); 45 return Encoding.UTF8.GetString(mStream.ToArray()); 46 } 47 catch 48 { 49 return decryptString; 50 } 51 }
加密方法封装代码:
1 using System.IO; 2 using System.Security.Cryptography; 3 using System.Text; 4 5 namespace EncryptTools 6 { 7 public class EncryptHelper 8 { 9 /// <summary> 10 /// 获取某个哈希算法对应下的哈希值 11 /// </summary> 12 /// <param name="sourceString">源字符串</param> 13 /// <param name="algorithm">哈希算法</param> 14 /// <returns>经过计算的哈希值</returns> 15 private static string GetHash(string sourceString, HashAlgorithm algorithm) 16 { 17 byte[] sourceBytes = Encoding.UTF8.GetBytes(sourceString); 18 byte[] result = algorithm.ComputeHash(sourceBytes); 19 algorithm.Clear(); 20 StringBuilder sb = new StringBuilder(32); 21 for (int i = 0; i < result.Length; i++) 22 { 23 sb.Append(result[i].ToString("X2")); 24 } 25 return sb.ToString(); 26 } 27 28 /// <summary> 29 /// 获取MD5值 30 /// </summary> 31 /// <param name="sourceString">源字符串</param> 32 /// <returns>MD5值</returns> 33 public static string GetMD5(string sourceString) 34 { 35 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 36 return GetHash(sourceString, md5); 37 } 38 39 /// <summary> 40 /// 获取SHA1值 41 /// </summary> 42 /// <param name="sourceString">源字符串</param> 43 /// <returns>SHA1值</returns> 44 public static string GetSHA1(string sourceString) 45 { 46 SHA1 sha1 = new SHA1CryptoServiceProvider(); 47 return GetHash(sourceString, sha1); 48 } 49 50 /// <summary> 51 /// 获取SHA256值 52 /// </summary> 53 /// <param name="sourceString">源字符串</param> 54 /// <returns>SHA256值</returns> 55 public static string GetSHA256(string sourceString) 56 { 57 SHA256 sha256 = SHA256.Create(); 58 return GetHash(sourceString, sha256); 59 } 60 61 /// <summary> 62 /// 获取SHA384值 63 /// </summary> 64 /// <param name="sourceString">源字符串</param> 65 /// <returns>SHA384值</returns> 66 public static string GetSHA384(string sourceString) 67 { 68 SHA384 sha384 = SHA384.Create(); 69 return GetHash(sourceString, sha384); 70 } 71 72 /// <summary> 73 /// 获取SHA512值 74 /// </summary> 75 /// <param name="sourceString">源字符串</param> 76 /// <returns>SHA512值</returns> 77 public static string GetSHA512(string sourceString) 78 { 79 SHA512 sha512 = SHA512.Create(); 80 return GetHash(sourceString, sha512); 81 } 82 83 public static string GetFileBase64String(string filePath) 84 { 85 using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) 86 { 87 using (BinaryReader reader = new BinaryReader(fs)) 88 { 89 try 90 { 91 return GetBase64String(reader.ReadBytes((int)fs.Length)); 92 } 93 catch (System.Exception ex) 94 { 95 throw ex; 96 } 97 } 98 } 99 } 100 101 public static string GetBase64String(string sourceString) 102 { 103 byte[] buffer = Encoding.UTF8.GetBytes(sourceString); 104 return GetBase64String(buffer); 105 } 106 107 public static string GetBase64String(string sourceString, Encoding encoding) 108 { 109 byte[] buffer = encoding.GetBytes(sourceString); 110 return GetBase64String(buffer); 111 } 112 113 public static string GetBase64String(byte[] sourceBytes) 114 { 115 string base64String = System.Convert.ToBase64String(sourceBytes); 116 return base64String; 117 } 118 } 119 }
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。