网页插件拼接参数加密
入口:管理中心->即时通信->任一网页插件->PUBLIC KEY
前端Demo示例:
jsencrypt基于rsa加解密的js库
有两种使用方式
- npm包管理模式
- 安装 jsencrypt
- npm install jsencrypt
- 引入 jsencrypt
- import JSEncrypt from 'jsencrypt'
- 使用
javascript var encryptor = new JSEncrypt() // 创建加密对象实例 var pubKey = '-----BEGIN PUBLIC KEY-----Public公钥-----END PUBLIC KEY-----' encryptor.setPublicKey(pubKey)//设置公钥 var rsaPassWord = encryptor.encrypt('要加密的内容') // 对内容进行加密
- 直接标签引入
- 安装 jsencrypt
- 使用
javascript var encryptor = new JSEncrypt() // 创建加密对象实例 var pubKey = '-----BEGIN PUBLIC KEY-----Public公钥-----END PUBLIC KEY-----' encryptor.setPublicKey(pubKey)//设置公钥 var rsaPassWord = encryptor.encrypt('要加密的内容') // 对内容进行加密
JavaDemo示例:
public class RSAUtils {
/**
* 加密算法RSA
*/
public static final String KEY_ALGORITHM = "RSA";
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
/**
* 私钥解密
* @param encryptedData 已加密数据
* @param privateKey 私钥(BASE64编码)
*/
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
byte[] keyBytes = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateK);
int inputLen = encryptedData.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
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 decryptedData;
}
/** */
/**
* 公钥加密
* @param data 源数据
* @param publicKey 私钥(BASE64编码)
*/
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
byte[] keyBytes = Base64.decodeBase64(publicKey);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicK = keyFactory.generatePublic(x509KeySpec);
// 对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicK);
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
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 encryptedData;
}
/**
* 公钥加密
*/
public static String encrypted(String data, String publicKey) {
try {
data = Base64.encodeBase64String(encryptByPublicKey(data.getBytes(), publicKey));
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
/**
* 私钥解密
*/
public static String decrypt(String data, String privateKey) {
String temp = "";
try {
byte[] rs = Base64.decodeBase64(data);
temp = new String(RSAUtils.decryptByPrivateKey(rs, privateKey),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return temp;
}
public static void main(String args[]){
/**
* {
* "customer": {
* "c_name": "客户姓名kimi"
* }
* }
* 正确的json格式即可
*/
String text ="{\n" + "\t\"customer\": {\n" + "\t\t\"c_name\": \"客户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wnGv9PrcgJjdFrA==";
String encryptedStr = RSAUtils.encrypted(text,publicKey);
String decryptStr = RSAUtils.decrypt(encryptedStr,privateKey);
System.out.println("密文:"+encryptedStr);
System.out.println("明文:"+decryptStr);
}
}
内容示例:
{
"customer": {
"c_name": "客户姓名kimi",
"c_email": "newest3@udesk.cn",
"c_other_emails": "11@udesk.cn,22@udesk.cn",
"c_desc": "意向客户,潜力巨大",
"c_org": "desc",
"c_phone": "1100110012",
"c_tags": "英俊,新加的",
"c_owner_group": "62",
"c_owner": "3",
"c_vip": "vip",
"c_qq": "123123",
"c_cf_年龄": 10,
"c_cf_爱好": "极限挑战",
"c_cn_title": "业务记录主题",
"c_cn_名字": "业务记录的名字",
"web_token": "newest3@udesk.cn"
}
}
注意:必须是正确的json格式
比如:
String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCGNDmc4+urXmt2lbsyVwQ+MNng/00rCXndESSyW0KBn31N+PdEexKCRI+OrxdSpwHUlXKAmQVYNKYZHJmcEdZ/KAq8IaHrAAogLWoOVKgKhiCUjflBcBVD6E93yQLa+AuMJCvupy6SP6mrzfyA2sZiHgvwPJac8SlNuUAMQ03u4QIDAQAB";
String privateKey="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIY0OZzj66tea3aVuzJXBD4w2eD/TSsJed0RJLJbQoGffU3490R7EoJEj46vF1KnAdSVcoCZBVg0phkcmZwR1n8oCrwhoesACiAtag5UqAqGIJSN+UFwFUPoT3fJAtr4C4wkK+6nLpI/qavN/IDaxmIeC/A8lpzxKU25QAxDTe7hAgMBAAECgYB75teSC7NXIfMhJpgPn9rg+AUP/2TlaPhDq0IwEKVUO0VaqZLQfLfX4SiQ2QI1Ys8Dz35JT/1pNCJtaMRBHQ7kLg+PISLXfO6yXDD2roT+ON/SJeOKVzfXXDPwhajpvxLHcbXfaWT8HI37myvDpFs8MAXTZvHoOiOBfqE+l6b8CQJBAPeBWMo+yC93G4UzyDDCx8Lw//fIGSwM0en+VykshCtxkgE+MkOzqKYHtcMxHIdkWr1tHcmVsz431PWwaCj3nwMCQQCKz2FkZLc67pi84QY+/f3zRD+p4DndCqSYh+PnKDpiTMsTzTHi20M5CfMXr0tyb0T9/v6rESnS2JQCySs2HHNLAkAfP1NIZVi8tVriHEF/9ndl4ytNyxNyfkPaOOQkLJybJ4F5arLJ3VrDa2HFPOQPK9pQ4//IvX+dsUrPoe/kjC6lAkAYtptMWHIW1+vxlM6b4HSaHwiGohkrNX3gejElMpBSeAGf4OSE0yzAtvjEurzf/td1sTR+GGwt0cloRZCf7ajJAkEAjWpxheI6hGnINCmMganNkVRwB8YwVNnhvadLFrx6CGNkzVWYVgUHPmXB3k8OtzxTdo8KnutwnGv9PrcgJjdFrA==";
请求示例:https://xxx.udesk.cn/im_client/?web_plugin_id=xxx&nonce=1685331807906×tamp=1685331807906&signature=166B6CCE7DF07F14E2D12796951BC9FC4348B47E&customer_encrypt=【加密后的内容】
web_token可以放到customer_encrypt中 参数内容按照如上json格式加密