AES加密解密通用版Object-C / C# / JAVA
Posted 落叶的瞬间
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AES加密解密通用版Object-C / C# / JAVA相关的知识,希望对你有一定的参考价值。
1.无向量 128位
/// <summary> /// AES加密(无向量) /// </summary> /// <param name="plainBytes">被加密的明文</param> /// <param name="key">密钥</param> /// <returns>密文</returns> public static string AESEncrypt(String Data, String Key) { MemoryStream mStream = new MemoryStream(); RijndaelManaged aes = new RijndaelManaged(); byte[] plainBytes = Encoding.UTF8.GetBytes(Data); Byte[] bKey = new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.PKCS7; aes.KeySize = 128; //aes.Key = _key; aes.Key = bKey; //aes.IV = _iV; CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write); try { cryptoStream.Write(plainBytes, 0, plainBytes.Length); cryptoStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } finally { cryptoStream.Close(); mStream.Close(); aes.Clear(); } }
/// AES解密(无向量) /// </summary> /// <param name="encryptedBytes">被加密的明文</param> /// <param name="key">密钥</param> /// <returns>明文</returns> public static string AESDecrypt(String Data, String Key) { Byte[] encryptedBytes = Convert.FromBase64String(Data); Byte[] bKey = new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); MemoryStream mStream = new MemoryStream(encryptedBytes); //mStream.Write( encryptedBytes, 0, encryptedBytes.Length ); //mStream.Seek( 0, SeekOrigin.Begin ); RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.PKCS7; aes.KeySize = 128; aes.Key = bKey; //aes.IV = _iV; CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read); try { byte[] tmp = new byte[encryptedBytes.Length + 32]; int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32); byte[] ret = new byte[len]; Array.Copy(tmp, 0, ret, 0, len); return Encoding.UTF8.GetString(ret); } finally { cryptoStream.Close(); mStream.Close(); aes.Clear(); } }
2. 256位 通用版 JAVA / C# / Object-C
/// <summary> /// AES 加密 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string AesEncrypt(string str, string key) { if (string.IsNullOrEmpty(str)) return null; Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); RijndaelManaged rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform cTransform = rm.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); }
/// <summary> /// AES 解密 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string AesDecrypt(string str, string key) { if (string.IsNullOrEmpty(str)) return null; Byte[] toEncryptArray = Convert.FromBase64String(str); RijndaelManaged rm = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 }; ICryptoTransform cTransform = rm.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); }
以上是关于AES加密解密通用版Object-C / C# / JAVA的主要内容,如果未能解决你的问题,请参考以下文章