An example analysis of AES encryption method in Java _java

Source: Internet
Author: User
Tags decrypt

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.

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.