Sunshine
2024-11-05 8f7985d7764a0aad24bd593ac5ea47b7fc290961
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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);
    }
 
}