package com.nova.sankuai.domain.api.xiamenzhongheyinhua;
|
|
import cn.hutool.core.codec.Base64;
|
import com.nova.sankuai.infra.config.CommonException;
|
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.lang3.StringUtils;
|
|
import javax.crypto.Cipher;
|
import javax.crypto.spec.SecretKeySpec;
|
import java.nio.charset.StandardCharsets;
|
import java.util.Random;
|
|
/**
|
* 签名工具类
|
*
|
* @author CRM
|
*/
|
public class SignatureUtil {
|
private static final String AES = "AES";
|
private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
|
|
public static final String SECRET_KEY = "9WEMIkyqS4tMywWJ";
|
|
/**
|
* 串接arr参数,生成sha256 digest.
|
* json,nonce,secretKey,timestamp
|
*/
|
public static String getSignature(String... arr) {
|
StringBuilder sb = new StringBuilder();
|
for (String a : arr) {
|
sb.append(a);
|
}
|
return DigestUtils.sha256Hex(sb.toString());
|
}
|
|
|
/**
|
* 随机生成16位字符串.
|
*/
|
public static String getRandomStr() {
|
return getRandomStr(16);
|
}
|
|
/**
|
* 随机生成字符串.
|
*
|
* @param length 字符串长度
|
*/
|
public static String getRandomStr(int length) {
|
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
Random random = new Random();
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < length; i++) {
|
int number = random.nextInt(base.length());
|
sb.append(base.charAt(number));
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 获取当前时间戳
|
*
|
* @return
|
*/
|
public static Long getTimeStamp() {
|
return System.currentTimeMillis() / 1000L;
|
}
|
|
/**
|
* 加密
|
*
|
* @param value 加密内容
|
* @param secretKey 密钥
|
* @return 加密后数据
|
*/
|
public static String encrypt(String value, String secretKey) {
|
if (StringUtils.isAnyBlank(value, secretKey)) {
|
throw new CommonException("加密失败,参数为空");
|
}
|
if (secretKey.length() != 16) {
|
return null;
|
}
|
try {
|
byte[] aesKey = secretKey.getBytes(StandardCharsets.UTF_8);
|
SecretKeySpec keySpec = new SecretKeySpec(aesKey, AES);
|
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
|
return Base64.encode(cipher.doFinal(value.getBytes(StandardCharsets.UTF_8)));
|
} catch (Exception e) {
|
throw new CommonException("加密失败");
|
}
|
}
|
|
/**
|
* 对密文进行解密.
|
*
|
* @param encryptedData 需要解密的密文
|
* @param secretKey 密钥
|
* @return 解密得到的明文
|
*/
|
public static String decrypt(String encryptedData, String secretKey) {
|
if (StringUtils.isAnyBlank(encryptedData, secretKey)) {
|
throw new CommonException("解密失败,参数为空");
|
}
|
if (secretKey.length() != 16) {
|
return null;
|
}
|
try {
|
byte[] aesKey = secretKey.getBytes(StandardCharsets.UTF_8);
|
SecretKeySpec keySpec = new SecretKeySpec(aesKey, AES);
|
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
cipher.init(Cipher.DECRYPT_MODE, keySpec);
|
return new String(cipher.doFinal(Base64.decode(encryptedData)));
|
} catch (Exception e) {
|
throw new CommonException("解密失败");
|
}
|
}
|
|
public static void main(String[] args) {
|
String json = "[{\"city\":\"350200\",\"poolNo\":\"10003\",\"name\":\"张三\",\"phone\":\"18500000001\",\"level\":\"5\",\"sex\":\"0\",\"age\":40,\"company\":\"\",\"email\":\"2@qq.com\",\"qq\":\"123\",\"wechat\":\"123\",\"quota\":100000,\"duty\":\"0\",\"house\":\"3\",\"car\":\"3\",\"salaryType\":\"2\",\"socialSecurity\":\"1\",\"accumulationFund\":\"1\",\"lifeInsurance\":\"2\",\"creditCard\":\"无信用卡\",\"weBank\":\"无微粒贷\",\"carValue\":\"20万\",\"carAge\":\"3年2月\",\"carLoan\":\"0\"},{\"city\":\"350200\",\"poolNo\":\"10003\",\"name\":\"李四\",\"phone\":\"18500000002\",\"level\":\"3\",\"sex\":\"0\",\"age\":31,\"company\":\"\",\"email\":\"1@qq.com\",\"qq\":\"123\",\"wechat\":\"123\",\"quota\":100000,\"duty\":\"0\",\"house\":\"3\",\"car\":\"3\",\"salaryType\":\"2\",\"socialSecurity\":\"1\",\"accumulationFund\":\"1\",\"lifeInsurance\":\"2\",\"creditCard\":\"无信用卡\",\"weBank\":\"无微粒贷\",\"carValue\":\"20万\",\"carAge\":\"3年2月\",\"carLoan\":\"0\"}]";
|
String secretKey = "********"; //联系管理员获取密钥
|
json = encrypt(json, secretKey);
|
String nonce = getRandomStr();
|
String timestamp = Long.toString(getTimeStamp());
|
String sign = getSignature(json, nonce, secretKey, timestamp);
|
System.out.println("nonce:" + nonce);
|
System.out.println("timestamp:" + timestamp);
|
System.out.println("sign:" + sign);
|
System.out.println("body:" + json);
|
}
|
|
}
|