[C#]加密解密 MD5、AES
/// <summary> /// MD5函数 /// </summary> /// <param name="str">原始字符串</param> /// <returns>MD5结果</returns> public static string MD5(string str) { byte[] b = Encoding.UTF8.GetBytes(str); b = new MD5CryptoServiceProvider().ComputeHash(b); string ret = string.Empty; for (int i = 0; i < b.Length; i++) { ret += b[i].ToString("x").PadLeft(2, '0'); } return ret; } /// <summary> /// AES加密 /// </summary> /// <param name="toEncrypt"></param> /// <returns></returns> public static string AesEncrypt(string toEncrypt) { if (string.IsNullOrEmpty(toEncrypt)) { return string.Empty; } try { byte[] keyArray = Encoding.UTF8.GetBytes(@"16位"); byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } catch { return string.Empty; } } /// <summary> /// AES解密 /// </summary> /// <param name="toDecrypt"></param> /// <returns></returns> public static string AesDecrypt(string toDecrypt) { if (string.IsNullOrEmpty(toDecrypt)) { return string.Empty; } try { byte[] keyArray = Encoding.UTF8.GetBytes(@"16位"); byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } catch { return string.Empty; } }
// AES 加密 private byte[] AESEncrypt(string text) { string Key = "123456"; var IV = Encoding.Default.GetBytes(Key); byte[] data = Encoding.Default.GetBytes(text); SymmetricAlgorithm aes = Rijndael.Create(); aes.Key = IV; aes.IV = IV; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; var encryptor = aes.CreateEncryptor(); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); byte[] cipherBytes = ms.ToArray(); // 得到加密后的字节数组 cs.Close(); ms.Close(); aes.Clear(); return cipherBytes; } } } // AES 解密 public string AESDecrypt(byte[] data) { string Key = "123456"; var IV = Encoding.Default.GetBytes(Key); SymmetricAlgorithm aes = Rijndael.Create(); aes.Key = IV; aes.IV = IV; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; var encryptor = aes.CreateDecryptor(); byte[] decryptBytes = new byte[data.Length]; using (MemoryStream ms = new MemoryStream(data)) { using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Read)) { cs.Read(decryptBytes, 0, decryptBytes.Length); cs.Close(); ms.Close(); } } aes.Clear(); return Encoding.Default.GetString(decryptBytes).Replace("\0", " "); }