Java implementation AES mode encryption

Source: Internet
Author: User
Tags base64 decrypt

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.