Java implementation of AES encryption and decryption algorithm

Source: Internet
Author: User

Implementation of AES encryption and decryption algorithm in original Java

ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/*** AES is a reversible encryption algorithm, the user's sensitive information encryption processing * to the original data AES encryption, in the Base64 encoding conversion;*/ Public classAesoperator {/** Encryption key can be composed of 26 letters and numbers * Here use AES-128-CBC encryption mode, key needs to be 16 bits. */    PrivateString SKey = "0123456789abcdef"; PrivateString ivparameter = "0123456789abcdef"; Private StaticAesoperator instance =NULL; PrivateAesoperator () {} Public Staticaesoperator getinstance () {if(Instance = =NULL) Instance=NewAesoperator (); returninstance; }    //Encrypt     PublicString Encrypt (String sSrc)throwsException {Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); byte[] Raw =skey.getbytes (); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Ivparameterspec IV=NewIvparameterspec (Ivparameter.getbytes ());//using CBC mode, a vector IV is required to increase the strength of the encryption algorithmCipher.init (Cipher.encrypt_mode, Skeyspec, iv); byte[] encrypted = Cipher.dofinal (Ssrc.getbytes ("Utf-8")); return NewBase64encoder (). Encode (encrypted);//transcoding is done here using BASE64.     }    //decryption     PublicString Decrypt (String sSrc)throwsException {Try {            byte[] raw = Skey.getbytes ("ASCII"); Secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("aes/cbc/pkcs5padding"); Ivparameterspec IV=NewIvparameterspec (Ivparameter.getbytes ());            Cipher.init (Cipher.decrypt_mode, Skeyspec, iv); byte[] encrypted1 =NewBase64decoder (). Decodebuffer (SSRC);//first decrypt with Base64 .            byte[] Original =cipher.dofinal (encrypted1); String originalstring=NewString (Original, "Utf-8"); returnoriginalstring; } Catch(Exception ex) {return NULL; }    }     Public Static voidMain (string[] args)throwsException {//strings that need to be encryptedString CSRC = "I'm from Www.wenhq.com"; System.out.println (CSRC);//Encrypt        LongLstart =System.currenttimemillis (); String enstring=aesoperator.getinstance (). Encrypt (CSRC); System.out.println ("The encrypted string is:" +enstring); LongLusetime = System.currenttimemillis ()-Lstart; System.out.println ("Encryption Time:" + Lusetime + "milliseconds");//decryptionLstart =System.currenttimemillis (); String destring=aesoperator.getinstance (). Decrypt (enstring); System.out.println ("The decrypted string is:" +destring); Lusetime= System.currenttimemillis ()-Lstart; System.out.println ("Decryption Time:" + Lusetime + "milliseconds"); }}

Java implementation of AES encryption and decryption algorithm

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.