feature: 加密/解密identity

jianshui-ui
xingze 1 year ago
parent 0023365133
commit 10d35fff70
  1. 40
      jianshui-admin/src/main/java/com/jianshui/web/controller/sandbox/IndexController.java
  2. 78
      jianshui-common/src/main/java/com/jianshui/common/utils/encrypt/InvoiceEncryptUtil.java

@ -3,7 +3,11 @@ package com.jianshui.web.controller.sandbox;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.utils.encrypt.InvoiceEncryptUtil;
import com.jianshui.system.mapper.CompanyserviceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -16,15 +20,47 @@ import org.springframework.web.bind.annotation.RestController;
**/
@RestController
@RequestMapping("/sandbox")
@Slf4j
public class IndexController {
@Autowired
private CompanyserviceMapper companyserviceMapper;
@PostMapping("/encrypt")
public AjaxResult encrypt(@RequestBody JSONObject requestBody) {
String key = requestBody.getString("key");
String order = requestBody.getString("order");
// 查询企业信息
Companyservice companyservice = companyserviceMapper.selectCompanyserviceByIdentity(key);
try {
if (companyservice != null){
String encryptTest = InvoiceEncryptUtil.encrypt(order, companyservice.getSecret());
return AjaxResult.success("success", encryptTest);
}else {
String encryptTest = InvoiceEncryptUtil.encrypt(order, key);
return AjaxResult.success("success", encryptTest);
}
}catch (Exception e){
return AjaxResult.error(e.getMessage());
}
}
@PostMapping("/decrypt")
public AjaxResult decrypt(@RequestBody JSONObject requestBody) {
String key = requestBody.getString("key");
String order = requestBody.getString("order");
// 查询企业信息
Companyservice companyservice = companyserviceMapper.selectCompanyserviceByIdentity(key);
try {
String encryptTest = InvoiceEncryptUtil.encrypt(order, key);
return AjaxResult.success("success", encryptTest);
if (companyservice != null){
String decryptTest = InvoiceEncryptUtil.decrypt(order, companyservice.getSecret());
return AjaxResult.success("success", decryptTest);
}else {
String decryptTest = InvoiceEncryptUtil.decrypt(order, key);
return AjaxResult.success("success", decryptTest);
}
}catch (Exception e){
return AjaxResult.error(e.getMessage());
}

@ -3,12 +3,19 @@ package com.jianshui.common.utils.encrypt;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/**
* 功能描述
@ -65,6 +72,77 @@ public class InvoiceEncryptUtil {
}
public static String decrypt(String encryptedStr, String mkey) {
byte[] encryptedData = {};
try {
// 解析Base64编码的字符串
encryptedData = new BASE64Decoder().decodeBuffer(encryptedStr);
} catch (Exception e) {
e.printStackTrace();
return null;
}
// 取密钥和偏转向量
byte[] key = new byte[8];
byte[] iv = new byte[8];
getKeyIV(mkey, key, iv);
SecretKeySpec deskey = new SecretKeySpec(key, "DES");
IvParameterSpec ivParam = new IvParameterSpec(iv);
// 使用DES算法解密消息体
byte[] decryptedData = null;
try {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, deskey, ivParam);
decryptedData = cipher.doFinal(encryptedData);
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException | InvalidAlgorithmParameterException |
IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
return null;
}
// 分离出MD5哈希和原始数据
byte[] originalData = null;
byte[] calculatedMD5Hash = null;
// 假设MD5哈希占用16字节
if (decryptedData.length >= 16) {
calculatedMD5Hash = new byte[16];
originalData = new byte[decryptedData.length - 16];
System.arraycopy(decryptedData, 0, calculatedMD5Hash, 0, 16);
System.arraycopy(decryptedData, 16, originalData, 0, originalData.length);
} else {
// 数据长度不符合预期,可能被篡改
return null;
}
// 验证MD5哈希
byte[] recalculatedMD5Hash = null;
try {
recalculatedMD5Hash = MD5Hash(originalData, 0, originalData.length);
} catch (Exception e) {
e.printStackTrace();
return null;
}
// MD5校验失败
if (!Arrays.equals(calculatedMD5Hash, recalculatedMD5Hash)) {
return null;
}
// 将原始数据转换回字符串
String originalStr = null;
try {
originalStr = new String(originalData, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
return originalStr;
}
/**
* <li>
* 方法名称:DES_CBC_Encrypt</li> <li>

Loading…
Cancel
Save