Here the use of encryption and decryption using the Base64 transcoding method, ECB mode, pkcs5padding fill, password must be 16 bits, otherwise it will error!
Mode: The ECB of Java corresponds to C # System.Security.Cryptography.CipherMode.ECB
Fill method: Java pkcs5padding corresponds to C#SYSTEM.SECURITY.CRYPTOGRAPHY.PADDINGMODE.PKCS7
Java and C # version of the encryption and decryption is interoperability, that is, can be decrypted with each other, the code explicitly specifies the use of UTF-8, there is need for other coding methods, please expand themselves
Java Edition
PackageNb.tmall.util;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;ImportJavax.crypto.*;ImportJavax.crypto.spec.SecretKeySpec;ImportSun.misc.*; @SuppressWarnings ("Restriction") Public classEncryptutil { Public StaticString Aesencrypt (String str, string key)throwsException {if(str = =NULL|| Key = =NULL)return NULL; Cipher Cipher= Cipher.getinstance ("aes/ecb/pkcs5padding"); Cipher.init (Cipher.encrypt_mode,NewSecretkeyspec (Key.getbytes ("Utf-8"), "AES")); byte[] bytes = cipher.dofinal (str.getbytes ("Utf-8")); return NewBase64encoder (). Encode (bytes); } Public StaticString Aesdecrypt (String str, string key)throwsException {if(str = =NULL|| Key = =NULL)return NULL; Cipher Cipher= Cipher.getinstance ("aes/ecb/pkcs5padding"); Cipher.init (Cipher.decrypt_mode,NewSecretkeyspec (Key.getbytes ("Utf-8"), "AES")); byte[] bytes =NewBase64decoder (). Decodebuffer (str); Bytes=cipher.dofinal (bytes); return NewString (Bytes, "Utf-8"); }}
C # Edition
usingSystem;usingSystem.Security.Cryptography;usingSystem.Text;namespacecsharp.util.security{/// <summary> ///AES Encryption/// </summary> /// <param name= "str" ></param> /// <param name= "key" ></param> /// <returns></returns> Public Static stringAesencrypt (stringStrstringkey) { if(string. IsNullOrEmpty (str))return NULL; Byte[] Toencryptarray=Encoding.UTF8.GetBytes (str); System.Security.Cryptography.RijndaelManaged RM=NewSystem.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); returnConvert.tobase64string (Resultarray,0, resultarray.length); } /// <summary> ///AES Decryption/// </summary> /// <param name= "str" ></param> /// <param name= "key" ></param> /// <returns></returns> Public Static stringAesdecrypt (stringStrstringkey) { if(string. IsNullOrEmpty (str))return NULL; Byte[] Toencryptarray=convert.frombase64string (str); System.Security.Cryptography.RijndaelManaged RM=NewSystem.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); returnEncoding.UTF8.GetString (resultarray); } }}
Java, C # bilingual version of AES Plus decryption example