AES -- symmetric encryption and decryption in JAVA and aesjava encryption and decryption
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. export; import javax. crypto. badPaddingException; import javax. crypto. cipher; import javax. crypto. illegalBlockSizeException; import javax. crypto. keyGe Nerator; 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 encryption ricencoder {/** encryption * 1. construct a key generator * 2. initialize the key generator according to ecnodeRules Rules * 3. generate a key * 4. create and initialize a secret * 5. content encryption * 6. returns the */public static String AESEncode (String encodeRules, String content) {try {// 1. Construct the key generator, which is specified as the AES algorithm and is case insensitive KeyGenerator keygen = KeyGenerator. getInstance ("AES"); // 2. initialize the key generator according to ecnodeRules rules // generate a 128-bit random source based on the incoming byte array keygen. init (128, new SecureRandom (encodeRules. getBytes (); // 3. generate the original symmetric key SecretKey original_key = keygen. generateKey (); // 4. get the byte array byte [] raw = original_key.getEncoded (); // 5. generate the AES key SecretKey = new SecretKeySpec (raw, "AES") based on the byte array; // 6. self-contained Cipher cipher Cipher based on the specified AES Algorithm = Cipher. getInstance ("AES"); // 7. initialize the cipher. The first parameter is the encryption (Encrypt_mode) or decryption (Decrypt_mode) operation, and the second parameter is the KEY cipher used. init (Cipher. ENCRYPT_MODE, key); // 8. obtain the byte array of the encrypted content (set to UTF-8 here). Otherwise, if there is a mix of Chinese and English characters in the content, the decryption will be garbled byte [] byte_encode = content. getBytes ("UTF-8"); // 9. encrypt Data byte [] byte_AES = cipher. doFinal (byte_encode); // 10. convert the encrypted data to a string // The package cannot be found in Base64Encoder. // solution: // remove the JRE System Library in the Build path of the project, and then Add the JRE System Library and recompile it. String AES_encode = new String (new BASE64Encoder (). encode (byte_AES); // 11. 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 ();} // Add nulll return null if there is an error;}/** decryption * decryption process: * 1. same as step 1-4 of encryption * 2. returns the encrypted string to the byte [] array * 3. decrypt the encrypted content */public static String AESDncode (String encodeRules, String content) {try {// 1. construct the key generator, which is specified as the AES algorithm and is case insensitive KeyGenerator keygen = KeyGenerator. getInstance ("AES"); // 2. initialize the key generator according to ecnodeRules rules // generate a 128-bit random source based on the incoming byte array keygen. init (128, new SecureRandom (encodeRules. getBytes (); // 3. generate the original symmetric key SecretKey original_key = keygen. generateKey (); // 4. get the byte array byte [] raw = original_key.getEncoded (); // 5. generate the AES key SecretKey = new SecretKeySpec (raw, "AES") based on the byte array; // 6. based on the specified algorithm AES self-contained Cipher cipher Cipher = Cipher. getInstance ("AES"); // 7. initialize the cipher. The first parameter is the encryption (Encrypt_mode) or decryption (Decrypt_mode) operation, and the second parameter is the KEY cipher used. init (Cipher. DECRYPT_MODE, key); // 8. decodes 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 ();} // Add nulll return null if there is an error;} public static void main (String [] args) {response ricencoder se = new response ricencoder (); required bytes = new bytes (System. in);/** encrypted */System. out. println ("use AES symmetric encryption, enter the encryption Rules"); String encodeRules = signature. next (); System. out. println ("Enter the content to be encrypted:"); String content = encrypted. next (); System. out. println ("according to the input rules" + encodeRules + "the encrypted ciphertext is:" + se. AESEncode (encodeRules, content);/** decrypt */System. out. println ("use AES for symmetric decryption, enter the encryption rules: (must be the same as encryption)"); encodeRules = cipher. next (); System. out. println ("Enter the content to decrypt (ciphertext):"); content = ciphertext. next (); System. out. println ("according to the input rules" + encodeRules + "the decrypted plaintext is:" + se. AESDncode (encodeRules, content ));}}
[Source code download] test results:
Use AES symmetric encryption. Enter the encryption rules to use AES symmetric encryption. Enter the content to be encrypted: Use AES symmetric encryption according to the input rules. The ciphertext encrypted with AES symmetric encryption is: z0NwrNPHghgXHN0CqjLS58YCjhMcBfeR33RWs7Lw + AY = use AES symmetric decryption. Enter the encryption rules: (same as encryption) Use AES symmetric encryption. Enter the content to decrypt (ciphertext ): z0NwrNPHghgXHN0CqjLS58YCjhMcBfeR33RWs7Lw + AY = according to the input rules, use AES symmetric encryption to decrypt the decrypted plaintext: Use AES symmetric encryption