C # the AES encryption/decryption algorithm in java,
I. C # AES encryption and decryption algorithm
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); }}
Ii. Java Algorithm
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 {/*** provides encryption for keys and vectors ** @ 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"); // "algorithm/mode/complement mode" IvParameterSpec _ iv = new IvParameterSpec (iv); // use the CBC mode, A vector iv is required to increase the strength of the encryption algorithm cipher. init (Cipher. ENCRYPT_MODE, skeySpec, _ iv); byte [] encrypted = cipher. doFinal (sSrc. getBytes ("UTF-8"); return Base64.encodeBase64String (encrypted );} /*** provide keys and vectors for decryption ** @ 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 ");} /*** use the key for encryption ** @ 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"); // "algorithm/mode/complement mode" IvParameterSpec _ iv = new IvParameterSpec (iv); cipher. init (Cipher. ENCRYPT_MODE, skeySpec, _ iv); byte [] encrypted = cipher. doFinal (sSrc. getBytes ("UTF-8"); return Base64.encodeBase64String (encrypted );} /*** use the key for decryption ** @ 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); // decode byte [] original = cipher With base64. doFinal (encrypted); return new String (original, "UTF-8 ");} /*** construct the key bytecode ** @ 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 ();}/*** construct the encryption/Decryption vector bytecode ** @ 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 requires commons-codec-1.10.jar, local_policy.jar, US_export_policy.jar