From b39b1f28e610055a53c641a8e3147345ff973906 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期五, 07 一月 2022 13:12:38 +0800 Subject: [PATCH] 添加Mysql数据库的AES加解密方法(新增十六进制的判断方法) --- src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java | 76 ++++++++++++++++++++++++++------------ 1 files changed, 52 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..c2a5d0a 100644 --- a/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java +++ b/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java @@ -6,6 +6,7 @@ import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; +import java.util.regex.Pattern; /** * mybatis鏁版嵁搴撶殑AES_ENCRYPT锛堝姞瀵嗭級鍜孉ES_DECRYPT锛堣В瀵嗭級 @@ -33,17 +34,24 @@ * @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(data == null){ + return null; + } + 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){ + throw new RuntimeException(e); } - // 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 +59,43 @@ * @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(data == null){ + return null; + } + 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) { + throw new RuntimeException(e); } - // 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; } + /** + * 鍒ゆ柇鏄笉鏄崄鍏繘鍒剁殑瀛楃涓诧紙瀛楁瘝鏄ぇ鍐欑殑锛� + * @param str 闇�瑕佸垽鏂殑瀛楃涓� + * @return true or false + */ + public static boolean isHexStrValid(String str) { + if(str == null){ + return false; + } + String pattern = "^[0-9A-F]+$"; + return Pattern.compile(pattern).matcher(str).matches(); + } + + + } -- Gitblit v1.8.0