import javax.crypto.badpaddingexception;import javax.crypto.cipher;import javax.crypto.illegalblocksizeexception;import javax.crypto.nosuchpaddingexception;import javax.crypto.spec.ivparameterspec;import javax.crypto.spec.secretkeyspec;import org.apache.commons.codec.binary.base64;import com.alibaba.fastjson.json;import com.alibaba.fastjson.jsonobject;import java.io.ioexception;import Java.io.unsupportedencodingexception;import java.net.urldecoder;import java.net.urlencoder;import java.security.invalidalgorithmparameterexception;import java.security.invalidkeyexception;import java.security.NoSuchAlgorithmException;public class AESUtil { private static final string iv_string = "SDF4DDFSFD86VDF2"; private static final String encoding = "UTF-8"; public static String encryptaes (String content, string key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, unsupportedencodingexception, invalidalgorithmparameterexception, illegalblocksizeexception, badpaddingexception { byte[] bytecontent = content.getbytes (encoding); // Note that in order to be able to work with iOS unified // here key can not use keygenerator, SecureRandom, secretkey generation byte[] encodeformat = key.getbytes (encoding); secretkeyspec secrEtkeyspec = new secretkeyspec (encodeformat, "AES"); byte[] initparam = iv_string.getbytes (encoding); ivparameterspec ivparameterspec = new ivparameterspec (InitParam); // specify cryptographic algorithms, work modes, and fill methods cipher cipher = cipher.getinstance ("aes/cbc/pkcs5padding"); cipher.init (Cipher.encrypt_mode, secretkeyspec, ivparameterspec); byte[] encryptedbytes = cipher.dofinal (byteContent); // also base64 encode the encrypted data string base64 = new base64 (). encodetostring (encryptedBytes); //URL encoding removing = ? & return Urlencoder.encode (base64,encoding); } public static String decryptaes (String content, string key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, invalidalgorithmparameterexception, illegalblocksizeexception, badpaddingexception, ioexception { //url decoding content = urldecoder.decode (content,encoding); // base64 decoding byte[] encryptedbyteS = base64.decodebase64 (content); byte[] Encodeformat = key.getbytes (encoding); secretkeyspec secretkey = new secretkeyspec (encodeformat, "AES"); byte[] initparam = iv_string.getbytes (encoding); ivparameterspec ivparameterspec = new ivparameterspec (InitParam); cipher cipher = cipher.getinstance ("AES/CBC/ Pkcs5padding "); cipher.init (Cipher.decrypt_mode, secretkey , ivparameterspec); byte[] result = Cipher.dofinal (encryptedbytes); return new string ( result, encoding); } public static void main (String[] args) throws Exception { JSONObject json = new Jsonobject (); json.put ("Custnum", "111111"); /*json.put (" Name ", " Zhang San ") json.put (" Citycode ", " 100001 "); json.put ("CityName", "Beijing"); json.put ("Mobileno", "15651876590");*/ string content = json.tojsonstring (); system.out.println ("Before encryption:" + content); String key = "DJADIKJDJ49DFJLD"; SYSTEM.OUT.PRINTLN ("Encryption key and decryption key:" + key); string Encrypt = encryptaeS (Content, key); system.out.println ("After encryption:" + encrypt); string decrypt = decryptaes (encrypt , key); system.out.println ("After decryption:" + decrypt); }}
Java implementation AES mode encryption