构建一个.net的干货类库,以便于快速的开发 - 加密
在开发程序的时候,加密是一个程序一个必须的功能,基本上任何程序都会用到加密,而不同的加密方式又适应不同需求,有些加密是不可逆的,最常见是用于用户密码的加密,因为很多时候程序里面不该显示出用户的明文密码,有些加密是可逆的,大多数用于一些私密的资料,例如用户的联系方式,私密信息等,这种加密方式能很好的保护用户的资料。加密方式有很多种,网上可以查到的加密方式有很多,但是很多都不全面,本文就在这里列出比较全面的加密方式。
1、MD5加密
这种加密方式不用我介绍了吧,最常见的,是一种不可逆加密
1 /// <summary> 2 /// MD5加密(只能转字符串类型) 3 /// </summary> 4 /// <param name="str"></param> 5 /// <returns></returns> 6 public static string toMD5(string str) 7 { 8 string strResult = str; 9 if (!string.IsNullOrWhiteSpace(str)) 10 { 11 string strSource = str.ToString(); 12 //new 13 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); 14 15 //获取密文字节数组 16 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource)); 17 18 //转换成字符串,并取9到25位 19 strResult = BitConverter.ToString(bytResult, 4, 8); 20 //转换成字符串,32位 21 //string strResult = BitConverter.ToString(bytResult); 22 23 //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉 24 strResult = strResult.Replace("-", ""); 25 } 26 return strResult; 27 }
2、哈希加密
(提供参考)
1 /// <summary> 2 /// 哈希加密(只能转字符串类型) 3 /// </summary> 4 /// <param name="str"></param> 5 /// <returns></returns> 6 public static string toHash(string str) 7 { 8 string strResult = ""; 9 if (!string.IsNullOrWhiteSpace(str)) 10 { 11 string strSource = str.ToString(); 12 //Create 13 System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); 14 15 //注意编码UTF8、UTF7、Unicode等的选择 16 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource)); 17 18 //字节类型的数组转换为字符串 19 for (int i = 0; i < bytResult.Length; i++) 20 { 21 //16进制转换 22 strResult = strResult + bytResult[i].ToString("X"); 23 } 24 } 25 return strResult; 26 }
3、DES加密(解密)
这种加密是一种基于密匙的对称加密
1 #region DES加密 2 //默认密钥向量 3 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 4 /// <summary> 5 /// DES加密 6 /// </summary> 7 /// <param name="encryptString">待加密的字符串</param> 8 /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> 9 public static string toEncryptDES(string encryptString) 10 { 11 try 12 { 13 string encryptKey = ASCIIEncoding.ASCII.GetString(Keys); 14 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); 15 byte[] rgbIV = Keys; 16 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 17 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); 18 MemoryStream mStream = new MemoryStream(); 19 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 20 cStream.Write(inputByteArray, 0, inputByteArray.Length); 21 cStream.FlushFinalBlock(); 22 return Convert.ToBase64String(mStream.ToArray()); 23 } 24 catch 25 { 26 return encryptString; 27 } 28 } 29 #endregion 30 31 #region DES解密 32 /// <summary> 33 /// DES解密 34 /// </summary> 35 /// <param name="decryptString">待解密的字符串</param> 36 /// <returns>解密成功返回解密后的字符串,失败返源串</returns> 37 public static string toDecryptDES(string decryptString) 38 { 39 try 40 { 41 string decryptKey = ASCIIEncoding.ASCII.GetString(Keys); 42 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); 43 byte[] rgbIV = Keys; 44 byte[] inputByteArray = Convert.FromBase64String(decryptString); 45 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); 46 MemoryStream mStream = new MemoryStream(); 47 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 48 cStream.Write(inputByteArray, 0, inputByteArray.Length); 49 cStream.FlushFinalBlock(); 50 return Encoding.UTF8.GetString(mStream.ToArray()); 51 } 52 catch 53 { 54 return decryptString; 55 } 56 } 57 #endregion
4、RSA加密(解密)
RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击。如果有过支付宝开发经验的开发人员对这个一定不陌生。
4.1、密匙生成
RSA加密方法需要两个文件用来存放公钥和私钥,在程序里建立key文件来存放这两个文件,由于文件是.pem格式的文件目前没有一种好的读取方式,所以先将文件转换成其他格式文件再读取
1 /// <summary> 2 /// 密匙生成(慎用) 3 /// 注意:当使用此方法时会重新生成一次密匙,之前生成的密匙会被覆盖,使用此方法时建议先进行密匙备份 4 /// 首次使用此方法加密要在程序上建立\Key\publicKey.xml和\Key\privateKey.xml两个文件 5 /// </summary> 6 public static void CreateKey() 7 { 8 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) 9 { 10 //pem文件解析,把pem转换成xml文件 11 string fileName = Utiltity.GetRootPath() + @"\Key\publicKey.xml"; 12 using (FileStream fsRead = new FileStream(fileName, FileMode.Create)) 13 { 14 string publicKey = rsa.ToXmlString(false); // 公钥 15 StreamWriter sw = new StreamWriter(fsRead); 16 fsRead.SetLength(0);//首先把文件清空了。 17 sw.Write(publicKey);//写你的字符串。 18 sw.Close(); 19 string dfileName = System.IO.Path.ChangeExtension(fileName, ".pem"); 20 File.Delete(dfileName); 21 File.Move(fileName, dfileName); 22 } 23 24 //pem文件解析,把pem转换成xml文件 25 fileName = Utiltity.GetRootPath() + @"\Key\privateKey.xml"; 26 using (FileStream fsRead = new FileStream(fileName, FileMode.Create)) 27 { 28 string privateKey = rsa.ToXmlString(true); // 私钥 29 StreamWriter sw = new StreamWriter(fsRead); 30 fsRead.SetLength(0);//首先把文件清空了。 31 sw.Write(privateKey);//写你的字符串。 32 sw.Close(); 33 string dfileName = System.IO.Path.ChangeExtension(fileName, ".pem"); 34 File.Delete(dfileName); 35 File.Move(fileName, dfileName); 36 } 37 } 38 }
4.2加密(解密)
解密要基于对称的密匙,如果不是就解密不了
1 /// <summary> 2 /// RSA加密 3 /// </summary> 4 /// <param name="content">需加密的字符串</param> 5 /// <returns></returns> 6 public static string toEncryptRSA(string content) 7 { 8 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 9 //pem文件解析,把pem转换成xml文件 10 string fileName = Tool.GetRootPath() + @"\Key\publicKey.pem"; 11 string dfileName = System.IO.Path.ChangeExtension(fileName, ".xml"); 12 File.Move(fileName, dfileName); 13 //文件读取 14 using (FileStream fsRead = new FileStream(dfileName, FileMode.Open)) 15 { 16 StreamReader sr = new StreamReader(fsRead); 17 string publickey = sr.ReadLine();//System.Text.Encoding.UTF8.GetString(heByte); 18 rsa.FromXmlString(publickey); 19 byte[] cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); 20 content = Convert.ToBase64String(cipherbytes); 21 } 22 File.Move(dfileName, fileName); 23 return content; 24 } 25 26 /// <summary> 27 /// RSA解密 28 /// </summary> 29 /// <param name="content">加密的字符串</param> 30 /// <returns></returns> 31 public static string toDecryptRSA(string content) 32 { 33 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 34 //pem文件解析,把pem转换成xml文件 35 string fileName = Tool.GetRootPath() + @"\Key\privatekey.pem"; 36 string dfileName = System.IO.Path.ChangeExtension(fileName, ".xml"); 37 File.Move(fileName, dfileName); 38 //文件读取 39 using (FileStream fsRead = new FileStream(dfileName, FileMode.Open)) 40 { 41 //int fsLen = (int)fsRead.Length; 42 //byte[] heByte = new byte[fsLen]; 43 StreamReader sr = new StreamReader(fsRead); 44 string privatekey = sr.ReadLine();//System.Text.Encoding.UTF8.GetString(heByte); 45 rsa.FromXmlString(privatekey); 46 byte[] cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); 47 content = Encoding.UTF8.GetString(cipherbytes); 48 } 49 File.Move(dfileName, fileName); 50 return content; 51 }
5、AES对称加密
也是一种基于密匙的对称加密
1 #region AES对称加密 2 /// <summary> 3 /// 默认密码钥 4 /// </summary> 5 private static string EncryptKey = "6FFDC945FA1752467C7F238A3BAB4FEA"; 6 7 /// <summary> 8 /// 算法初始IV种子 9 /// </summary> 10 private static string ivString = "1D2CFACB382AE6AD36BD4F4C"; 11 12 /// <summary> 13 /// AES 加密 14 /// </summary> 15 /// <param name="encryptString">待加密密文</param> 16 /// <param name="key">密钥</param> 17 /// <returns></returns> 18 public static string AESEncrypt(string encryptString, string key) 19 { 20 if (string.IsNullOrEmpty(encryptString)) 21 return null; 22 if (string.IsNullOrEmpty(key)) { throw (new Exception("密钥不得为空")); } 23 string m_strEncrypt = ""; 24 byte[] m_btIV = Convert.FromBase64String(ivString); 25 Rijndael m_AESProvider = Rijndael.Create(); 26 try 27 { 28 byte[] m_btEncryptString = Encoding.UTF8.GetBytes(encryptString); 29 MemoryStream m_stream = new MemoryStream(); 30 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.UTF8.GetBytes(key), m_btIV), CryptoStreamMode.Write); 31 m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_csstream.FlushFinalBlock(); 32 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); 33 m_strEncrypt = HttpUtility.UrlEncode(m_strEncrypt, Encoding.UTF8); 34 m_stream.Close(); m_stream.Dispose(); 35 m_csstream.Close(); m_csstream.Dispose(); 36 } 37 catch (IOException ex) { throw ex; } 38 catch (CryptographicException ex) { throw ex; } 39 catch (ArgumentException ex) { throw ex; } 40 catch (Exception ex) { throw ex; } 41 finally { m_AESProvider.Clear(); } 42 return m_strEncrypt; 43 } 44 45 /// <summary> 46 /// AES 加密 47 /// </summary> 48 /// <param name="encryptString">待加密密文</param> 49 /// <returns></returns> 50 public static string AESEncrypt(string encryptString) 51 { 52 return AESEncrypt(encryptString, EncryptKey); 53 } 54 #endregion 55 56 #region AES对称解密 57 /// <summary> 58 /// AES 解密 59 /// </summary> 60 /// <param name="decryptString">待解密密文</param> 61 /// <param name="key">解密密钥</param> 62 /// <returns></returns> 63 public static string AESDecrypt(string decryptString, string key) 64 { 65 if (string.IsNullOrEmpty(decryptString)) 66 return null; 67 if (string.IsNullOrEmpty(key)) { throw (new Exception("密钥不得为空")); } 68 string m_strDecrypt = ""; 69 byte[] m_btIV = Convert.FromBase64String(ivString); 70 Rijndael m_AESProvider = Rijndael.Create(); 71 try 72 { 73 if (HttpContext.Current == null) 74 decryptString = HttpUtility.UrlDecode(decryptString, Encoding.UTF8); 75 byte[] m_btDecryptString = Convert.FromBase64String(decryptString); 76 MemoryStream m_stream = new MemoryStream(); 77 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.UTF8.GetBytes(key), m_btIV), CryptoStreamMode.Write); 78 m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_csstream.FlushFinalBlock(); 79 m_strDecrypt = Encoding.UTF8.GetString(m_stream.ToArray()); 80 m_stream.Close(); m_stream.Dispose(); 81 m_csstream.Close(); m_csstream.Dispose(); 82 } 83 catch (IOException ex) { throw ex; } 84 catch (CryptographicException ex) { throw ex; } 85 catch (ArgumentException ex) { throw ex; } 86 catch (Exception ex) { throw ex; } 87 finally { m_AESProvider.Clear(); } 88 return m_strDecrypt; 89 } 90 91 /// <summary> 92 /// AES 解密 93 /// </summary> 94 /// <param name="decryptString">待解密密文</param> 95 /// <returns></returns> 96 public static string AESDecrypt(string decryptString) 97 { 98 return AESDecrypt(decryptString, EncryptKey); 99 } 100 101 /// <summary> 102 /// 随机生成密钥 103 /// </summary> 104 /// <returns></returns> 105 public static string GetIv(int n) 106 { 107 char[] arrChar = new char[] { 'a', 'b', 'd', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 'q', 's', 't', 'u', 'v', 'w', 'z', 'y', 'x', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'Q', 'P', 'R', 'T', 'S', 'V', 'U', 'W', 'X', 'Y', 'Z' }; 108 StringBuilder iv = new StringBuilder(); 109 Random rnd = new Random(DateTime.Now.Millisecond); 110 for (int i = 0; i < n; i++) 111 { 112 iv.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); 113 } 114 return iv.ToString(); 115 } 116 #endregion
本文的加密方式均修改于网络文章,出处已无法一一列举请见谅