update 移除未使用utils

This commit is contained in:
2024-03-27 15:25:52 +08:00
parent dac785fa42
commit 190e96b13d
2 changed files with 0 additions and 171 deletions

View File

@@ -1,98 +0,0 @@
package com.jsowell.thirdparty.nanrui.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* TODO
*
* @author Lemon
* @Date 2023/10/12 13:42
*/
public class NRMD5Util {
public static void main(String[] args) {
String sigSecret = "yyyyyyyyyyyyyyyy";
String dataSecret = "DDDDDDD";
String dataSecretIv = "XXXXXXXX";
String operatorId = "MA002TMQX";
String data = "{'EquipAuthSeq':'MA002TMQX202004090925368255','ConnectorID':'MA01H3BQ1_1306060015204'}";
String retData = QEncodeUtil.encrypt(data, dataSecret, dataSecretIv);
String timeStamp = "20200409000000";
String seq = "0001";
String sig = getHmacMd5Str(sigSecret, operatorId + retData + timeStamp + seq);
}
public static String getHmacMd5Str(String key, String data) {
String result = "";
try {
byte[] keyByte = key.getBytes("UTF-8");
byte[] dataByte = data.getBytes("UTF-8");
byte[] hmacMd5Byte = getHmacMd5Bytes(keyByte, dataByte);
StringBuffer md5StrBuff = new StringBuffer();
for (byte b : hmacMd5Byte) {
if (Integer.toHexString(0xFF & b).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & b));
else md5StrBuff.append(Integer.toHexString(0xFF & b));
}
result = md5StrBuff.toString().toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static byte[] getHmacMd5Bytes(byte[] key, byte[] data) throws NoSuchAlgorithmException {
/**
* HmacMd5 calculation formula: H(K XOR opad, H(K XOR ipad, text))
* HmacMd5 计算公式H(K XOR opad, H(K XOR ipad, text))
* H 代表 hash 算法,本类中使用 MD5 算法K 代表密钥text 代表要加密的数据 ipad 为 0x36opad 为 0x5C。
*/
int length = 64;
byte[] ipad = new byte[length];
byte[] opad = new byte[length];
for (int i = 0; i < 64; i++) {
ipad[i] = 0x36;
opad[i] = 0x5C;
}
byte[] actualKey = key;
// Actual key.
byte[] keyArr = new byte[length];
// 如果密钥长度,大于 64 字节,就使用哈希算法,计算其摘要,作为真正的密钥。
if (key.length > length) {
actualKey = md5(key);
}
// append zeros to K 如果密钥长度不足 64 字节,就使用 0x00 补齐到 64 字节。
System.arraycopy(actualKey, 0, keyArr, 0, actualKey.length);
if (actualKey.length < length) {
for (int i = actualKey.length; i < keyArr.length; i++) keyArr[i] = 0x00;
}
// calc K XOR ipad 使用密钥和 ipad 进行异或运算。
byte[] kIpadXorResult = new byte[length];
for (int i = 0; i < length; i++) {
kIpadXorResult[i] = (byte) (keyArr[i] ^ ipad[i]);
}
// append "text" to the end of "K XOR ipad" 将待加密数据追加到 K XOR ipad 计算结果后面。
byte[] firstAppendResult = new byte[kIpadXorResult.length + data.length];
System.arraycopy(kIpadXorResult, 0, firstAppendResult, 0, kIpadXorResult.length);
// calc H(K XOR ipad, text) 使用哈希算法计算上面结果的摘要
System.arraycopy(data, 0, firstAppendResult, keyArr.length, data.length);
// calc K XOR opad 使用密钥和 opad 进行异或运算。
byte[] firstHashResult = md5(firstAppendResult);
byte[] kOpadXorResult = new byte[length];
for (int i = 0; i < length; i++) {
kOpadXorResult[i] = (byte) (keyArr[i] ^ opad[i]);
}
// 将 H(K XOR * ipad, text)结果追加到 K XOR opad 结果后面
byte[] secondAppendResult = new byte[kOpadXorResult.length + firstHashResult.length]; //
System.arraycopy(kOpadXorResult, 0, secondAppendResult, 0, kOpadXorResult.length);
// 对上面的数据进行哈希运算。
System.arraycopy(firstHashResult, 0, secondAppendResult, keyArr.length, firstHashResult.length);
return md5(secondAppendResult);
}
private static byte[] md5(byte[] str) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str);
return md.digest();
}
}

View File

@@ -1,73 +0,0 @@
package com.jsowell.thirdparty.nanrui.util;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* TODO
*
* @author Lemon
* @Date 2023/10/12 13:54
*/
public class QEncodeUtil {
public static String encrypt(String sSrc, String sKey, String datasecretiv) {
try {
if (sKey == null) {
return null;
}
// 判断 Key 是否为 16 位
if (sKey.length() != 16) {
return null;
}
byte[] raw = sKey.getBytes("UTF-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec(datasecretiv.getBytes());//使用 CBC 模式,需要一个向量 iv可增加加密算法的强度 1234567890123456
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
String str = new BASE64Encoder().encode(encrypted); // 此处使用 BASE64 做转码功能,同时能起到 2 次加密的作用。
str = str.replaceAll("\r", "");
str = str.replaceAll("\n", "");
return str;
} catch (Exception ex) {
return null;
}
}
// 解密
public static String decrypt(String sSrc, String sKey, String datasecretiv) {
try {
// 判断 Key 是否正确
if (sKey == null) {
System.out.print("Key 为空 null");
return null;
}
// 判断 Key 是否为 16 位
if (sKey.length() != 16) {
System.out.print("Key 长度不是 16 位");
return null;
}
byte[] raw = sKey.getBytes("UTF-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(datasecretiv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);
//先用 base64 解密
try {
byte[] original = cipher.doFinal(encrypted1);
return new String(original, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
return null;
}
}
}