|
@@ -0,0 +1,308 @@
|
|
|
|
+package webApiPlugin.utils;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.util.Base64Utils;
|
|
|
|
+
|
|
|
|
+import javax.crypto.Cipher;
|
|
|
|
+import javax.crypto.KeyGenerator;
|
|
|
|
+import javax.crypto.SecretKey;
|
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author bi lei
|
|
|
|
+ * @date 2020/12/18 10:35
|
|
|
|
+ */
|
|
|
|
+public class CodecUtil {
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(CodecUtil.class);
|
|
|
|
+ private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * AES加密密钥
|
|
|
|
+ */
|
|
|
|
+ public static final byte[] AES_SECRET_KEY_BYTES = Base64Utils.decodeFromString("KSKiOQgLKcdaCZLbnkgG7V==");
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * SHA1加密密钥(用于增加加密的复杂度)
|
|
|
|
+ */
|
|
|
|
+ public static final String SHA1_SECRET_KEY = "uErQ0KY3J2CwttyuaeEYR2==";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES加密再用Base64编码
|
|
|
|
+ *
|
|
|
|
+ * @param data 待加密数据
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesEncrypt(String data) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ byte[] dataBytes = data.getBytes("UTF-8");
|
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+ return Base64Utils.encodeToString(result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesEncrypt失败:data={},异常:{}", data, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES解密
|
|
|
|
+ *
|
|
|
|
+ * @param encryptedDataBase64
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesDecrypt(String encryptedDataBase64) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+ return new String(result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES加密再用Base64编码
|
|
|
|
+ *
|
|
|
|
+ * @param data 待加密数据
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesEncrypt(String data,String chartsetName) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ if(chartsetName == null || chartsetName.length()==0){
|
|
|
|
+ chartsetName = "UTF-8";
|
|
|
|
+ }
|
|
|
|
+ byte[] dataBytes = data.getBytes(chartsetName);
|
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+ return Base64Utils.encodeToString(result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesEncrypt失败:data={},异常:{}", data, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES解密
|
|
|
|
+ *
|
|
|
|
+ * @param encryptedDataBase64
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesDecryp(String encryptedDataBase64,String chartsetName) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+ if(chartsetName == null || chartsetName.length()==0){
|
|
|
|
+ chartsetName = "UTF-8";
|
|
|
|
+ }
|
|
|
|
+ return new String(result,chartsetName);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES解密
|
|
|
|
+ *
|
|
|
|
+ * @param encryptedDataBase64
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesDecrypt(String encryptedDataBase64,byte[] aesSecretKeyBytes) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesSecretKeyBytes, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+ return new String(result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES解密
|
|
|
|
+ *
|
|
|
|
+ * @param encryptedDataBase64
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesDecrypt(String encryptedDataBase64,byte[] aesSecretKeyBytes,String chartsetName) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesSecretKeyBytes, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+
|
|
|
|
+ if(chartsetName == null || chartsetName.length()==0){
|
|
|
|
+ chartsetName = "UTF-8";
|
|
|
|
+ }
|
|
|
|
+ return new String(result,chartsetName);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES加密再用Base64编码
|
|
|
|
+ *
|
|
|
|
+ * @param data 待加密数据
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesEncrypt(String data,byte[] AES_SECRET_KEY_BYTES) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ byte[] dataBytes = data.getBytes(DEFAULT_CHARSET);
|
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+ return Base64Utils.encodeToString(result);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesEncrypt失败:data={},异常:{}", data, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对业务数据进行加密,用AES解密
|
|
|
|
+ *
|
|
|
|
+ * @param encryptedDataBase64
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String aesDecryp(String encryptedDataBase64,byte[] AES_SECRET_KEY_BYTES) {
|
|
|
|
+ try {
|
|
|
|
+ // 加密算法/工作模式/填充方式
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ byte[] dataBytes = Base64Utils.decodeFromString(encryptedDataBase64);
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AES_SECRET_KEY_BYTES, "AES"));
|
|
|
|
+ byte[] result = cipher.doFinal(dataBytes);
|
|
|
|
+ return new String(result,DEFAULT_CHARSET);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("执行CodecUtil.aesDecrypt失败:data={},异常:{}", encryptedDataBase64, e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对数据进行加密,用SHA1加密再转换为16进制
|
|
|
|
+ *
|
|
|
|
+ * @param data
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String sha1Encrypt(String data,String SHA1_SECRET_KEY ) {
|
|
|
|
+ return DigestUtils.sha1Hex(data + SHA1_SECRET_KEY);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对数据进行加密,用SHA1加密再转换为16进制
|
|
|
|
+ *
|
|
|
|
+ * @param data
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String sha1Encrypt(String data) {
|
|
|
|
+ return DigestUtils.sha1Hex(data + SHA1_SECRET_KEY);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * AES密钥长度,支持128、192、256
|
|
|
|
+ */
|
|
|
|
+ private static final int AES_SECRET_KEY_LENGTH = 128;
|
|
|
|
+
|
|
|
|
+ private static String generateAESSecretKeyBase64(String key) {
|
|
|
|
+ try {
|
|
|
|
+ KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
|
|
|
|
+ keyGenerator.init(AES_SECRET_KEY_LENGTH);
|
|
|
|
+ SecretKey secretKey = keyGenerator.generateKey();
|
|
|
|
+ return Base64Utils.encodeToString(secretKey.getEncoded());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static class LoginDemoVo {
|
|
|
|
+ private String account;
|
|
|
|
+ private String password;
|
|
|
|
+
|
|
|
|
+ public String getAccount() {
|
|
|
|
+ return account;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setAccount(String account) {
|
|
|
|
+ this.account = account;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getPassword() {
|
|
|
|
+ return password;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setPassword(String password) {
|
|
|
|
+ this.password = password;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
|
+
|
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
|
+ JSONObject r = new JSONObject();
|
|
|
|
+// r.put("poundNo","");
|
|
|
|
+ r.put("subtaskId","13111111111");
|
|
|
|
+ r.put("validate","11111111");
|
|
|
|
+ r.put("beginDate","2024-04-03 11:11:11");
|
|
|
|
+ r.put("endDate","2024-04-03 11:11:11");
|
|
|
|
+ r.put("poundNo","");
|
|
|
|
+ r.put("unit",3);
|
|
|
|
+ r.put("batchNo",3);
|
|
|
|
+ r.put("productName",3);
|
|
|
|
+ r.put("netWeight",3);
|
|
|
|
+// r.put("loadingStartDate","2024-04-03 11:11:11");
|
|
|
|
+// r.put("loadingEndDate","2024-04-03 11:11:11");
|
|
|
|
+ r.put("remark",3);
|
|
|
|
+ r.put("operationStatus",3);
|
|
|
|
+ r.put("firstWeighingTime","2024-04-03 11:11:11");
|
|
|
|
+ r.put("secondWeighingTime","2024-04-03 11:11:11");
|
|
|
|
+ String data = JSON.toJSONString(r);
|
|
|
|
+ //对参数进行加密
|
|
|
|
+ String encryptedData = CodecUtil.aesEncrypt(data);
|
|
|
|
+ //生成签名
|
|
|
|
+ String sign = CodecUtil.sha1Encrypt(encryptedData + timestamp);
|
|
|
|
+ //封装请求参数
|
|
|
|
+ r = new JSONObject();
|
|
|
|
+ r.put("encryptedData",encryptedData);
|
|
|
|
+ r.put("timestamp",timestamp);
|
|
|
|
+ r.put("sign",sign);
|
|
|
|
+ //加密后的请求
|
|
|
|
+ System.out.println("加密后的请求:" + JSON.toJSONString(r));
|
|
|
|
+ //请求url
|
|
|
|
+// String url = "http://123.183.159.70:5066/sso-server/user/register";
|
|
|
|
+ //返回结果
|
|
|
|
+// String result2 = HttpRequestor.doPost(url, JSON.toJSONString(r));
|
|
|
|
+
|
|
|
|
+// System.out.println(result2);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|