This article describes the encryption and decryption functions implemented by Java based on AES symmetric encryption algorithm. Share it for your reference, as follows:
package com.soufun.com;import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.util.Date;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;/** * @author WHD */public class AesUtil { private static final String AES="AES"; private static final String UTF8="UTF-8"; static KeyGenerator kgen =null; static{ try { kgen= KeyGenerator.getInstance(AES); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * @param content: * @param password: */ private static byte[] encrypt(String content, String password) { try { // Use static code blocks to generate KeyGenerator object//KeyGenerator kgen = KeyGenerator.getInstance(AES); // Use 128-bit kgen.init(128, new SecureRandom(password.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] encodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // The Cipher object actually completes the encryption operation Cipher cipher = Cipher.getInstance(AES); // Encrypt the content for encoding byte[] byteContent = content.getBytes(UTF8); // Initialize the Cipher object cipher.init(Cipher.ENCRYPT_MODE, key); // Execute the encryption operation byte[] result = cipher.doFinal(byteContent); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } /* * @param content: * @param password: */ private static byte[] decrypt(byte[] content, String password) { try {// Use static code blocks to generate KeyGenerator object//KeyGenerator kgen = KeyGenerator.getInstance(AES); // Use 128-bit kgen.init(128, new SecureRandom(password.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] encodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // The Cipher object actually completes the encryption operation Cipher cipher = Cipher.getInstance(AES); // Initialize the Cipher object with a key cipher.init(Cipher.DECRYPT_MODE, key); // Formal execution of the decryption operation byte[] result = cipher.doFinal(content); return result; } 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(); } return null; } /** * Binary--》Hex conversion* @param buf * @return */ private static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * Hex--》Binary conversion* @param hexStr * @return */ private static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) { return null; } byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } public static void main(String[] args) throws UnsupportedEncodingException { long begin=new Date().getTime(); String content = "aaades encryption test"; String password = "12345678dd"; // Encrypt System.out.println("Before encryption: " + content); byte[] encryptResult = encrypt(content, password); String encryptResultStr = parseByte2HexStr(encryptResult); System.out.println("After encrypt: " + encryptResultStr); // Decrypt byte[] decryptFrom = parseHexStr2Byte(encryptResultStr); byte[] decryptResult = decrypt(decryptFrom, password); // Decrypt content for decoding String result = new String(decryptResult, UTF8); System.out.println("After decrypting: " + result); long end= new Date().getTime(); System.out.println(end-begin); }}Note: SecureRandom generates a safe random number sequence, password.getBytes() is a seed. As long as the seed is the same, the sequence is the same. Therefore, as long as there is password, the decryption can be done, and this sequence can be restored.
PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
Password security online detection:
http://tools.VeVB.COM/password/my_password_safe
High-strength password generator:
http://tools.VeVB.COM/password/CreateStrongPassword
Thunder, Express, and Tornado URL encryption/decryption tools:
http://tools.VeVB.COM/password/urlrethunder
Online hash/hash algorithm encryption tool:
http://tools.VeVB.COM/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools.VeVB.COM/password/sha_encode
I hope this article will be helpful to everyone's Java programming.