|
@@ -1,15 +1,25 @@
|
|
|
package com.boman.web.core.utils;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.boman.common.core.utils.StringUtils;
|
|
|
import com.boman.web.core.domain.JsonRequest;
|
|
|
+import com.boman.web.core.domain.vo.AccountingDataVo;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.tomcat.util.buf.HexUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
|
|
-import javax.crypto.Cipher;
|
|
|
+import javax.crypto.*;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.SecureRandom;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import static com.boman.web.core.utils.RSAUtil.getUUID32;
|
|
|
|
|
@@ -18,6 +28,8 @@ import static com.boman.web.core.utils.RSAUtil.getUUID32;
|
|
|
* @Date: 2022/03/15/11:04
|
|
|
*/
|
|
|
public class AESUtil {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(AESUtil.class);
|
|
|
|
|
|
* 加密
|
|
|
*
|
|
@@ -50,6 +62,261 @@ public class AESUtil {
|
|
|
return new String(original);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ * 获取主键
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getUUID(){
|
|
|
+ String uuid = UUID.randomUUID().toString();
|
|
|
+ uuid = uuid.replaceAll("-", "");
|
|
|
+ return uuid;
|
|
|
+ }
|
|
|
+
|
|
|
+ * 随机生成秘钥
|
|
|
+ */
|
|
|
+ public static String getKey() {
|
|
|
+ try {
|
|
|
+ KeyGenerator kg = KeyGenerator.getInstance("AES");
|
|
|
+ kg.init(128);
|
|
|
+
|
|
|
+ SecretKey sk = kg.generateKey();
|
|
|
+ byte[] b = sk.getEncoded();
|
|
|
+ return byteToHexString(b);
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ * 使用指定的字符串生成秘钥
|
|
|
+ */
|
|
|
+ public static String getKeyByPass(String password) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ KeyGenerator kg = KeyGenerator.getInstance("AES");
|
|
|
+
|
|
|
+
|
|
|
+ kg.init(128, new SecureRandom(password.getBytes()));
|
|
|
+ SecretKey sk = kg.generateKey();
|
|
|
+ byte[] b = sk.getEncoded();
|
|
|
+ return byteToHexString(b);
|
|
|
+ }
|
|
|
+ catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ * byte数组转化为16进制字符串
|
|
|
+ * @param bytes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String byteToHexString(byte[] bytes) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (int i = 0; i < bytes.length; i++) {
|
|
|
+ String strHex=Integer.toHexString(bytes[i]);
|
|
|
+ if(strHex.length() > 3) {
|
|
|
+ sb.append(strHex.substring(6));
|
|
|
+ } else {
|
|
|
+ if(strHex.length() < 2) {
|
|
|
+ sb.append("0" + strHex);
|
|
|
+ } else {
|
|
|
+ sb.append(strHex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ * aes加密
|
|
|
+ * @param appKey
|
|
|
+ * @param content 明文
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String AESEncode(String appKey, String content) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ KeyGenerator keygen = KeyGenerator.getInstance("AES");
|
|
|
+
|
|
|
+ SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
|
|
+ secureRandom.setSeed(appKey.getBytes());
|
|
|
+
|
|
|
+ keygen.init(128, secureRandom);
|
|
|
+
|
|
|
+ SecretKey original_key = keygen.generateKey();
|
|
|
+
|
|
|
+ byte[] raw = original_key.getEncoded();
|
|
|
+
|
|
|
+ SecretKey key = new SecretKeySpec(raw, "AES");
|
|
|
+
|
|
|
+ Cipher cipher = Cipher.getInstance("AES");
|
|
|
+
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, key);
|
|
|
+
|
|
|
+ byte[] byte_encode = content.getBytes("utf-8");
|
|
|
+
|
|
|
+ byte[] byte_AES = cipher.doFinal(byte_encode);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
|
|
|
+
|
|
|
+ return AES_encode;
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchPaddingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvalidKeyException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalBlockSizeException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (BadPaddingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ * aes解密
|
|
|
+ * @param appkey
|
|
|
+ * @param content 密文
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String AESDncode(String appkey, String content) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ KeyGenerator keygen = KeyGenerator.getInstance("AES");
|
|
|
+
|
|
|
+ SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
|
|
|
+ secureRandom.setSeed(appkey.getBytes());
|
|
|
+
|
|
|
+ keygen.init(128, secureRandom);
|
|
|
+
|
|
|
+ SecretKey original_key = keygen.generateKey();
|
|
|
+
|
|
|
+ byte[] raw = original_key.getEncoded();
|
|
|
+
|
|
|
+ SecretKey key = new SecretKeySpec(raw, "AES");
|
|
|
+
|
|
|
+ Cipher cipher = Cipher.getInstance("AES");
|
|
|
+
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, key);
|
|
|
+
|
|
|
+ byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
|
|
|
+
|
|
|
+ * 解密
|
|
|
+ */
|
|
|
+ byte[] byte_decode = cipher.doFinal(byte_content);
|
|
|
+ String AES_decode = new String(byte_decode, "utf-8");
|
|
|
+ return AES_decode;
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchPaddingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvalidKeyException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalBlockSizeException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (BadPaddingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 验签方法
|
|
|
+ * @param content
|
|
|
+ * @param appkey
|
|
|
+ * @param sign
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean verifySign(String content,String appkey, String sign) {
|
|
|
+
|
|
|
+ String result = null;
|
|
|
+ result = sign( content, appkey);
|
|
|
+ if(result.equals(sign)){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ * 签名方法
|
|
|
+ * @param content sid+rid+rtime
|
|
|
+ * @param appkey
|
|
|
+ * @return sign
|
|
|
+ */
|
|
|
+ public static String sign(String content,String appkey) {
|
|
|
+
|
|
|
+ String result = null;
|
|
|
+ try {
|
|
|
+ Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
|
+ byte[] keyBytes = appkey.getBytes("UTF-8");
|
|
|
+ hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
|
|
|
+ logger.info("INPUT: " + content);
|
|
|
+ byte[] hmacSha256Bytes = hmacSha256.doFinal(content.getBytes("UTF-8"));
|
|
|
+ result = new String(Base64.encodeBase64(hmacSha256Bytes), "UTF-8");
|
|
|
+ logger.info("OUTPUT: " + result);
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvalidKeyException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ String key = getKey();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Long gjzwfwpt_rtime = new Date().getTime();
|
|
|
+ System.out.println(gjzwfwpt_rtime);
|
|
|
+ String gjzwfwpt_rid = "TE3400003400000001@f6eae674bdc840d2b0474dd3d736faf1";
|
|
|
+ String gjzwfwpt_sid = "7aa991a53cb04ea7a3f5c48fb119ddfd";
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(gjzwfwpt_rid).append(gjzwfwpt_sid).append(String.valueOf(gjzwfwpt_rtime));
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println(AESEncode("24289506f7b4b5c5f5708bbdd73cb761",sb.toString()));
|
|
|
+
|
|
|
+ String gjzwfwpt_sign = AESEncode("24289506f7b4b5c5f5708bbdd73cb761",sb.toString());
|
|
|
+
|
|
|
+ Map<String, String> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("gjzwfwpt_rid",gjzwfwpt_rid);
|
|
|
+ paramMap.put("gjzwfwpt_sid",gjzwfwpt_sid);
|
|
|
+ paramMap.put("gjzwfwpt_rtime",String.valueOf(gjzwfwpt_rtime));
|
|
|
+ paramMap.put("gjzwfwpt_sign",gjzwfwpt_sign);
|
|
|
+ String http = "http://59.255.22.70:8443/authz/authSystem/getAppSecret";
|
|
|
+ try {
|
|
|
+ String data = HttpClientUtils.doPost(http, paramMap);
|
|
|
+ System.out.println(data);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
Map<String, Object> businessParams = new HashMap<>();
|
|
|
String aseKey = "6717c09c65fd487bb61e6710613faa20";
|