分享一个加密类和一个解密类
加密类
1 internal class CryptInfo 2 { 3 /// <summary> 4 /// 私有构造方法禁止实例化该类型 5 /// </summary> 6 public CryptInfo() { } 7 8 /// <summary> 9 /// 对输入的字符串进行加密,并获取加密后的字符串,调用这个方法 10 /// </summary> 11 /// <param name="text">输入的字符串</param> 12 /// <returns></returns> 13 public string GetEncrypt(string text) 14 { 15 return this.Encrypt(text); 16 } 17 18 /// <summary> 19 /// 加密 对用户名和密码进行加密的方法 20 /// </summary> 21 /// <param name="text"></param> 22 /// <returns></returns> 23 private string Encrypt(string text) 24 { 25 Rijndael crypt = Rijndael.Create(); 26 byte[] key = new byte[32] { 0XA6, 0X7D, 0XE1, 0X3F, 0X35, 0X0E, 0XE1, 0XA9, 0X83, 0XA5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0X98, 0XA7, 0X33, 0X49, 0XFF, 0XE6, 0XAE, 0XBF, 0X8D, 0X8D, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40 }; 27 28 byte[] iv = new byte[16] { 0XF8, 0X8B, 0X01, 0XFB, 0X08, 0X85, 0X9A, 0XA4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0XF6, 0X19 }; 29 crypt.Key = key; 30 crypt.IV = iv; 31 32 MemoryStream ms = new MemoryStream(); 33 34 ICryptoTransform transtormEncode = new ToBase64Transform(); 35 //Base64编码 36 CryptoStream csEncode = new CryptoStream(ms, transtormEncode, CryptoStreamMode.Write); 37 38 CryptoStream csEncrypt = new CryptoStream(csEncode, crypt.CreateEncryptor(), CryptoStreamMode.Write); 39 40 System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); 41 byte[] rawData = enc.GetBytes(text); 42 43 csEncrypt.Write(rawData, 0, rawData.Length); 44 csEncrypt.FlushFinalBlock(); 45 46 byte[] encryptedData = new byte[ms.Length]; 47 ms.Position = 0; 48 ms.Read(encryptedData, 0, (int)ms.Length); 49 50 return enc.GetString(encryptedData); 51 } 52 53 }
解密类:
internal class DecryptInfo { /// <summary> /// 私有化构造函数不允许外部实例化 /// </summary> public DecryptInfo() { } /// <summary> /// 对输入的字符串进行加密,并获取解密后的字符串,调用这个方法 /// </summary> /// <param name="text">输入的字符串</param> /// <returns></returns> public string GetDecrypte(string text) { return this.Decrypt(text); } /// <summary> /// 解密方法 对用户名和密码进行解密 /// </summary> /// <param name="text"></param> /// <returns></returns> private string Decrypt(string text) { Rijndael crypt = Rijndael.Create(); byte[] key = new byte[32] { 0XA6, 0X7D, 0XE1, 0X3F, 0X35, 0X0E, 0XE1, 0XA9, 0X83, 0XA5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0X98, 0XA7, 0X33, 0X49, 0XFF, 0XE6, 0XAE, 0XBF, 0X8D, 0X8D, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40 }; byte[] iv = new byte[16] { 0XF8, 0X8B, 0X01, 0XFB, 0X08, 0X85, 0X9A, 0XA4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0XF6, 0X19 }; crypt.Key = key; crypt.IV = iv; MemoryStream ms = new MemoryStream(); CryptoStream csDecrypt = new CryptoStream(ms, crypt.CreateDecryptor(), CryptoStreamMode.Write); ICryptoTransform transformDecode = new FromBase64Transform(); CryptoStream csDecode = new CryptoStream(csDecrypt, transformDecode, CryptoStreamMode.Write); System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); byte[] rawData = enc.GetBytes(text); csDecode.Write(rawData, 0, rawData.Length); csDecode.FlushFinalBlock(); byte[] decryptedData = new byte[ms.Length]; ms.Position = 0; ms.Read(decryptedData, 0, (int)ms.Length); return (enc.GetString(decryptedData)); } }
把其中的UserInfo类换成T,这样就把加密类和解密类变成泛型类,更通用。