public class AESUtil { /** * 密钥算法 */ private static final String ALGORITHM = "AES"; /** * 加解密算法/工作模式/填充方式 */ private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS5Padding"; /** * 生成key */ private static SecretKeySpec key = new SecretKeySpec("3d6ecd1d264b7224686af2c0a73f8d9c".getBytes(), ALGORITHM); /** * AES加密 * * @param data * @return * @throws Exception */ public static String encryptData(String data) throws Exception { // 创建密码器 Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING); // 初始化 cipher.init(Cipher.ENCRYPT_MODE, key); return new String(Base64.encode(cipher.doFinal(data.getBytes()))); } static SecretKey secretKey; /** * AES解密 * * @param data 待解密的数据 * @param keyMd5 解密的key * @return * @throws Exception */ public static String decryptData(String data,String keyMd5) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING); secretKey = new SecretKeySpec(keyMd5.getBytes("utf-8"), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey);//使用解密模式初始化 密钥 java.util.Base64.Decoder base64 = java.util.Base64.getDecoder(); byte[] decrypt = cipher.doFinal(base64.decode(data)); return new String(decrypt, "utf-8"); } public static void main(String[] args) throws Exception { String data = "73nx/039xYO9CcQE7b9dexbQ1WciyPr2hRYXVnTJ1jRnCsNQxnG/2m18IQtEDeJ99XFvvz1UPdOYf0G10d3oBHKQGJp0BHTfbDOF+ATFST3kJEyQzcaB/0cRtBxHH/6Den7sElBujMsPKNbRdYgelm8zN+YfZ+aK6FPoM4jvRhQmcD6AoyEBOeaxjvvL0K3AK29qGFd6qzJDXOVnCcoNich+K/9QyXRjS/V6h/0vNi4Z6U/31ev8kJneWN3b8w+Wrmc8I1ckUhEXChvgguR1K8p/0oXB38lBUZvNlq6AygGqWpOPT31cstxf0Af341DKA47EhutJ+8SZGlk/RQcs7u+lWo9IRXTwUqnCWADY/zv057Py6M6v/yeQy5nJpdHaSuV6l4t6Hgvk924XsC4UDZDoR+VsTNcZWNcdGHev295Z6+acuvAMl2py23t+Crovcxa62xEJ0noZIn57X0CzfNtLjoKf7KueA9lBslKz/hXmqRvX8dDdDn3NFLAWVLYcVplVEIXJtqiDBN7xMntbHTWYzBTiT9+K83yUssn5ZRe3lst4HiUOuy85AqJ8AHNQ+jxbLv1pqmDFVPYVlvWlDNCj70wL8Quk0lnJxb1mybolcIWAe5uGHuRZrRA1duVhFmTjrGLVT9faMRizZjTjSY9zO/gasYQKG/8I/YfoFtOkjIN2gMIO78TOe/+JnXDwvFTuoj3mMPWTqWvNzILysN8wGdB85vGawkjMbPqlNee+uigwXkSzJsv2+BwZEzuOPB6EREwQ7O0Hv116p+0G7Emgv5yHrypuZmjZXfNFgpAgbRLp9WEQ0f/6356oUpXeLrT4QRoAIliSZYON7sw9YRz2NRpq/LheyegnodI8LWa0C1K9jdfULg3G0AXf+1NRVUXueqxNSsptAX1WjsTFVJ/PS4mvtZ+yY0DVOUAhWjvHx2AqZjPTeuoXoH3KPQgUgSlGAhtNKIMAWpMM+Kik2mEc8EnTgYqQZzPhH3oY6CiqeSV5Ey0HBUI5Iey9IFKgTLpfE4m4ENA4tMbxzGDR7w=="; String keyMd5 = "3d6ecd1d264b7d04686af2c0173f8d9c"; System.out.println(decryptData(data,keyMd5)); } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/13706.html