...
代码块 | ||
---|---|---|
| ||
const userInfo = { // 传入用户信息,包括昵称、头像、性别、省、市及其他自定义的用户信息 wechatUserInfo: { nickname: "", headimgurl: "", sex: 0, city: "", province: "", country: "", // 必传字段,值为自建应用id,表明数据来源于某自建应用 platform: app_id, customFields: [ { // 这里只是示例 fieldValue: ["手机号码值","项目编码值"], fieldId: 123 } ] // 用户自定义字段,对象数组的形式,fieldValue为字符串数组类型,可传入多条数据,fieldId为工作台【用户】-【字段管理】中的 字段id(非 字段名称) }, audienceUserInfo: { // 传入用户身份标识,使用key value形式传入,key为识别字段的类型(对应工作台-【用户】-【用户身份管理】中的用户身份id),value为对应值,如果需使用微信openid和unionid作为用户身份标识,也许按照此格式在此处传递 // 如身份证,可传入"idCardNo":"xxxxxxxxxxxxxxx", 此处仅为举例,具体字段类型请根据接入方自身的用户身份识别标识传入 // 如openid,可传入"openid": "xxxxxxxx", 此处仅为举例,具体字段类型请根据接入方自身的用户身份识别标识传入 // 如third_userid,可传入"third_userid": "1234567890", 此处仅为举例,具体字段类型请根据接入方自身的用户身份识别标识传入 // 身份类型标识,全局唯一用户ID标识建议用 third_userid 。自定义身份字段名需要优先在 兔展 用户-身份字段管理中新增配置后才能正常使用 key: value, }, }; |
Vue版AES(ECB)加密demo:
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
vue下的前端加密
安装依赖:npm install crypto-js
代码示例:
/**
* AES加密
* @param {string} secretKey 用于加密的key
* @param {string} content 待加密明文字符串
* @returns {string} 加密后的密文
*/
export function setTzEncrypt (content) {
const secretKey = 'DpOGgKkq7-AFkKpUu5LsluM7acc'
const key = crypto.SHA1(crypto.SHA1(secretKey)).toString().substring(0, 32)
const newKey = crypto.enc.Hex.parse(key)
const newContent = crypto.enc.Utf8.parse(content)
const encrypted = crypto.AES.encrypt(newContent, newKey, { mode: crypto.mode.ECB, padding: crypto.pad.Pkcs7 })
return encrypted.ciphertext.toString().toUpperCase()
} |
Java版AES(ECB)加密demo:
以下为Java版本的AES加密demo。
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// 加密方法实现如下(加密的key:自建应用的授权密钥secretkey) /** * AES加密 (默认加密模式: AES/ECB/PKCS5Padding) * * @param secretKey: 用于加密的key * @param content: 待加密明文字符串 * @return 加密后密文 */ public static String encrypt(String secretKey, String content) throws Exception { // 1.构造密钥生成器,指定为AES算法,不区分大小写 KeyGenerator keygen = KeyGenerator.getInstance("AES"); // 2.根据encodeRules规则初始化密钥生成器,生成一个128位的随机源,根据传入的字节数组 SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(secretKey.getBytes(StandardCharsets.UTF_8)); keygen.init(128, secureRandom); // 3.产生原始对称密钥 SecretKey originalKey = keygen.generateKey(); // 4.获得原始对称密钥的字节数组 byte[] raw = originalKey.getEncoded(); // 5.根据字节数组生成AES密钥 SecretKey key = new SecretKeySpec(raw, "AES"); // 6.根据指定算法AES生成密码器 Cipher cipher = Cipher.getInstance("AES"); // 7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY cipher.init(Cipher.ENCRYPT_MODE, key); // 8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码 byte[] byteContent = content.getBytes(StandardCharsets.UTF_8); // 9.根据密码器的初始化方式--加密:将数据加密 byte[] byteAES = cipher.doFinal(byteContent); // 10.将加密后的数据转换为字符串并返回 return parseByte2HexStr(byteAES); } /** * 将二进制转换为16进制 * * @param buf 待转换二进制数组 * @return 16进制字符串 */ private static String parseByte2HexStr(byte[] buf) { StringBuilder sb = new StringBuilder(); for (byte b : buf) { String hex = Integer.toHexString(b & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } |
...
以下为Node.js版本的AES加密demo。
代码块 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// 加密方法实现如下(加密的key:自建应用的授权密钥secretkey) const crypto = require("crypto"); /** * SHA1PRNG算法 * @param {string} secretKey * @returns {Buffer} */ function sha1prng(secretKey) { const sha = crypto.createHash("sha1"); sha.update(secretKey, "utf8"); let state = sha.digest("buffer"); const buffer = Buffer.alloc(16); const getInt8 = (num) => { return num <= 127 ? num : num - 256; }; const updateState = (state, output) => { let last = 1; let v = 0; let t = 0; let zf = false; for (let i = 0; i < state.length; i++) { v = getInt8(state[i]) + getInt8(output[i]) + last; t = v & 255; zf = zf | (state[i] != t); state[i] = t; last = v >> 8; } if (!zf) state[0]++; return state; }; let index = 0; while (index < 16) { const sha = crypto.createHash("sha1"); sha.update(state); let output = sha.digest("buffer"); state = updateState(state, output); const todo = 16 - index > 20 ? 20 : 16 - index; for (var i = 0; i < todo; i++) { buffer[index++] = output[i]; output[i] = 0; } } return buffer; } /** * AES加密 (默认加密模式: AES/ECB/PKCS5Padding) * * @param {string} secretKey 用于加密的key * @param {string} content 待加密明文字符串 * @returns {string} 加密后的密文 */ function encrypt(secretKey, content) { // 使用128位ECB模式的`AES`加密算法 const algorithm = "aes-128-ecb"; // 对自建应用密钥进行`SHA2PRNG`计算,得出加密密钥 const key = sha1prng(secretKey); const cipher = crypto.createCipheriv(algorithm, key, ""); cipher.setAutoPadding(true); // 使用`utf8`字符集,保证中文不是乱码,并输出16进制的字符串 let encrypted = cipher.update(content, "utf8", "hex"); encrypted += cipher.final("hex"); return encrypted.toUpperCase(); } |
...