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