This example describes the AES encryption method in Java. Share to everyone for your reference. Specifically as follows:
Java code:
keygenerator kg = keygenerator.getinstance ("AES"); Gets the key generator
kg.init (256);//
//des algorithm must be 56-bit
//desede algorithm can be 112-bit or 168-bit
//aes algorithm can be 128, 192, 256 bit
Secretkey key = Kg.generatekey (); To generate a key, you can save the key in several ways
Encryption:
Cipher CP = Cipher.getinstance ("AES"); Create the cipher
cp.init (Cipher.encrypt_mode, key);//Initialize
String str = "I am required to be encrypted plaintext";
byte [] Ptext = Str.getbytes ("UTF8");
byte [] Ctext = Cp.dofinal (Ptext); Encryption
Decrypt:
Cipher CP = Cipher.getinstance ("AES"); Create cipher
cp.init (Cipher.decrypt_mode, key);//Initialize
byte [] ptext = Cp.dofinal (Ctext);//decrypt
String str = new String (Ptext, "UTF8"); Re-display Clear text
I hope this article will help you with your Java programming.