Keygenerator kg = keygenerator. getinstance ("AES"); // get the key generator
Kg. INIT (256); // Initialization
// The DES algorithm must be 56 bits
// The desede algorithm can be 112-bit or 168-bit.
// The AES algorithm can be 128, 192, or 256 bits.
Secretkey key = kg. generatekey (); // you can use multiple methods to save the key.
Encryption:
Cipher CP = cipher. getinstance ("AES"); // create a secret
CP. INIT (Cipher. encrypt_mode, key); // Initialization
String STR = "I am the plaintext to be encrypted ";
Byte [] ptext = Str. getbytes ("utf8 ");
Byte [] ctext = CP. dofinal (ptext); // Encryption
// Multiple storage methods are available.
Decryption:
Cipher CP = cipher. getinstance ("AES"); // create a secret
CP. INIT (Cipher. decrypt_mode, key); // Initialization
Byte [] ptext = CP. dofinal (ctext); // decrypt
String STR = new string (ptext, "utf8"); // re-display the plaintext
Author dipper <reprinted by the author and source retained>
Blog http://blog.csdn.net/mq612