EncryptTest.js 3.9 KB
'use strict';
/**
 HTTP请求结果校验返回字段值失败 HttpRequest: 
 curl 'http://test.alice.dodokeroro.cn:6655/dingtalk/events?
 signature=1e08e2b73b7581207d82eb4c96d7238aaf3a7f17&
 msg_signature=1e08e2b73b7581207d82eb4c96d7238aaf3a7f17&timestamp=1648094231677&nonce=7wgzaO5O' -d '{"encrypt":"4nzVlKqXYwYsH8IkfhGnRvzlKgud0gmSGKTWfq6bjClSpLNM8FYEQNTcA0Jc2yuvxlkxkhsbvvYTeCcXScU41CowiBhcuXONqj8amCINX83UYb6qSL5+aLl1VA6gcxPK"}' -H 'Content-Type:application/json' HttpCode:200 HttpReponse:


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


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

/** 加解密需要,可以随机填写。如 "12345" */
// const TOKEN = 'OUOdEgcMFxNDqoiADrf';
const TOKEN = 'BQB3WotDFx7gOKb5ewn6m';
/** 加密密钥,用于回调数据的加密,固定为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';
const ENCODING_AES_KEY ='96MzrEOxlqKx3OulRLJx0KzelfHKM7zz82sw4eI4UxD';
// 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);
let timeStamp = '1648094231677';
let nonce = '7wgzaO5O';

/** 测试加解密响应报文或者字符串 */
// 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));
let signature =  '1e08e2b73b7581207d82eb4c96d7238aaf3a7f17'; 
let encryptMsg =  '4nzVlKqXYwYsH8IkfhGnRvzlKgud0gmSGKTWfq6bjClSpLNM8FYEQNTcA0Jc2yuvxlkxkhsbvvYTeCcXScU41CowiBhcuXONqj8amCINX83UYb6qSL5+aLl1VA6gcxPK';

// signature='3cd3eac8d324861d85e6a359e4765fc4cc689832';
// timeStamp='1648094231747';
// nonce='LY6iii48'; 
// encryptMsg='jGaRz1YkaX3MdQMoe4s31TwVDYhFuH3gYCz4AzJsuSi/VV2C2ijIZTL83I4xnM4KU0DMdjaMuMTYqurW3o6rcNityAmHf0QchJQyaQaEBIM=';


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)
 
const plainText2 = encryptor.getDecryptMsg(result.msg_signature, result.timeStamp, result.nonce, result.encrypt)


console.log("plainText222::",plainText2)