EncryptTest.js 2.9 KB
'use strict';

const DingTalkEncryptor = require('./DingTalkEncryptor');
const utils = require('./Utils');


// const DingTalkEncryptException = require('./DingTalkEncryptException');

/** 加解密需要,可以随机填写。如 "12345" */
const TOKEN = 'OUOdEgcMFxNDqoiADrf';
/** 加密密钥,用于回调数据的加密,固定为43个字符,从[a-z, A-Z, 0-9]共62个字符中随机生成*/
// const ENCODING_AES_KEY = 'TXpRMU5qYzRPVEF4TWpNME5UWTNPRGt3TVRJek5EVTI';
// const ENCODING_AES_KEY = utils.getRandomStr(43);
const ENCODING_AES_KEY = 'FRArTXHBSiMuyPjOjk4RqhOLuQ54d75A6PyYbFrShJy';
// console.log('ENCODING_AES_KEY:\n' + ENCODING_AES_KEY);
// let buffer = Buffer.from(ENCODING_AES_KEY + '=', 'base64');
// const base64Str  = buffer.toString('base64');
// console.log(":::::ZZ",buffer.length,base64Str.length,base64Str,(Buffer.from(ENCODING_AES_KEY + '=', 'base64')));
/** 企业corpid, 可以在钉钉企业管理后台查看(https://oa.dingtalk.com/) */
// const CORP_ID = 'ding12345678901234567890123456789012';
const CORP_ID ='dingkuvhxslusd5hkjem';
/** 实例化加密类 */
// console.log('\nEncryptor Test:');
const encryptor = new DingTalkEncryptor(TOKEN, ENCODING_AES_KEY, CORP_ID);

// const plainText = 'success';
const ENCRYPT_RANDOM_16 = 'aaaabbbbccccdddd';
// const timeStamp = (new Date().getTime()).toString();
// const nonce = utils.getRandomStr(8);
const timeStamp = '1648014018742';
const nonce = 'AU1FFNAK';

/** 测试加解密响应报文或者字符串 */
// const testJson = {
//   EventType: 'bpms_instance_change',
//   processInstanceId: 'ad253df6-e175caf-68085c60ba8a',
//   corpId: 'ding2c4d8175651',
//   createTime: 1495592259000,
//   title: '自测-1016',
//   type: 'start',
//   staffId: 'er5875',
//   url: 'https://aflow.dingtalk.com/dingtalk/mobile/homepage.htm',
//   processCode: 'xxx',
// };
// const testJson = '中文乱码测试/abc/123dddd';

// // console.log(JSON.parse(JSON.stringify(testJson)));
const unencryptedJson = 'success';
// const unencryptedJson = JSON.stringify(testJson);
// console.log(`  node unencryptedJson:\n ${unencryptedJson}`);
// const encryptedJson = encryptor.encrypt(ENCRYPT_RANDOM_16, unencryptedJson);
// console.log(`  \nnode encryptedJson:\n ${encryptedJson}`);
// const decryptedJson = encryptor.decrypt(encryptedJson);
// console.log(`  \nnode decryptedJson:\n ${decryptedJson}, (${decryptedJson.length})`);
// console.log('  \nnode sign:\n ' + encryptor.getSignature(TOKEN, timeStamp, nonce, encryptedJson));
const signature =  '99011aff26fe6eea2aea3b9f1c17620483e8495d'; 
const encryptMsg =  'kKCIt2zJ1xPfCex1h3d4ZULhLvXi3RBbht1GxAsYK/Y9iZcw06P20xGTq8Lb9bskrd7fypdzcqo0GHvk+9zLonIXPFNKRTRkswZaH+t1A5683FoDYlXOsBRgpCsjSljo';

const plainText = encryptor.getDecryptMsg(signature, timeStamp, nonce, encryptMsg)


console.log("plainText::",plainText)

const result = encryptor.getEncryptedMap(unencryptedJson, timeStamp, utils.getRandomStr(8));


console.log("result::",result)