Using AES256 CBC encryption in Java projects

Source: Internet
Author: User

First of all, note that the default JDK does not support 256-bit encryption, you need to download the encryption enhancement file to Oracle official website (Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8), or the compilation will error:

Java.security.InvalidKeyException:Illegal Key Size

After decompression, replace the same name file under the jre/lib/security/directory.

The simplest example of application:

 Public classIotserver {Private Static Final byte[] key = {.};//Key.length must meet 16 of the integer multiples    Private Static Final byte[] IV = {..};//Iv.length must meet 16 of the integer multiples    Private Static FinalString transform = "aes/cbc/pkcs5padding"; Private Static FinalString algorithm = "AES"; Private Static FinalSecretkeyspec KeySpec =NewSecretkeyspec (key, algorithm);  Public Static voidMain (string[] args) {Cipher Cipher=cipher.getinstance (transform); Cipher.init (Cipher.encrypt_mode, KeySpec,NewIvparameterspec (iv)); byte[] CipherData = cipher.dofinal ("PlainText to be encrypted". GetBytes ("UTF-8"));    System.out.println (arrays.tostring (CipherData)); }}

Both key and IV can be generated in more complex ways, many of which are no longer listed, and more usage techniques will be found in practical applications.

Using AES256 CBC encryption in Java projects

Related Article

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.