import org.apache.commons.codec.binary.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.Key; import java.security.Security; public class AesUtil { static { Security.addProvider(new BouncyCastleProvider()); } private static final String IV = "33AB00C0B0Q00F12"; private static final String KEY = "34bfeacAB39053deb006df63"; private static final String AES_ALGORITHM = "AES/CBC/PKCS7Padding"; private static byte[] hexStringToByteArray(String hexString) { int len = hexString.length() / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) { result[i] = (byte) Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16); } return result; } private static String byteArrayToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); } /** * @description : AES加密返回16进制 **/ public static String encryptHex(String data) { try { Key k = new SecretKeySpec(KEY.getBytes(), "AES"); byte[] encoded = k.getEncoded(); SecretKeySpec aes = new SecretKeySpec(encoded, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(IV.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, aes, iv); byte[] bytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); return byteArrayToHexString(bytes); } catch (Exception e) { e.printStackTrace(); } return null; } /** * @description : AES解密返回16进制 **/ public static String decryptHex(String content) { try { Key k = new SecretKeySpec(KEY.getBytes(), "AES"); byte[] encoded = k.getEncoded(); SecretKeySpec aes = new SecretKeySpec(encoded, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(IV.getBytes()); cipher.init(Cipher.DECRYPT_MODE, aes, iv); byte[] encryptedBytes = hexStringToByteArray(content); byte[] bytes = cipher.doFinal(encryptedBytes); return new String(bytes, StandardCharsets.UTF_8); } catch (Exception e) { e.printStackTrace(); } return null; } /** * @description : 解密返回 Base64 **/ public static String decryptBase64(String content) { try { Key k = new SecretKeySpec(KEY.getBytes(), "AES"); byte[] encoded = k.getEncoded(); SecretKeySpec aes = new SecretKeySpec(encoded, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(IV.getBytes()); cipher.init(Cipher.DECRYPT_MODE, aes, iv); byte[] bytes = cipher.doFinal(Base64.decodeBase64(content)); return new String(bytes, StandardCharsets.UTF_8); } catch (Exception e) { e.printStackTrace(); } return null; } /** * @description : 加密返回 Base64 **/ public static String encryptBase64(String data) { try { Key k = new SecretKeySpec(KEY.getBytes(), "AES"); byte[] encoded = k.getEncoded(); SecretKeySpec aes = new SecretKeySpec(encoded, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(IV.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, aes, iv); byte[] bytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); return Base64.encodeBase64String(bytes); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String content = "hello world"; System.out.println(encryptHex(content)); System.out.println(decryptHex(encryptHex(content))); System.out.println(encryptBase64(content)); System.out.println(decryptBase64(encryptBase64(content))); } }
java中 AES/CBC/PKCS7Padding加密解密十六进制或base64
最新推荐文章于 2024-05-03 13:24:36 发布