C#与java中的AES加解密互解算法
一、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