AES算法
1 public static string Encrypt(string key, string clearText) 2 { 3 byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); 4 using (Aes encryptor = Aes.Create()) 5 { 6 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64 }); 7 encryptor.Key = pdb.GetBytes(32); 8 encryptor.IV = pdb.GetBytes(16); 9 using (MemoryStream ms = new MemoryStream()) 10 { 11 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) 12 { 13 cs.Write(clearBytes, 0, clearBytes.Length); 14 cs.Close(); 15 } 16 clearText = Convert.ToBase64String(ms.ToArray()); 17 } 18 } 19 return clearText; 20 } 21 public static string Decrypt(string key, string cipherText) 22 { 23 cipherText = cipherText.Replace(" ", "+"); 24 byte[] cipherBytes = Convert.FromBase64String(cipherText); 25 using (Aes encryptor = Aes.Create()) 26 { 27 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64 }); 28 encryptor.Key = pdb.GetBytes(32); 29 encryptor.IV = pdb.GetBytes(16); 30 using (MemoryStream ms = new MemoryStream()) 31 { 32 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) 33 { 34 cs.Write(cipherBytes, 0, cipherBytes.Length); 35 cs.Close(); 36 } 37 cipherText = Encoding.Unicode.GetString(ms.ToArray()); 38 } 39 } 40 return cipherText; 41 }
DES算法
1 /// <summary> 2 /// DES加密字符串 3 /// </summary> 4 /// <param name="encryptString">待加密的字符串 5 /// <param name="encryptKey">加密密钥,要求为8位 6 /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> 7 public static string EncryptDES(string encryptString, string encryptKey) 8 { 9 try 10 { 11 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); 12 byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 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">待解密的字符串 31 /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同 32 /// <returns>解密成功返回解密后的字符串,失败返源串</returns> 33 public static string DecryptDES(string decryptString, string decryptKey) 34 { 35 try 36 { 37 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); 38 byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 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 /// <summary> 2 /// 修改Name字段内容为加密后的值,如果thatName不为空,则修改后的值保存到thatName字段,否则存到本字段。 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <param name="obj">数据源</param> 6 /// <param name="Name">需要加密的字段 如果为非string类型,则必须有thatName字段,且thatName字段必须为string类型</param> 7 /// <param name="thatName">存储加密后值的字段 必须为string</param> 8 /// <param name="key">加密用到的key</param> 9 /// <returns></returns> 10 public static T EncryptModelWord<T>(T obj, string Name, string thatName, string key) 11 { 12 if (obj == null) 13 { 14 return obj; 15 } 16 PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 17 int length = myPropertyInfo.Length; 18 var thisVal = string.Empty; 19 for (int i = 0; i < length; i++) 20 { 21 22 PropertyInfo pi = myPropertyInfo[i]; 23 if (pi.Name == Name) 24 { 25 // 获取值 26 thisVal = pi.GetValue(obj, null) + ""; 27 } 28 // 如果获取值不为空且当前name和thatname相同 29 if (!string.IsNullOrEmpty(thisVal) && pi.Name == thatName) 30 { 31 pi.SetValue(obj, EncryptionHelper.Encrypt(key, thisVal), null); 32 thisVal = string.Empty; 33 break; 34 } 35 else 36 // 循环到最后一个,并且获取值不为空 37 if (i == length - 1 && !string.IsNullOrEmpty(thisVal)) 38 { 39 // 第一种情况,thatname有值,再循环一遍找到thatname把加密后的值给thatname 40 if (!string.IsNullOrEmpty(thatName)) 41 { 42 for (int j = 0; j < length; j++) 43 { 44 pi = myPropertyInfo[j]; 45 if (pi.Name == thatName) 46 { 47 // 加密然后把加密的值给 48 pi.SetValue(obj, EncryptionHelper.Encrypt(key, thisVal), null); 49 thisVal = string.Empty; 50 break; 51 52 } 53 } 54 55 } 56 else 57 { 58 // 第二种情况,thatname没值,把加密后的值给当前name 59 for (int j = 0; j < length; j++) 60 { 61 pi = myPropertyInfo[j]; 62 if (pi.Name == Name) 63 { 64 65 // 加密然后把加密的值给 66 pi.SetValue(obj, EncryptionHelper.Encrypt(key, thisVal), null); 67 thisVal = string.Empty; 68 break; 69 70 } 71 } 72 } 73 74 } 75 } 76 return obj; 77 }
1 /// <summary> 2 /// 修改Name字段内容为加密后的值,如果thatName不为空,则修改后的值保存到thatName字段,否则存到本字段。 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <param name="obj">数据源</param> 6 /// <param name="Name">需要加密的字段 如果为非string类型,则必须有thatName字段,且thatName字段必须为string类型</param> 7 /// <param name="thatName">存储加密后值的字段 必须为string</param> 8 /// <param name="key">加密用到的key</param> 9 /// <returns></returns> 10 public static IList<T> EncryptListWord<T>(IList<T> obj, string Name, string thatName, string key) 11 { 12 13 foreach (T t in obj) 14 { 15 EncryptModelWord<T>(t, Name, thatName, key); 16 } 17 return obj; 18 }