這年頭找個正經能用的東西那是真難,網上一搜索一大堆,正經能用的沒幾個,得,最後仍是得靠本身,正巧趕上須要AES加解密的地方了,並且仍是Java和C#間的相互加解密操做,這裏作個備忘java
這裏採用的加解密使用base64轉碼方法,ECB模式,PKCS5Padding填充,密碼必須是16位,不然會報錯哈dom
模式:Java的ECB對應C#的System.Security.Cryptography.CipherMode.ECB編碼
填充方法:Java的PKCS5Padding對應C#System.Security.Cryptography.PaddingMode.PKCS7加密
Java和C#版的加解密是互通的,也就是能相互加解密,編碼明確指定了採用UTF-8,有須要其餘編碼方法的請自行擴展spa
Java版rest
package nb.tmall.util; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import sun.misc.*; @SuppressWarnings("restriction") public class EncryptUtil { public static String aesEncrypt(String str, String key) throws Exception { if (str == null || key == null) return null; Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES")); byte[] bytes = cipher.doFinal(str.getBytes("utf-8")); return new BASE64Encoder().encode(bytes); } public static String aesDecrypt(String str, String key) throws Exception { if (str == null || key == null) return null; Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES")); byte[] bytes = new BASE64Decoder().decodeBuffer(str); bytes = cipher.doFinal(bytes); return new String(bytes, "utf-8"); } }
C#版code
using System; using System.Security.Cryptography; using System.Text; namespace CSharp.Util.Security { /// <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); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.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); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } } }