From a89a8f02792ef0672bcceca3dd151042ef77d679 Mon Sep 17 00:00:00 2001 From: ChenJiaHe <763432473@qq.com> Date: 星期日, 09 一月 2022 11:41:55 +0800 Subject: [PATCH] Merge branch 'master' of https://gitee.com/huoxiong/hx_common --- src/main/java/com/hx/common/BaseController.java | 1 src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/com/hx/util/HttpMethodUtil.java | 2 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/hx/common/BaseController.java b/src/main/java/com/hx/common/BaseController.java index de5f6dc..a6ddd46 100644 --- a/src/main/java/com/hx/common/BaseController.java +++ b/src/main/java/com/hx/common/BaseController.java @@ -27,7 +27,6 @@ @Resource protected CommonService commonService; - /*璇蜂笉瑕佸0鏄庡彉閲忥紝浼氬鑷翠笉瀹夊叏锛屽洜涓鸿繖涓槸鍗曞垪*/ //鍙渶瑕佸姞涓婁笅闈㈣繖娈靛嵆鍙紝娉ㄦ剰涓嶈兘蹇樿娉ㄨВ diff --git a/src/main/java/com/hx/util/HttpMethodUtil.java b/src/main/java/com/hx/util/HttpMethodUtil.java index 0ce4fea..b97b4e9 100644 --- a/src/main/java/com/hx/util/HttpMethodUtil.java +++ b/src/main/java/com/hx/util/HttpMethodUtil.java @@ -46,7 +46,7 @@ con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); - con.setReadTimeout(5000); + con.setReadTimeout(300000); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "application/json"); if(header != null){ diff --git a/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java b/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java new file mode 100644 index 0000000..c2a5d0a --- /dev/null +++ b/src/main/java/com/hx/util/mysql/aes/MysqlHexAes.java @@ -0,0 +1,101 @@ +package com.hx.util.mysql.aes; + +import com.hx.util.StringUtils; +import org.apache.commons.codec.binary.Hex; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import java.io.UnsupportedEncodingException; +import java.util.regex.Pattern; + +/** + * mybatis鏁版嵁搴撶殑AES_ENCRYPT锛堝姞瀵嗭級鍜孉ES_DECRYPT锛堣В瀵嗭級 + * mybatis鏁版嵁搴撶殑HEX锛堝姞澹筹級UNHEX锛堣В澹筹級 + * @author CJH + * @Date 2021-01-06 + */ +public class MysqlHexAes { + + + public static SecretKeySpec generateMySQLAESKey(final String key, final String encoding) { + try { + final byte[] finalKey = new byte[16]; + int i = 0; + for(byte b : key.getBytes(encoding)) { + finalKey[i++%16] ^= b; + } + return new SecretKeySpec(finalKey, "AES"); + } catch(UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + /**AES 瑙e瘑 + * @param data 闇�瑕佽В瀵嗙殑鏁版嵁 + * @param aesKey 绉橀挜 + * @param encoding 缂栫爜锛屼笉濉粯璁TF-8 + * @return 瑙e瘑鏁版嵁 + */ + 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); + } + return data; + } + + /**AES鍔犲瘑 + * @param data 闇�瑕佽В瀵嗙殑鏁版嵁 + * @param aesKey 绉橀挜 + * @param encoding 缂栫爜锛屼笉濉粯璁TF-8 + * @return 杩斿洖澶у啓鍔犲瘑鐨勬暟鎹� + */ + 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); + } + 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