AES symmetric encryption and decryption codes are introduced in detail for your reference. The specific content is as follows
package demo.security;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.util.Base64;import java.util.Scanner;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/* * AES symmetric encryption and decryption*/public class SymmetricEncoder { /* * Encryption* 1. Construct the key generator* 2. Initialize the key generator according to the ecnodeRules rule * 3. Generate the key * 4. Create and initialize the password * 5. Content encryption * 6. Return the string */ public static String AESEncode(String encodeRules,String content){ try { //1. Construct the key generator, specified as the AES algorithm, and is case-insensitive KeyGenerator keygen=KeyGenerator.getInstance("AES"); //2. Initialize the key generator according to the ecnodeRules rule // Generate a 128-bit random source, and according to the passed byte array keygen.init(128, new SecureRandom(encodeRules.getBytes())); //3. Generate the original symmetric key SecretKey original_key=keygen.generateKey(); //4. Obtain the byte array of the original symmetric key byte [] raw=original_key.getEncoded(); //5. Generate the AES key from the byte array SecretKey key=new SecretKeySpec(raw, "AES"); //6. According to the specified algorithm AES self-generated cryptor Cipher cipher=Cipher.getInstance("AES"); //7. Initialize the cryptor, the first parameter is encryption (Encrypt_mode) or decryption and decryption (Decrypt_mode) operation, and the second parameter is the KEY used cipher.init(Cipher.ENCRYPT_MODE, key); //8. Get the byte array of encrypted content (it needs to be set to utf-8 here). Otherwise, if there is a mixed Chinese and English in the content, it will be decrypted into garbled code byte [] byte_encode=content.getBytes("utf-8"); //9. According to the initialization method of the cipher--encryption: Encrypt the data byte [] byte_AES=cipher.doFinal(byte_encode); //10. Convert the encrypted data into a string//The package will not be found in Base64Encoder here//Solution: //Remove it first removes the JRE System Library in the project's Build path, and then adds the library JRE System Library, everything goes well after recompilation. String AES_encode=new String(new BASE64Encoder().encode(byte_AES)); //11. Return the string to return AES_encode; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //If there is any error, add null return null; } /* * Decrypt* Decryption process: * 1. Same encryption steps 1-4* 2. Inverse spun the encrypted string into a byte[] array* 3. Decrypt the encrypted content*/ public static String AESDncode(String encodeRules,String content){ try { //1. Construct the key generator, specify it as an AES algorithm, and is not case sensitive KeyGenerator keygen=KeyGenerator.getInstance("AES"); //2. Initialize the key generator according to the ecnodeRules rule//Generate a 128-bit random source, and according to the passed byte array keygen.init(128, new SecureRandom(encodeRules.getBytes())); //3. Generate the original symmetric key SecretKey original_key=keygen.generateKey(); //4. Obtain the byte array of the original symmetric key byte [] raw=original_key.getEncoded(); //5. Generate AES key based on the byte array SecretKey key=new SecretKeySpec(raw, "AES"); //6. According to the specified algorithm AES self-forming cipher, Cipher cipher=Cipher.getInstance("AES"); //7. Initialize the cipher, the first parameter is encryption (Encrypt_mode) or decrypt (Decrypt_mode) operation, and the second parameter is the KEY cipher.init(Cipher.DECRYPT_MODE, key); //8. Decode the encrypted and encoded content into a byte array byte [] byte_content= new BASE64Decoder().decodeBuffer(content); /* * Decrypt*/ byte [] byte_decode=cipher.doFinal(byte_content); String AES_decode=new String(byte_decode,"utf-8"); return AES_decode; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } //If there is any error, add null return null; } public static void main(String[] args) { SymmetricEncoder se=new SymmetricEncoder(); Scanner scanner=new Scanner(System.in); /* * Encryption*/ System.out.println("Use AES symmetric encryption, please enter the encryption rules"); String encodeRules=scanner.next(); System.out.println("Please enter the content to be encrypted:"); String content = scanner.next(); System.out.println("According to the input rule"+encodeRules+" the encrypted ciphertext is: "+se.AESEncode(encodeRules, content)); /* * Decrypt*/ System.out.println("Use AES symmetric decryption, please enter the encryption rules: (must be the same as encryption)"); encodeRules=scanner.next(); System.out.println("Please enter the content to be decrypted (ciphertext):"); content = scanner.next(); System.out.println("according to input rules"+encodeRules+" the plain text decrypted is: "+se.AESDncode(encodeRules, content)); }} Test results:
Use AES symmetric encryption, please enter the encryption rules. Use AES symmetric encryption. Please enter the content you want to encrypt:
Use AES symmetric encryption to encrypt the ciphertext using AES symmetric encryption according to the input rules is: Z0NwrNPHghgXHN0CqjLS58YCjhMcBfeR33RWs7Lw+AY=
Use AES symmetric decryption, please enter the encryption rules: (must be the same as encryption)
Use AES symmetric encryption to enter the content (ciphertext):
Z0NwrNPHghgXHN0CqjLS58YCjhMcBfeR33RWs7Lw+AY=
The plaintext decrypted using AES symmetric encryption according to the input rules is: Use AES symmetric encryption
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.