/// <summary> /// AES加密 /// </summary> /// <param>要加密的内容</param> /// <param>密钥(16或者32位)</param> /// <returns>Base64转码后的密文</returns> public static string Encrypt(string toEncrypt, string strKey) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); RijndaelManaged rDel = new RijndaelManaged();//using System.Security.Cryptography; rDel.Key = keyArray; rDel.Mode = CipherMode.ECB;//using System.Security.Cryptography; rDel.Padding = PaddingMode.PKCS7;//using System.Security.Cryptography; ICryptoTransform cTransform = rDel.CreateEncryptor();//using System.Security.Cryptography; byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> /// <param>要解密的内容</param> /// <param>密钥(16或者32位)</param> /// <returns>解密后的明文</returns> public static string Decrypt(string toDecrypt, string strKey) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey); 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); } #endregion AES解密 }
DES加密解密与AES加密解密(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/975c5991090b1e14ad3a54b2a5cc3406.html