using log4net; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; namespace CnasSynchronousCommon { public class EncryptionOperation { #region MD5加密 /// /// MD5加密 /// /// /// public static string GetMD5Hash(String input) { string cl = input; string pwd = ""; MD5 md5 = MD5.Create();//实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择  byte[] s = md5.ComputeHash(Encoding.Default.GetBytes(cl)); // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 for (int i = 0; i < s.Length; i++) { // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 pwd = pwd + s[i].ToString("X"); } return pwd; } #endregion // 对称加密算法提供器 private ICryptoTransform encryptor; // 加密器对象 private ICryptoTransform decryptor; // 解密器对象 private const int BufferSize = 1024; public EncryptionOperation(string algorithmName, string key) { try { SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName); provider.Key = Encoding.UTF8.GetBytes(key); provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; encryptor = provider.CreateEncryptor(); decryptor = provider.CreateDecryptor(); } catch (Exception ex) { string s = ex.Message.ToString(); } } public EncryptionOperation(string key) : this("TripleDES", key) { } // 加密算法 public string Encrypt(string clearText) { try { // 创建明文流 byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText); MemoryStream clearStream = new MemoryStream(clearBuffer); // 创建空的密文流 MemoryStream encryptedStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write); // 将明文流写入到buffer中 // 将buffer中的数据写入到cryptoStream中 int bytesRead = 0; byte[] buffer = new byte[BufferSize]; do { bytesRead = clearStream.Read(buffer, 0, BufferSize); cryptoStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); cryptoStream.FlushFinalBlock(); // 获取加密后的文本 buffer = encryptedStream.ToArray(); string encryptedText = Convert.ToBase64String(buffer); return encryptedText; } catch (Exception ex) { AppLog.Error(ex.Message); return ""; } } // 解密算法 public string Decrypt(string encryptedText) { try { byte[] encryptedBuffer = Convert.FromBase64String(encryptedText); Stream encryptedStream = new MemoryStream(encryptedBuffer); MemoryStream clearStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read); int bytesRead = 0; byte[] buffer = new byte[BufferSize]; do { bytesRead = cryptoStream.Read(buffer, 0, BufferSize); clearStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); buffer = clearStream.GetBuffer(); string clearText = Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length); return clearText; } catch (Exception ex) { AppLog.Error(ex.Message); return ""; } } public static string Encrypt(string clearText, string key) { EncryptionOperation helper = new EncryptionOperation(key); return helper.Encrypt(clearText); //return clearText; } public static string Decrypt(string encryptedText, string key) { EncryptionOperation helper = new EncryptionOperation(key); return helper.Decrypt(encryptedText); //return encryptedText; } } }