Compare commits
2 Commits
5e4a804908
...
e875a77cf3
Author | SHA1 | Date |
---|---|---|
|
e875a77cf3 | 4 months ago |
|
d3b64fdb97 | 4 months ago |
@ -0,0 +1,537 @@ |
||||
package com.jianshui.common.utils.encrypt; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.jianshui.common.core.domain.AjaxResult; |
||||
import com.jianshui.common.enums.ErrorCode; |
||||
import org.apache.shiro.codec.Base64; |
||||
import sun.misc.BASE64Decoder; |
||||
import sun.misc.BASE64Encoder; |
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.spec.GCMParameterSpec; |
||||
import javax.crypto.spec.IvParameterSpec; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.io.IOException; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.MessageDigest; |
||||
|
||||
/** |
||||
* 功能描述 |
||||
* 加密常用类 |
||||
*/ |
||||
public class JianshuiInvoiceDecryptUtilCopy { |
||||
|
||||
private static final String ALGORITHM = "AES/GCM/NoPadding"; |
||||
private static final int KEY_SIZE = 128; // 密钥长度为128位
|
||||
private static final int IV_LENGTH = 12; // 初始化向量长度为12字节(96位)
|
||||
private static final int TAG_LENGTH = 16; // GCM模式的认证标签长度为16字节
|
||||
|
||||
|
||||
/** |
||||
* 解密api里order请求,同时兼容老版本order里面包了identity的版本 |
||||
* |
||||
* @param order |
||||
* @param identity |
||||
* @param JNkey |
||||
*/ |
||||
public static AjaxResult decryptOrder(String order, String identity, String JNkey) { |
||||
// 平台解密
|
||||
try { |
||||
order = decrypt(order, JNkey); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return AjaxResult.error(ErrorCode.DECRYPT_ERROR); |
||||
} |
||||
|
||||
// 根据用户封装参数进行区别处理
|
||||
JSONObject orderJson = JSONObject.parseObject(order); |
||||
String myIdentity = orderJson.getString("identity"); |
||||
if (myIdentity != null) { |
||||
// order里面有identity的情况,取出来order
|
||||
JSONObject tempJson = new JSONObject(); |
||||
tempJson = orderJson.getJSONObject("order"); |
||||
if (tempJson != null) { |
||||
orderJson = tempJson; |
||||
} |
||||
} |
||||
|
||||
return AjaxResult.success(orderJson); |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:encrypt</li> <li> |
||||
* 加密方法 |
||||
* |
||||
* @param xmlStr 需要加密的消息字符串 |
||||
* @return 加密后的字符串 |
||||
*/ |
||||
public static String encrypt(String xmlStr, String mkey) { |
||||
byte[] encrypt = {}; |
||||
|
||||
try { |
||||
// 取需要加密内容的utf-8编码。
|
||||
if (xmlStr != null) { |
||||
encrypt = xmlStr.getBytes("utf-8"); |
||||
} |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 取MD5Hash码,并组合加密数组
|
||||
byte[] md5Hasn = null; |
||||
try { |
||||
md5Hasn = MD5Hash(encrypt, 0, encrypt.length); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 组合消息体
|
||||
byte[] totalByte = addMD5(md5Hasn, encrypt); |
||||
|
||||
// 取密钥和偏转向量
|
||||
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[] temp = null; |
||||
try { |
||||
temp = DES_CBC_Encrypt(totalByte, deskey, ivParam); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
// 使用Base64加密后返回
|
||||
return new BASE64Encoder().encode(temp); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @author kk |
||||
* @date 2023/11/21 14:45 |
||||
* @param: [xmlStr, mKey] |
||||
* @return: 新增AES加密方式,兼容旧版 |
||||
*/ |
||||
public static String decrypt(String xmlStr, String mKey) throws Exception { |
||||
|
||||
// base64解码
|
||||
BASE64Decoder decoder = new BASE64Decoder(); |
||||
byte[] encBuf = null; |
||||
try { |
||||
if (xmlStr != null) { |
||||
xmlStr = xmlStr.replaceAll(" ", "+");//解决URL里加号变空格
|
||||
} |
||||
encBuf = decoder.decodeBuffer(xmlStr); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
// 取密钥和偏转向量
|
||||
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[] temp = null; |
||||
try { |
||||
temp = DES_CBC_Decrypt(encBuf, deskey, ivParam); |
||||
|
||||
// 进行解密后的md5Hash校验
|
||||
byte[] md5Hash = null; |
||||
try { |
||||
md5Hash = MD5Hash(temp, 16, temp.length - 16); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 进行解密校检
|
||||
for (int i = 0; i < md5Hash.length; i++) { |
||||
if (md5Hash[i] != temp[i]) { |
||||
// System.out.println(md5Hash[i] + "MD5校验错误。" + temp[i]);
|
||||
throw new Exception("MD5校验错误。"); |
||||
} |
||||
} |
||||
// 返回解密后的数组,其中前16位MD5Hash码要除去。
|
||||
return new String(temp, 16, temp.length - 16, StandardCharsets.UTF_8); |
||||
} catch (Exception e) { |
||||
temp = AES_CBC_Decrypt(hexToBytes(xmlStr), mKey.getBytes(), ivParam); |
||||
String resTemp = new String(temp); |
||||
|
||||
// TODO kk:2024/1/30 解密有问题,每次前面带一段编码,后面在研究
|
||||
return resTemp.substring(resTemp.indexOf("{\""), resTemp.length()); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
// 新版AES解密方法
|
||||
private static byte[] AES_CBC_Decrypt(byte[] data, byte[] key, IvParameterSpec ivSpec) throws Exception { |
||||
|
||||
byte[] iv = key; |
||||
System.arraycopy(data, 0, iv, 0, IV_LENGTH); |
||||
|
||||
byte[] encryptedData = new byte[data.length - IV_LENGTH]; |
||||
System.arraycopy(data, IV_LENGTH, encryptedData, 0, encryptedData.length); |
||||
|
||||
SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); |
||||
Cipher cipher = Cipher.getInstance(ALGORITHM); |
||||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH * 8, iv); |
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec); |
||||
|
||||
return cipher.doFinal(encryptedData); |
||||
} |
||||
|
||||
/** 还原方法*/ |
||||
public static byte[] hexToBytes(String hexString) { |
||||
int len = hexString.length(); |
||||
byte[] data = new byte[len / 2]; |
||||
for (int i = 0; i < len; i += 2) { |
||||
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) |
||||
+ Character.digit(hexString.charAt(i+1), 16)); |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:encrypt</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* 解密方法 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param xmlStr 需要解密的消息字符串 |
||||
* @return 解密后的字符串 |
||||
* @throws Exception |
||||
*/ |
||||
/*public static String decrypt(String xmlStr, String mKey) throws Exception { |
||||
// base64解码
|
||||
BASE64Decoder decoder = new BASE64Decoder(); |
||||
byte[] encBuf = null; |
||||
try { |
||||
if (xmlStr != null) { |
||||
xmlStr = xmlStr.replaceAll(" ", "+");//解决URL里加号变空格
|
||||
} |
||||
encBuf = decoder.decodeBuffer(xmlStr); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
// 取密钥和偏转向量
|
||||
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[] temp = null; |
||||
try { |
||||
temp = DES_CBC_Decrypt(encBuf, deskey, ivParam); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
// 进行解密后的md5Hash校验
|
||||
byte[] md5Hash = null; |
||||
try { |
||||
md5Hash = MD5Hash(temp, 16, temp.length - 16); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
// 进行解密校检
|
||||
for (int i = 0; i < md5Hash.length; i++) { |
||||
if (md5Hash[i] != temp[i]) { |
||||
// System.out.println(md5Hash[i] + "MD5校验错误。" + temp[i]);
|
||||
throw new Exception("MD5校验错误。"); |
||||
} |
||||
} |
||||
|
||||
// 返回解密后的数组,其中前16位MD5Hash码要除去。
|
||||
return new String(temp, 16, temp.length - 16, "utf-8"); |
||||
}*/ |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:TripleDES_CBC_Encrypt</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* 经过封装的三重DES/CBC加密算法,如果包含中文,请注意编码。 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param sourceBuf 需要加密内容的字节数组。 |
||||
* @param deskey KEY 由24位字节数组通过SecretKeySpec类转换而成。 |
||||
* @param ivParam IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。 |
||||
* @return 加密后的字节数组 |
||||
* @throws Exception |
||||
*/ |
||||
public static byte[] TripleDES_CBC_Encrypt(byte[] sourceBuf, |
||||
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { |
||||
byte[] cipherByte; |
||||
// 使用DES对称加密算法的CBC模式加密
|
||||
Cipher encrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding"); |
||||
|
||||
encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam); |
||||
|
||||
cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length); |
||||
// 返回加密后的字节数组
|
||||
return cipherByte; |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:TripleDES_CBC_Decrypt</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* 经过封装的三重DES / CBC解密算法 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param sourceBuf 需要解密内容的字节数组 |
||||
* @param deskey KEY 由24位字节数组通过SecretKeySpec类转换而成。 |
||||
* @param ivParam IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。 |
||||
* @return 解密后的字节数组 |
||||
* @throws Exception |
||||
*/ |
||||
public static byte[] TripleDES_CBC_Decrypt(byte[] sourceBuf, |
||||
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { |
||||
|
||||
byte[] cipherByte; |
||||
// 获得Cipher实例,使用CBC模式。
|
||||
Cipher decrypt = Cipher.getInstance("TripleDES/CBC/PKCS5Padding"); |
||||
// 初始化加密实例,定义为解密功能,并传入密钥,偏转向量
|
||||
decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam); |
||||
|
||||
cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length); |
||||
// 返回解密后的字节数组
|
||||
return cipherByte; |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:DES_CBC_Encrypt</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* 经过封装的DES/CBC加密算法,如果包含中文,请注意编码。 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param sourceBuf 需要加密内容的字节数组。 |
||||
* @param deskey KEY 由8位字节数组通过SecretKeySpec类转换而成。 |
||||
* @param ivParam IV偏转向量,由8位字节数组通过IvParameterSpec类转换而成。 |
||||
* @return 加密后的字节数组 |
||||
* @throws Exception |
||||
*/ |
||||
public static byte[] DES_CBC_Encrypt(byte[] sourceBuf, |
||||
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { |
||||
byte[] cipherByte; |
||||
// 使用DES对称加密算法的CBC模式加密
|
||||
Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
||||
|
||||
encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam); |
||||
|
||||
cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length); |
||||
// 返回加密后的字节数组
|
||||
return cipherByte; |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:DES_CBC_Decrypt</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* 经过封装的DES/CBC解密算法。 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param sourceBuf 需要解密内容的字节数组 |
||||
* @param deskey KEY 由8位字节数组通过SecretKeySpec类转换而成。 |
||||
* @param ivParam IV偏转向量,由6位字节数组通过IvParameterSpec类转换而成。 |
||||
* @return 解密后的字节数组 |
||||
* @throws Exception |
||||
*/ |
||||
public static byte[] DES_CBC_Decrypt(byte[] sourceBuf, |
||||
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { |
||||
|
||||
byte[] cipherByte; |
||||
// 获得Cipher实例,使用CBC模式。
|
||||
Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
||||
// 初始化加密实例,定义为解密功能,并传入密钥,偏转向量
|
||||
decrypt.init(Cipher.DECRYPT_MODE, deskey, ivParam); |
||||
|
||||
cipherByte = decrypt.doFinal(sourceBuf, 0, sourceBuf.length); |
||||
// 返回解密后的字节数组
|
||||
return cipherByte; |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:MD5Hash</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* MD5,进行了简单的封装,以适用于加,解密字符串的校验。 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param buf 需要MD5加密字节数组。 |
||||
* @param offset 加密数据起始位置。 |
||||
* @param length 需要加密的数组长度。 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static byte[] MD5Hash(byte[] buf, int offset, int length) |
||||
throws Exception { |
||||
MessageDigest md = MessageDigest.getInstance("MD5"); |
||||
md.update(buf, offset, length); |
||||
return md.digest(); |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:byte2hex</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* 字节数组转换为二行制表示 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param inStr 需要转换字节数组。 |
||||
* @return 字节数组的二进制表示。 |
||||
*/ |
||||
public static String byte2hex(byte[] inStr) { |
||||
String stmp; |
||||
StringBuffer out = new StringBuffer(inStr.length * 2); |
||||
|
||||
for (int n = 0; n < inStr.length; n++) { |
||||
// 字节做"与"运算,去除高位置字节 11111111
|
||||
stmp = Integer.toHexString(inStr[n] & 0xFF); |
||||
if (stmp.length() == 1) { |
||||
// 如果是0至F的单位字符串,则添加0
|
||||
out.append("0" + stmp); |
||||
} else { |
||||
out.append(stmp); |
||||
} |
||||
} |
||||
return out.toString(); |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:addMD5</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* MD校验码 组合方法,前16位放MD5Hash码。 把MD5验证码byte[],加密内容byte[]组合的方法。 |
||||
* </pre> |
||||
* |
||||
* </li> |
||||
* |
||||
* @param md5Byte 加密内容的MD5Hash字节数组。 |
||||
* @param bodyByte 加密内容字节数组 |
||||
* @return 组合后的字节数组,比加密内容长16个字节。 |
||||
*/ |
||||
public static byte[] addMD5(byte[] md5Byte, byte[] bodyByte) { |
||||
int length = bodyByte.length + md5Byte.length; |
||||
byte[] resutlByte = new byte[length]; |
||||
|
||||
// 前16位放MD5Hash码
|
||||
for (int i = 0; i < length; i++) { |
||||
if (i < md5Byte.length) { |
||||
resutlByte[i] = md5Byte[i]; |
||||
} else { |
||||
resutlByte[i] = bodyByte[i - md5Byte.length]; |
||||
} |
||||
} |
||||
|
||||
return resutlByte; |
||||
} |
||||
|
||||
/** |
||||
* <li> |
||||
* 方法名称:getKeyIV</li> <li> |
||||
* 功能描述: |
||||
* |
||||
* <pre> |
||||
* |
||||
* </pre> |
||||
* </li> |
||||
* |
||||
* @param encryptKey |
||||
* @param key |
||||
* @param iv |
||||
*/ |
||||
public static void getKeyIV(String encryptKey, byte[] key, byte[] iv) { |
||||
// 密钥Base64解密
|
||||
BASE64Decoder decoder = new BASE64Decoder(); |
||||
byte[] buf = null; |
||||
try { |
||||
buf = decoder.decodeBuffer(encryptKey); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 前8位为key
|
||||
int i; |
||||
for (i = 0; i < key.length; i++) { |
||||
key[i] = buf[i]; |
||||
} |
||||
// 后8位为iv向量
|
||||
for (i = 0; i < iv.length; i++) { |
||||
iv[i] = buf[i + 8]; |
||||
} |
||||
} |
||||
|
||||
//byte数组相加
|
||||
public static byte[] addBytes(byte[] data1, byte[] data2) { |
||||
byte[] data3 = new byte[data1.length + data2.length]; |
||||
System.arraycopy(data1, 0, data3, 0, data1.length); |
||||
System.arraycopy(data2, 0, data3, data1.length, data2.length); |
||||
return data3; |
||||
|
||||
} |
||||
|
||||
public static String myencrypt(String data) throws Exception { |
||||
String str; |
||||
byte[] content = data.toString().getBytes("utf-8"); |
||||
byte[] sourceBuf = addBytes(MD5Hash(content, 0, content.length), content); |
||||
SecretKeySpec deskey = new SecretKeySpec("LmMGStGt".getBytes("utf-8"), "DES"); |
||||
IvParameterSpec ivParam = new IvParameterSpec("Yt54EQ==".getBytes("utf-8")); |
||||
str = Base64.encodeToString(DES_CBC_Encrypt(sourceBuf, deskey, ivParam)); |
||||
return str; |
||||
} |
||||
|
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
// String str = "{\"identity\":\"B8FB14CA1C6D7C93D9154E2262FF0F9DD796B180A2A7690A\",\"order\":{\"orderno\":\"No.20160308151221\",\"saletaxnum\":\"110101MXB6CK9Q6\",\"saleaddress\":\"万塘路30号\",\"salephone\":\"0571-1234567\",\"saleaccount\":\"工商银行6222222222222\",\"clerk\":\"sa\",\"payee\":\"sa\",\"checker\":\"sa\",\"invoicedate\":\"2016-03-08 15:12:21\",\"ordertotal\":\"8.00\",\"kptype\":\"1\",\"taxtotal\":\"1.16\",\"bhtaxtotal\":\"6.84\",\"address\":\"浙江杭州 0571-123456789\",\"phone\":\"0571-123456789\",\"taxnum\":\"339900000000004\",\"buyername\":\"浙江爱信诺航天信息有限公司\",\"account\":\"工商-123456789浙江爱信诺航天信息有限公司\",\"message\":\"浙江爱信诺航天信息有限公司\",\"fpdm\":\"\",\"fphm\":\"\",\"detail\":[{\"goodsname\":\"1\",\"spec\":\"1\",\"unit\":\"1\",\"hsbz\":\"1\",\"num\":\"1\",\"price\":\"4\",\"taxrate\":\"0.17\"},{\"goodsname\":\"2\",\"spec\":\"2\",\"unit\":\"2\",\"hsbz\":\"1\",\"num\":\"1\",\"price\":\"4\",\"taxrate\":\"0.17\"}]}}";
|
||||
// String str = "53316a496c4a524c58424e74567fa67fbca2984d30f04309cf9887a8c1f9de11287b4c8a41b5640888243d0f9de16e335281925cf48eb07d28b241228bb27af3bb9bb21328b9967f5219cb9c7ff3f818f0dbfbb8c6246eeda7909b8057a305b9fa4c95b8c98bf67300300b6e2e3c7acbb353289bb0496125698cf5719dcbbd2f81b6627e25b040c883866f290ee75d7931bf00c0f8fd6b89ffaab386f22114275687fe618458da978b8bc3bc302fdaa3bb8c06199a877bd5fdb1c628d0c18ad1b7e9038ad88ff3f37f9c212c31a994696d910e9ee403db58c9f557ac051c4bd7b441f5ba6e53eec828191545a3cd1f82a0a9bab46cc42c271ee7d6043f35d32ee9ec9014a015333f9f0cbc2cf0db816d44424606ea4370364cccd303c369efef51870a2bfe7c80d680f3ff9a40619e2bf94e3407a5315cd3ca7086489481c4004cf8ab90ed29e1c942b275ad956ef045efa5d6950036091f02d2165a7a1953e00bbe2d0e276ba5b7b9e47f106d8addfcef2d1e7bed39ab6b1a25de650f068596dbefd6cf9be4c7e3b50cadc0d49e370b3d6d9268ee73b3d5b1fcbf253f2785743484e9fd213582b7866d58672afb8603ec0f1a730b98469355d98c05205a7b497fb91e67b85af1fb2e5c388655819af08f5bea958704eb637f00073439aa968ea345f6befce238e65cb4d753483c94f58898991d0e096508350c56589ac2abe4b6e9f901cf414992d2a1f38887d885e0cecea164794ebdbd6dde7102a70a4ff1174b8791e824699b740cdf9d8c217f667ca209bcc7a927495036ed21ce280dfc2c1eb1a740d4021a5378cf11d1ac105a4b5107ee5831b831b2f30adf79527603578cd9d875fb0d5a974ec57354ef2a8e82ec23b0172a4d56f8d50898096c6bc6e6f5ebf49f1109f756be881457b206db1905b88d8922fc78507705dec5706e6d18d31b413e55541f4b394ea6f2a906e902dfea104efe30c0f909e25cba571be6703da5f589fe488bdb810cd9ed426f3a73476128a8393e9b40c96ec430a66412be6f613c63a9e94091f87a7daa6043b1be37fa6e7cc0520352cae8aaa7cbd26dce78f2a9419d39bf1f109249d64d5f34454dab0f3c9b1466192104790a9371794c6ea73838ec6197a87c87f328bc0921cc8e0d060b1d757fee0d59df84b1116519935c16d7b89655c78eb782c08fdcc04d60c8b7df32852a90cf72a10aace001fd0d56b2036a2f614fa695c506e96859c4227e09f4ec3f05cdaabd41127063b150b825a1905b1c26bf17cac02cefaefeb4265a531de3ccc1507609717418d34628542a6a95d49e1126d590b921c360ae18896d715bb5f9770e962966e6059693d8e7cdfed5dc1452b709ee5dfc96f9e679a40ec6e937792aa9f8c9268e31ebf21788352eb98cb658707b844f859c21aa2b62d2ff247f1b2e81b49e2028bc759bf2df4e54c151650ad0147c7e9e23d0d7364db0fa0810a404412c07f13389310200b114917c42a4ab7542170d5176b927972910b359b2fdb9809ae087f3edccab738f01ea5df1a7a2e05964363194ebdcd5dcb5d36b20621f4bb0837f8c9938aa6c8250e5430b0485c7aac982b83dc91c691d0d98fe110a7a0b31b324469887659b87ea4116f5039b4419816ff6a3dfc28431fd0c00360ac9bc1536ed11f1d9fde938fd057a5b9e4a4a87af81d72399b4475d431b7c65f1ce1626e696a45a7186b01e26b1c3710df3efd098af0e396c5c117294e81a6ea1874a396736207d9c92a7db2a92c2adc5061498d11a167915a34536d38f29c2a7e5574fbf1e2d8e3cc1c621305fdf97e0b64cfbbc94b76aa66b20a82b189894b5b3e369bdb639a55ab40f7afed7840eb836bc9a54ffdd228476b1085855e883975526826d2efbbb1aadf7c10f1441e28d7d515846";
|
||||
// String str = "543071746f37496638525641668e5246a6a424c1bd74a49fab978c707fd3b1aa3112a3134dbe42fb0108eccf0a4025cf3b173b77f6a32d0992f053062b56a60854e644add6a9da006690a43edeaf6e16ea8ee48a6eb23a6cc2da19bc8dcae068061fc6a292aa170770c050b370bcf7df2b5092b2212855ed15f0d4fe6159b0be9536ffcb32c2ec50bcbc899994b0be2d78a55b6a996d1e14384301e5676b9e2aac878eded4d7f2e19096b27662f4881123032ff8a80e096db65db55329400a1fdcd0aa8798be857020e93ca310a285169500d7ee51f527d85c4ace198717fb0831978f266fba0fc92191b6298f441cc7d6c17b178483a250444afd98f3255e032b482b6dbfe19b89067cda1d5b9be23e7b305b74cc9df481cbddc22119c84f7d59ed9286e91fd5c4187c81b9858e06354ccb384f64269c42b4f36c03ede6a2a9b350fa1bd57a61d353fa25a2e1f34bd896587824a10493fe19df3b41d22e0e2c298c4c0e827595763d4c7d93787b2e32baf6d571d0676a8178059bbeebe7dd00b76c31da713c750f00cf48034df6c958e830fada5fc57fcc728111ac3dc8fb28b3c134eb3aa1b7247085bd6eaca3461002a710209992079502a52869a0e7ad0f632de6ee118f05f9de9caffaa17467c88c3cdaea47f0a5da3925502d1cef3b67a0d0fbdc7752c975cf28ad1c92c93c21d6e3fc2d92e565f2f23131328c10a92e1843e9155b7cb01a5c7ec79dc6ab77644056bbae76696f6babad6725dcf181130733aaf4eeec7cca4ce060e2fef448a4ee2b8b970546f919538738ed0140bb507b66f7599aa2437245ac772756f0e4d6fe9a917932612082667bf07c908646148c7ca0c3d8bdd4ff0717f1226d4e321dbe4cb584dfca0669f8f00dd53d04b4e3d4114e390fecd49e3e4733f493f6097de40254b7bc0fe29ed603cb041563ac75380d8a90fd090f10407c5b4f158ef7f0690698f54ab3162aa44df33577a295c87cda736cf55cca6e9ef8b8230577b69b864557cb38de63fcfc6129d1224a002c2677460b05a6bb7b2fd9170ae6837838b843ea26eac966300f4f07a5caf88d92bc252a495fd73df59ba37d961b43d3845e1a40deeccaa82fd0456cd2db89201951bc737455f78e5dd15024acf411bf5df82db7e74b0f7f8dbe9787fa3cae7de1a993bca5ea37ccd8eba5783cfe43d2d2075780243685d8499d1a66dcc846d617a44f0d1968e456c6a4f4701a3ee77d26a3bcc030f162798214849af65e4a38bffa769354181c623238eb8f885ada17551c840d844d785b1c138664dcdb584bee88d372fb863ad3bb064ffe03c6e503c7216bb5d2a8c8bc50b3c0fd8e331972093447390496904393fc397c6dc147f4a177dd76c9ff0ba43cb8039a1d14f6ed1b165cca33a92d71af5ed26a484962aa6cbe8f906f8370adf7127c5b8a2b4d98099605c0a26735475c080fc3cbfe23a6902ea912ef813c74a915b1bb3ed116a65ccd7aa0943a5b2867b6b03eed0f30d4060dcebc791a4c46ab3e64ca583192b053a6d7aa99cf9c395a3ba155477b5e788dc28f6f431772faf192334602d084bdb908a21841ac1402649cc9c12440603f5488de41a2a9bd2e3da4ed153fa41740e94f92966c853957499f6125b88769abb0dc8e0123d852baac0ef1300f5c07ad9854c38861f8d17d9f98545f32ef4c397571d91996bb6f56bd3071df71915186ca65607e12c1241e647593cd16f637e5cb19eac64080f6917a4ce82aaa66bccd140d9df8d79f6e44d00a81d457e81f284ceddec827f1e589963fddf7c4636f033590076d8759c03e39370efd8c6740529785e594f7594f2f8bf9a5d5a4862eb9ca13ffcd2449f890359ebbca702d44dd28b4f392714788b3bc8321dcb55d9a7dc46718c84ae40711c2d8b52ca99f5d89b6d20888e9a5cf62a0c295aa566b0617ff8bf6c9e4a634ae62f4fa3cd3935315e9cc98aa98af0c895a7b0778cc703d176ca0a24abff161e193d8ac8cbe5fe469104992c7b2217834ddf77e59df32d2ce9e0f99bb6bfa89a680fad18882a3210466789356087a0f3ce9351d1aaaf667a67166a9049de4290f506f6f1bf0c5dd37b69eff7f7e8935408da8ff41c09d07f7345b23fc5f615021651f81c16f143f7e85b3c5b16f0a967964659754ec180d96a37c0854443f2d1bcbd55ea63a33b7981479588a27f033c4da5bc6900be68cc98e5c7e44b1f56b2bbe4ad527de711d3ce6c43addf740036fcb289120e89309b6c64b997f969c6937d26f6bb8245dd3c8c3af183aa12497a909372c977845d3eefafdb5523188834401903d1626cf3d5499f06af7658de591ca88";
|
||||
// String str = "LifVh2BkgTOsVG5PKvxdg11JvJMmZwIb46fuDbMTJexZ1D6DbJQqEvw0OGxwOQGMOwUHNS15yB9unCNiCI7qf6ccSkIQGWHvysrTaz4R2yZ+WGo9HzXeqYUGya/1bUp9zgF5cNQY0tJ7LVbMNJWYG9OvLn7PvLtV8Oas/OR9AGpKgKXLpzFQqmu9d89bltxRRs";
|
||||
String str = "+z1ernhDQtMrB6dZD1uKDqFXZkcmtfwX9B1E7HGmPDQgc4FpKpfVlPzltS4SIiXXXFjx9UGHHmzH\\nPWDzrYhG0gQpcWNzoQAr7Ycpd682IBDe9mQVFVrR5i41m3V7tMqFA1/SF2RMtU3ez8MBPU0wYW3h\\nMuxmCYwr1tjcjireoGIv7erYRtPbJ1/s/GSoFyicmRfrs9oFsJo="; |
||||
str=decrypt(str,"4H36m03LjyeQSg974yoO8Q=="); |
||||
System.out.println(str); |
||||
// str=decrypt(str);
|
||||
// System.out.println(str);
|
||||
// System.out.println(myencrypt(str));
|
||||
} |
||||
} |
@ -0,0 +1,223 @@ |
||||
package com.jianshui.common.utils.encrypt; |
||||
|
||||
import cn.hutool.core.util.HexUtil; |
||||
import cn.hutool.crypto.SecureUtil; |
||||
import sun.misc.BASE64Decoder; |
||||
import sun.misc.BASE64Encoder; |
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.SecretKey; |
||||
import javax.crypto.spec.GCMParameterSpec; |
||||
import javax.crypto.spec.IvParameterSpec; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.io.IOException; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.MessageDigest; |
||||
|
||||
/** |
||||
* 功能描述 |
||||
* 加密常用类 |
||||
*/ |
||||
public class JianshuiInvoiceEncryptUtilCopy { |
||||
|
||||
private static final String ALGORITHM = "AES/GCM/NoPadding"; |
||||
private static final int IV_LENGTH = 12; // 初始化向量长度为12字节(96位)
|
||||
private static final int TAG_LENGTH = 16; // GCM模式的认证标签长度为16字节
|
||||
|
||||
/** 加密共有两种方法 一、DES 二、AES*/ |
||||
|
||||
/** 一、DES加密方法*/ |
||||
public static String encrypt(String xmlStr, String mkey) { |
||||
byte[] encrypt = {}; |
||||
|
||||
try { |
||||
// 取需要加密内容的utf-8编码。
|
||||
if (xmlStr != null) { |
||||
encrypt = xmlStr.getBytes("utf-8"); |
||||
} |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 取MD5Hash码,并组合加密数组
|
||||
byte[] md5Hasn = null; |
||||
try { |
||||
md5Hasn = MD5Hash(encrypt, 0, encrypt.length); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 组合消息体
|
||||
byte[] totalByte = addMD5(md5Hasn, encrypt); |
||||
|
||||
// 取密钥和偏转向量
|
||||
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[] temp = null; |
||||
try { |
||||
temp = DES_CBC_Encrypt(totalByte, deskey, ivParam); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
// 使用Base64加密后返回
|
||||
return new BASE64Encoder().encode(temp); |
||||
} |
||||
|
||||
|
||||
/** DES加密接口*/ |
||||
public static byte[] DES_CBC_Encrypt(byte[] sourceBuf, |
||||
SecretKeySpec deskey, IvParameterSpec ivParam) throws Exception { |
||||
byte[] cipherByte; |
||||
// 使用DES对称加密算法的CBC模式加密
|
||||
Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
||||
|
||||
encrypt.init(Cipher.ENCRYPT_MODE, deskey, ivParam); |
||||
|
||||
cipherByte = encrypt.doFinal(sourceBuf, 0, sourceBuf.length); |
||||
// 返回加密后的字节数组
|
||||
return cipherByte; |
||||
} |
||||
|
||||
|
||||
/** AES 加密接口*/ |
||||
public static byte[] AES_Encrypt(byte[] sourceBuf, |
||||
byte[] keys, IvParameterSpec ivParam) throws Exception { |
||||
byte[] iv = keys; |
||||
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM); |
||||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH * 8, iv); |
||||
SecretKeySpec keySpec = new SecretKeySpec(keys, "AES"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec); |
||||
|
||||
byte[] ciphertext = cipher.doFinal(sourceBuf); |
||||
|
||||
byte[] result = new byte[IV_LENGTH + ciphertext.length]; |
||||
System.arraycopy(iv, 0, result, 0, IV_LENGTH); |
||||
System.arraycopy(ciphertext, 0, result, IV_LENGTH, ciphertext.length); |
||||
|
||||
return result; |
||||
} |
||||
|
||||
/** 转为十六进制字符串*/ |
||||
private static String bytesToHex(byte[] bytes) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
for (byte b : bytes) { |
||||
sb.append(String.format("%02x", b)); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
|
||||
/** 二、AES加密方法*/ |
||||
public static String encryptAES(String xmlStr, String mkey) { |
||||
byte[] encrypt = {}; |
||||
|
||||
// 取需要加密内容的utf-8编码。
|
||||
if (xmlStr != null) { |
||||
encrypt = xmlStr.getBytes(StandardCharsets.UTF_8); |
||||
} |
||||
// 取MD5Hash码,并组合加密数组
|
||||
byte[] md5Hasn = null; |
||||
try { |
||||
md5Hasn = MD5Hash(encrypt, 0, encrypt.length); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 组合消息体
|
||||
byte[] totalByte = addMD5(md5Hasn, encrypt); |
||||
|
||||
// 取密钥和偏转向量
|
||||
byte[] iv = new byte[16]; |
||||
IvParameterSpec ivParam = new IvParameterSpec(iv); |
||||
|
||||
// 使用DES算法使用加密消息体
|
||||
byte[] temp = null; |
||||
try { |
||||
temp = AES_Encrypt(totalByte, mkey.getBytes(), ivParam); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
// 使用Base64加密后返回
|
||||
return bytesToHex(temp); |
||||
} |
||||
|
||||
|
||||
public static byte[] MD5Hash(byte[] buf, int offset, int length) |
||||
throws Exception { |
||||
MessageDigest md = MessageDigest.getInstance("MD5"); |
||||
md.update(buf, offset, length); |
||||
return md.digest(); |
||||
} |
||||
|
||||
|
||||
public static byte[] addMD5(byte[] md5Byte, byte[] bodyByte) { |
||||
int length = bodyByte.length + md5Byte.length; |
||||
byte[] resutlByte = new byte[length]; |
||||
|
||||
// 前16位放MD5Hash码
|
||||
for (int i = 0; i < length; i++) { |
||||
if (i < md5Byte.length) { |
||||
resutlByte[i] = md5Byte[i]; |
||||
} else { |
||||
resutlByte[i] = bodyByte[i - md5Byte.length]; |
||||
} |
||||
} |
||||
|
||||
return resutlByte; |
||||
} |
||||
|
||||
|
||||
public static void getKeyIV(String encryptKey, byte[] key, byte[] iv) { |
||||
// 密钥Base64解密
|
||||
BASE64Decoder decoder = new BASE64Decoder(); |
||||
byte[] buf = null; |
||||
try { |
||||
buf = decoder.decodeBuffer(encryptKey); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 前8位为key
|
||||
int i; |
||||
for (i = 0; i < key.length; i++) { |
||||
key[i] = buf[i]; |
||||
} |
||||
// 后8位为iv向量
|
||||
for (i = 0; i < iv.length; i++) { |
||||
iv[i] = buf[i + 8]; |
||||
} |
||||
} |
||||
|
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
|
||||
// 生成一个随机的AES密钥
|
||||
SecretKey secretKey = SecureUtil.generateKey("AES"); |
||||
// 转换为16进制字符串
|
||||
String keyHex = HexUtil.encodeHexStr(secretKey.getEncoded()); |
||||
System.out.println("AES密钥:"); |
||||
System.out.println(keyHex); |
||||
|
||||
String str = "{\"terminalNumber\":\"\",\"orderno\":\"fcd3a67d-6a6e-f714-0790-c705db41403d\",\"sellerBank\":null,\"checker\":\"\",\"saleaddress\":null,\"payee\":\"\",\"hsbz\":\"2\",\"kjlx\":\"\",\"saleaccount\":null,\"qdbz\":\"0\",\"ext\":{\"sfhqsyed\":false},\"xsfnsrsbh\":\"92370214MACAAEK16E\",\"xsflxdh\":null,\"cpybz\":\"0\",\"fjh\":\"\",\"tax\":\"0.3\",\"dkbz\":\"0\",\"phone\":\"\",\"dqbm\":\"\",\"xsfyhzh\":null,\"jshj\":\"10.3\",\"tsfs\":\"2\",\"detail\":[{\"fphxz\":\"0\",\"lslbs\":\"\",\"unit\":\"\",\"hsbz\":\"0\",\"taxrate\":\"0.03\",\"yhzcbs\":\"0\",\"zzstsgl\":\"\",\"price\":\"\",\"num\":\"\",\"goodsname\":\"维修费\",\"spbm\":\"202\",\"spec\":\"\",\"taxfreeamt\":\"10.00\",\"tax\":\"0.30\",\"taxamt\":\"10.30\"}],\"taxamt\":\"10\",\"saletaxnum\":\"92370214MACAAEK16E\",\"sellerName\":\"城阳区兴顺通制冷设备维修服务部\",\"billInfono\":\"\",\"xsfkhh\":null,\"fpqqlsh\":\"fcd3a67d-6a6e-f714-0790-c705db41403d\",\"qdxmmc\":\"\",\"gfxxconfirm\":\"\",\"invoiceLine\":\"82\",\"serviceId\":\"lzfpkj\",\"email\":\"\",\"salephone\":null,\"address\":\"北京市-西城区-市岱岳区山口镇油坊村 80965631\",\"fpdm\":\"\",\"bsrysfzjhm\":\"370882198408212815\",\"kptype\":\"1\",\"message\":\"\",\"clerk\":\"鹿旭\",\"async\":\"true\",\"xsfmc\":\"城阳区兴顺通制冷设备维修服务部\",\"xsfdz\":null,\"taxnum\":\"91370112MA3C5P3322\",\"jqbh\":\"370882198408212815\",\"account\":\"中国建设银行济南市泉城支行 376010100101019236\",\"invoicedate\":\"2024-01-30\",\"buyername\":\"高速公路有限公司\",\"fphm\":\"\"}"; |
||||
String key = "S1jIlJRLXBNtIFihvQ0VPw=="; |
||||
System.out.println(encryptAES(str,key)); |
||||
|
||||
|
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue