From e5bae51093c5c92d20337bc40987baa212f1dd03 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期五, 07 一月 2022 10:50:37 +0800 Subject: [PATCH] 添加Mysql数据库的AES加解密方法 --- src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java | 54 ++++++++++++++++++++++++++++++------------------------ 1 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java b/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java index 4c24d45..5e7a1d8 100644 --- a/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java +++ b/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java @@ -33,17 +33,21 @@ * @param data 闇�瑕佽В瀵嗙殑鏁版嵁 * @param aesKey 绉橀挜 * @param encoding 缂栫爜锛屼笉濉粯璁TF-8 - * @return - * @throws Exception + * @return 瑙e瘑鏁版嵁 */ - public static String decrpt(String data,String aesKey,String encoding) throws Exception { - if(StringUtils.isEmpty(encoding)){ - encoding = "UTF-8"; + public static String decryptData(String data,String aesKey,String encoding) { + try{ + if(StringUtils.isEmpty(encoding)){ + encoding = "UTF-8"; + } + // Decrypt + final Cipher decryptCipher = Cipher.getInstance("AES"); + decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); + data = new String(decryptCipher.doFinal(Hex.decodeHex(data.toCharArray()))); + }catch (Exception e){ + e.printStackTrace(); } - // Decrypt - final Cipher decryptCipher = Cipher.getInstance("AES"); - decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); - return new String(decryptCipher.doFinal(Hex.decodeHex(data.toCharArray()))); + return data; } /**AES鍔犲瘑 @@ -51,23 +55,25 @@ * @param aesKey 绉橀挜 * @param encoding 缂栫爜锛屼笉濉粯璁TF-8 * @return 杩斿洖澶у啓鍔犲瘑鐨勬暟鎹� - * @throws Exception */ - public static String encrpt(String data,String aesKey,String encoding) throws Exception { - if(StringUtils.isEmpty(encoding)){ - encoding = "UTF-8"; + public static String encryptData(String data,String aesKey,String encoding) { + try { + if (StringUtils.isEmpty(encoding)) { + encoding = "UTF-8"; + } + // Encrypt + final Cipher encryptCipher = Cipher.getInstance("AES"); + encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); + char[] code = Hex.encodeHex(encryptCipher.doFinal(data.getBytes(encoding))); + StringBuilder builder = new StringBuilder(); + for (char d : code) { + builder.append(d); + } + data = builder.toString().toUpperCase(); + } catch (Exception e) { + e.printStackTrace(); } - // Encrypt - final Cipher encryptCipher = Cipher.getInstance("AES"); - encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); - char[] code = Hex.encodeHex(encryptCipher.doFinal(data.getBytes(encoding))); - StringBuilder builder = new StringBuilder(); - for (char d : code) { - builder.append(d); - } - String strning = builder.toString(); - return strning.toUpperCase(); - + return data; } } -- Gitblit v1.8.0