JAVA AES Plus decryption detailed

Source: Internet
Author: User
Tags base64

The last essay left a question, the two kinds of encryption results are different?

In fact, the internal implementation is not the same way, see the notes

1  /**2 * provide keys and vectors for encryption3      *4      * @paramSSRC5      * @paramKey6      * @paramIV7      * @return8      * @throwsException9      */Ten      Public StaticString Encrypt (String sSrc,byte[] Key,byte[] IV)throwsException { OneSecretkeyspec Skeyspec =NewSecretkeyspec (Key, "AES");//constructs a key based on a given byte array ACipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");//"algorithm/Mode/complement Method" -Ivparameterspec _iv =NewIvparameterspec (iv);//using CBC mode, a vector IV is required to increase the strength of the encryption algorithm -Cipher.init (Cipher.encrypt_mode, Skeyspec, _iv);//initializes this Cipher with a key and a set of algorithm parameters the         byte[] encrypted = Cipher.dofinal (Ssrc.getbytes ("Utf-8"));//encrypt or decrypt data by one-part operation, or end a multipart operation.  -         returnbase64.encodebase64string (encrypted); -}
1  /**2 * Encryption3      * @paramcontent4      * @paramkeybytes5      * @paramIV6      * @return7      * @throwsException8      */9      PublicString Aes_cbc_encrypt (byte[] content,byte[] keybytes,byte[] IV)throwsexception{Ten         Try{ OneKeygenerator keygenerator= keygenerator.getinstance ("AES");//returns the Keygenerator object that generates the secret key for the specified algorithm AKeygenerator.init (128,NewSecureRandom (keybytes));//initializes this key generator with a user-supplied random source that has a determined key size -Secretkey Key=keygenerator.generatekey ();//generates a key.  -Cipher cipher=cipher.getinstance ("aes/cbc/pkcs5padding"); theCipher.init (Cipher.encrypt_mode, Key,NewIvparameterspec (iv)); -             byte[] result=cipher.dofinal (content); -             returnbase64.encodebase64string (result); -}Catch(Exception e) { + e.printstacktrace (); -             Throwe; +         } A}

JAVA AES Plus decryption detailed

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.