一、C#版AES加解密算法
public class AESCode { public string Key { get; set; } public string Encrypt(string val) { if (string.IsNullOrEmpty(val)) return null; #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] inputByteArray = Encoding.UTF8.GetBytes(val); byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); byte[] bytes = (byte[])ms.ToArray(); return Convert.ToBase64String(bytes); } } } } public string Decrypt(string val) { if (string.IsNullOrEmpty(val)) return null; #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] inputByteArray = Convert.FromBase64String(val); byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.UTF8.GetString(ms.ToArray()); } } } } public void GeneralKeyIV(string keyStr, out byte[] key, out byte[] iv) { byte[] bytes = Encoding.UTF8.GetBytes(keyStr); key = SHA256Managed.Create().ComputeHash(bytes); iv = MD5.Create().ComputeHash(bytes); } }
二、Java版算法
import java.security.MessageDigest; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class AESCode { /** * 提供密鑰和向量進行加密 * * @param sSrc * @param key * @param iv * @return * @throws Exception */ public static String Encrypt(String sSrc, byte[] key, byte[] iv) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/補碼方式" IvParameterSpec _iv = new IvParameterSpec(iv);// 使用CBC模式,需要一個向量iv,可增加加密算法的強度 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv); byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); return Base64.encodeBase64String(encrypted); } /** * 提供密鑰和向量進行解密 * * @param sSrc * @param key * @param iv * @return * @throws Exception */ public static String Decrypt(String sSrc, byte[] key, byte[] iv) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec _iv = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, skeySpec, _iv); byte[] encrypted = Base64.decodeBase64(sSrc); byte[] original = cipher.doFinal(encrypted); return new String(original, "utf-8"); } /** * 使用密鑰進行加密 * * @param sSrc * @param keyStr * @return * @throws Exception */ public static String Encrypt(String sSrc, String keyStr) throws Exception { byte[] key = GeneralKey(keyStr); byte[] iv = GeneralIv(keyStr); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/補碼方式" IvParameterSpec _iv = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv); byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); return Base64.encodeBase64String(encrypted); } /** * 使用密鑰進行解密 * * @param sSrc * @param keyStr * @return * @throws Exception */ public static String Decrypt(String sSrc, String keyStr) throws Exception { byte[] key = GeneralKey(keyStr); byte[] iv = GeneralIv(keyStr); SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec _iv = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, skeySpec, _iv); byte[] encrypted = Base64.decodeBase64(sSrc);// 先用base64解碼 byte[] original = cipher.doFinal(encrypted); return new String(original, "utf-8"); } /** * 構建密鑰字節碼 * * @param keyStr * @return * @throws Exception */ private static byte[] GeneralKey(String keyStr) throws Exception { byte[] bytes = keyStr.getBytes("utf-8"); MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(bytes); return md.digest(); } /** * 構建加解密向量字節碼 * * @param keyStr * @return * @throws Exception */ private static byte[] GeneralIv(String keyStr) throws Exception { byte[] bytes = keyStr.getBytes("utf-8"); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes); return md.digest(); } }
java版需要commons-codec-1.10.jar,local_policy.jar,US_export_policy.jar