# 数据安全与加密

# 颁布接口签名

请妥善保管API账户与秘钥,当秘钥存在泄露风险,请及时联系售后工作人员进行销毁替换!!!

参数 描述
password 用户接口密码(点集提供的apiKey),若该密码有泄露风险。请及时咨询管理员销毁替换
mobile 接口中提及的手机号码,固定通知类接口一般为被通知方号码,任务接口一般为账户绑定的手机号。具体以对应接口的mobile参数描述为主。
timestamps 时间戳(13位),语音通知发送当前时间毫秒数,生成数字签名用,有效时间1分钟,强烈建议实时生成。并与接口timestamps值一致

签名采用 接口密码(password) 手机号(mobile) 时间戳(timestamps)拼接,并采用32位MD5加密生成(具体以实际接口为准)

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecurityUtil {

    public static void main(String[] args) {
        String password = "我是API接口密码";
        String mobile = "我是关联手机号";
        Long timestamps = System.currentTimeMillis();//13位时间戳
        String sign = getMD532Str(password + mobile + timestamps);
        System.out.println("获取到的API签名=" + sign);
    }
    /**
     * MD5 32位 加密
     */
    public static String getMD532Str(String sourceStr) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }
}

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

# 加密与解密

隐私内容均采用非对称 RSA加密,请联系售后工作人员获取对应公钥

请妥善保管公钥,避免隐私内容泄露。当公钥存在泄露风险,请及时联系售后工作人员进行销毁替换!!!

参数 描述
PUBLIC_KEY RSA加密公钥,若该公钥有泄露风险。请及时咨询管理员销毁替换

# 规则介绍

本文档接口RSA加密均采用2048长度秘钥,填充方式为PKCS#1 v1.5

RSA算法加密时,最大能加密的明文长度由被加密的RSA密钥长度决定。具体来说,最大可加密的明文长度由以下公式决定:

最大明文长度 = 密钥长度(以字节为单位) - 填充方案所需的额外字节数(2048位/8 = 256字节)。

本文档接口采用PKCS#1 v1.5填充,加密最大明文长度为:2048位/8 - 11 = 245字节

以下为部分代码示例,仅供参考。

# 请求内容加密

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSAUtils {
    //RSA最大加密明文大小
    private static final int MAX_ENCRYPT_BLOCK = 245;
  
    /**
     * 公钥加密
     * @param plaintext 需要加密的内容
     * @param publicKeyString 公钥 PUBLIC_KEY
     * @return 加密后的字符串
     * @throws Exception
     */
    public static String encryptByPublicKey(String plaintext, String publicKeyString) throws Exception {
        PublicKey publicKey = getPublicKey(publicKeyString);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] data = plaintext.getBytes();
        int inputLen = data.length;
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        return Base64.getEncoder().encodeToString(encryptedData);
    }
  
    // 将公钥字符串转换为PublicKey对象
    private static PublicKey getPublicKey(String key) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(key);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }

}
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

# 回执内容解密

本文档接口RSA加密均采用2048长度秘钥,填充方式为PKCS#1 v1.5

RSA算法加密时,最大能加密的明文长度由被加密的RSA密钥长度决定。具体来说,最大可加密的明文长度由以下公式决定:

最大明文长度 = 密钥长度(以字节为单位) - 填充方案所需的额外字节数(2048位/8 = 256字节)。

本文档接口采用PKCS#1 v1.5填充,解密最大秘文长度为:2048位/8 = 256字节

以下为部分代码示例,仅供参考。

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSAUtils {
    //RSA最大解密密文大小
    private static final int MAX_DECRYPT_BLOCK = 256;
  
  	/**
     * 公钥解密
     * @param encryptedText 需要解密的内容
     * @param publicKeyString 公钥 PUBLIC_KEY
     * @return
     * @throws Exception
     */
    public static String decryptWithPublicKey(String encryptedText, String publicKeyString) throws Exception {
        PublicKey publicKey = getPublicKey(publicKeyString);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] encryptedData = Base64.getDecoder().decode(encryptedText);
        int inputLen = encryptedData.length;
        int offSet = 0;
        byte[] cache;
        int i = 0;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return new String(decryptedData, StandardCharsets.UTF_8);
    }
    // 将公钥字符串转换为PublicKey对象
    private static PublicKey getPublicKey(String key) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(key);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }
}

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
最近更新: 6/20/2024, 3:42:05 PM
点集科技   |