日期:2014-05-17 浏览次数:20467 次
private static readonly String strAesKey = "iwww.maoblog.comi123.maoblog.com";//加密所需32位密匙 /// <summary> /// AES加密 /// </summary> /// <param name="str">要加密字符串</param> /// <returns>返回加密后字符串</returns> public static String Encrypt_AES(String str) { Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey); Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str); System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.ECB; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> /// <param name="str">要解密字符串</param> /// <returns>返回解密后字符串</returns> public static String Decrypt_AES(String str) { Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey); Byte[] toEncryptArray = Convert.FromBase64String(str); System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.ECB; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return System.Text.UTF8Encoding.UTF8.GetString(resultArray); }
byte[] keyArray = null; using (System.Security.Cryptography.MD5CryptoServiceProvider m = new System.Security.Cryptography.MD5CryptoServiceProvider()) { keyArray = m.ComputeHash(Texts.MyEncoder.GetBytes(strAesKey)); }
------解决方案--------------------
参考:http://www.google.com.hk/search?q=Rfc2898DeriveBytes&hl=zh-CN&newwindow=1&safe=strict&rls=com.microsoft:en-US:IE-Address&prmd=imvns&source=lnt&tbs=lr:lang_1zh-CN%7Clang_1zh-TW&lr=lang_zh-CN%7Clang_zh-TW&sa=X&ei=ryUVUNzLDaWPiAfH9IDoCw&ved=0CFEQpwUoAQ&biw=1440&bih=815