电话加密方法
更新时间:2025-04-08
获取加密密钥
当您购买智能外呼产品后,可以登录智能外呼,在智能外呼的「系统管理」-> 「对接配置」中可以获取到密钥用于加密
加密代码示例
Java代码示例
Plain Text
1package baidu.com;
2
3import okhttp3.*;
4import org.json.JSONObject;
5
6import java.io.IOException;
7
8class Demo {
9
10 public static final String CHARSET = "utf-8";
11 private static final String AES = "AES";
12 private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
13
14 public static void main(String[] args) throws IOException {
15 // 电话 密钥
16 encryptAES("15532271587", "MRvNJ2R9HAAuImT3");
17 }
18
19 @SneakyThrows
20 /**
21 * 加密
22 * plainText 电话号码
23 * password 密钥
24 */
25 private static String encryptAES(String plainText, String password) {
26 String cipherText = plainText;
27 if (StringUtils.isNotBlank(plainText) && StringUtils.isNotBlank(password)) {
28 byte[] cipherBytes = encryptAES(plainText.getBytes(CHARSET), password.getBytes(CHARSET));
29 cipherText = byte2HexText(cipherBytes);
30 }
31
32 return cipherText;
33 }
34
35 private static byte[] encryptAES(byte[] plainBytes, byte[] password) {
36 byte[] cipherBytes = null;
37 try {
38 if (plainBytes != null && password != null) {
39 SecretKeySpec skeySpec = new SecretKeySpec(password, AES);
40 Cipher cipher = Cipher.getInstance(ALGORITHM);
41 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
42 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
43
44 cipherBytes = cipher.doFinal(plainBytes);
45 }
46 } catch (Exception e) {
47 // throw new BusinessException(e.getMessage(), e);
48 throw ExceptionUtil.make(CommonErrorStatusCode.AES_DECRYPT_ENCRYPT_ERROR);
49 }
50
51 return cipherBytes;
52 }
53
54 private static String byte2HexText(byte[] bytes) {
55 String hexText = "";
56 String strTmp = "";
57 for (int n = 0; n < bytes.length; n++) {
58 strTmp = (Integer.toHexString(bytes[n] & 0XFF));
59 if (strTmp.length() == 1) {
60 hexText = hexText + "0" + strTmp;
61 } else {
62 hexText = hexText + strTmp;
63 }
64 }
65 return hexText.toUpperCase();
66 }
67}
python代码示例
Plain Text
1from Crypto.Cipher import AES
2from Crypto.Util.Padding import pad
3import binascii
4
5class Demo:
6 CHARSET = "utf-8"
7 AES_MODE = AES.MODE_ECB
8
9 @staticmethod
10 def main():
11 # 电话号码,密钥
12 encrypted = Demo.encryptAES("15532271587", "MRvNJ2R9HAAuImT3")
13 print(encrypted)
14
15 @staticmethod
16 def encryptAES(plain_text, password):
17 cipher_text = plain_text
18 if plain_text and password:
19 cipher_bytes = Demo._encryptAES(plain_text.encode(Demo.CHARSET),
20 password.encode(Demo.CHARSET))
21 cipher_text = Demo.byte2HexText(cipher_bytes)
22 return cipher_text
23
24 @staticmethod
25 def _encryptAES(plain_bytes, password_bytes):
26 try:
27 if plain_bytes and password_bytes:
28 key = pad(password_bytes, AES.block_size)[:16]
29 cipher = AES.new(key, Demo.AES_MODE)
30 cipher_bytes = cipher.encrypt(pad(plain_bytes, AES.block_size))
31 return cipher_bytes
32 except Exception as e:
33 raise Exception("AES encryption error") from e
34
35 @staticmethod
36 def byte2HexText(bytes_array):
37 hex_text = binascii.hexlify(bytes_array).decode().upper()
38 return hex_text
39
40if __name__ == "__main__":
41 Demo.main()
42