This article describes the use of Hex encoding and decoding to implement Aes encryption and decryption. Share it for your reference, as follows:
Here, the Aes encryption and decryption method is encoded and decoded using Hex.
package com.baidu.wallet.bdwallet.utils;import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.DecoderException;import org.apache.commons.codec.binary.Hex;public class Test { private static final String AES="AES"; private static final String UTF8="UTF-8"; /** * AES encryption* @param content * @param pkey * @return * @throws DecoderException */ private static byte[] encrypt(String content, String pkey) throws DecoderException { try { String private_key=pkey; byte[] encodeFormat=null; try { //Why does the secret key need to be decoded? Because the secret key is the value of a secret key encoded by Hex, so it needs to be decoded when used encodeFormat = Hex.decodeHex(private_key.toCharArray()); } catch (DecoderException e) { e.printStackTrace(); } SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // The Cipher object actually completes the encryption operation Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // Encrypt the content for encoding byte[] byteContent = content.getBytes(UTF8); // Initialize the Cipher object cipher.init(Cipher.ENCRYPT_MODE, key); // Formal execution of 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; } /** * AES decryption* @param contents * @param password * @return * @throws DecoderException */ private static byte[] decrypt(String contents, String password) throws DecoderException { try { //Use Hex to decode the ciphertext byte[]content = Hex.decodeHex(contents.toCharArray()); //Why does the secret key need to be decoded? Because the secret key is the value of a secret key encoded by Hex, it must be decoded when used byte[] encodeFormat = Hex.decodeHex(password.toCharArray()); SecretKeySpec key = new SecretKeySpec(encodeFormat, AES); // The Cipher object actually completes the encryption operation Cipher cipher = Cipher.getInstance(AES); // Initialize the Cipher object 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; } /** * Aes encryption* @param context plaintext* @param private_key secret key* @return * @throws DecoderException */ public static String encryption(String context,String private_key) throws DecoderException{ //The encrypted plaintext becomes the ciphertext byte[] encryptResult = encrypt(context, private_key); //Password text Hex encoding String encryptResultStr = Hex.encodeHexString(encryptResult); return encryptResultStr; } /** * Aes decrypt* @param context ciphertext* @param private_key secret key* @return * @throws DecoderException * @throws UnsupportedEncodingException */ public static String decryption(String context,String private_key) throws DecoderException, UnsupportedEncodingException{ //Hex decoding is performed before decrypting the ciphertext here byte[] decryptResult = decrypt(context, private_key); String result = new String(decryptResult, UTF8); return result; } public static void main(String[] args) throws UnsupportedEncodingException, DecoderException { // Encrypted content String content = "123456787654321"; // AES encryption and decryption key String password = "This value is generally given, both senders know"; // Encrypt System.out.println("Before encryption: " + content); // Call the encryption method String encryptResultStr = encryption(content, password); System.out.println("After after encryption: " + encryptResultStr); // Call the decryption method String result = decrypt(encryptResultStr, password); // Decrypt the content for decoding System.out.println("After after decryption: " + result); }}There is no problem with this method in formal projects. Please note that you need to correct AES encryption and decryption here...
The above method is used in org.apache.commons.codec.binary.Hex. The configuration in maven is as follows:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version></dependency>
Note: You should use version 1.4 and above here, and there should be no Hex.encodeHexString (byte[]) method below 1.4!
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.