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 | 26 ++++++++++++++++++++++++-- 1 files changed, 24 insertions(+), 2 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 7ab145b..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锛堣В瀵嗭級 @@ -37,6 +38,9 @@ */ public static String decryptData(String data,String aesKey,String encoding) { try{ + if(data == null){ + return null; + } if(StringUtils.isEmpty(encoding)){ encoding = "UTF-8"; } @@ -45,7 +49,7 @@ decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); data = new String(decryptCipher.doFinal(Hex.decodeHex(data.toCharArray()))); }catch (Exception e){ - throw new RuntimeException("瑙e瘑澶辫触锛�"+e.getMessage()); + throw new RuntimeException(e); } return data; } @@ -58,6 +62,9 @@ */ public static String encryptData(String data,String aesKey,String encoding) { try { + if(data == null){ + return null; + } if (StringUtils.isEmpty(encoding)) { encoding = "UTF-8"; } @@ -71,9 +78,24 @@ } data = builder.toString().toUpperCase(); } catch (Exception e) { - throw new RuntimeException("鍔犲瘑澶辫触锛�"+e.getMessage()); + throw new RuntimeException(e); } 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