Package ***;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 * The original data AES encryption, in the BASE64 encoding conversion; * Correct*/ Public classAESCBC {/*confirmed * Encryption key can be composed of 26 letters and numbers * Here using AES-128-CBC encryption mode, key needs to be 16 bits. */ Private StaticString skey= "1234567890123456"; Private StaticString ivparameter= "1234567890123456"; Private StaticAESCBC instance=NULL; //private static PrivateAESCBC () {} Public StaticAESCBC getinstance () {if(instance==NULL) Instance=NewAESCBC (); returninstance; } //Encrypt PublicString Encrypt (String sSrc, String Encodingformat, String SKey, String ivparameter)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 (Encodingformat)); return NewBase64encoder (). Encode (encrypted);//transcoding is done here using BASE64. } //decryption PublicString Decrypt (String sSrc, String Encodingformat, String SKey, String ivparameter)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,encodingformat); returnoriginalstring; } Catch(Exception ex) {return NULL; }} Public Static voidMain (string[] args)throwsException {//strings that need to be encryptedString CSRC = "123456"; System.out.println ("The string before encryption is:" +CSRC); //EncryptString enstring = Aescbc.getinstance (). Encrypt (CSRC, "Utf-8", Skey,ivparameter); System.out.println ("The encrypted string is:" +enstring); System.out.println ("1jdzwunig6umtoa3t6unla==". Equals (enstring)); //decryptionString destring = Aescbc.getinstance (). Decrypt (enstring, "Utf-8", Skey,ivparameter); System.out.println ("The decrypted string is:" +destring); }}
Java Cryptographic algorithm series-AESCBC