富贵资源网 Design By www.hznty.com

这是我开发Worktile钉钉版本的时候遇到的当时感觉比较难的地方,现在写下来方法供大家学习交流

解密方法

exports.decryptMsg = function (text) {
 var aes_msg_buffer = new Buffer(text, 'base64');
 var key = new Buffer(aes_key + "=", 'base64');
 var iv = key.slice(0, 16);
 var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
 var dec = decipher.write(aes_msg_buffer);
 dec = decipher.read();
 var msg_leng_buffer = dec.slice(16, 20);
 var msg_length = msg_leng_buffer.readInt32BE();
 var rand_buffer = dec.slice(20, 20 + msg_length);
 var msg = rand_buffer.toString("utf-8");
 var result = JSON.parse(msg);
 return result;
};

加密方法

exports.encrypt_text = function (text, aes_key, suiteKey) {
 var key = new Buffer(aes_key + "=", 'base64');
 var iv = key.slice(0, 16);
 var random_buffer = new Buffer(exports.getRandomStr(16), 'utf-8');
 var buffer = new Buffer(text, 'utf-8');
 var length_buffer = new Buffer(4);
 length_buffer.writeUInt32BE(buffer.length, 0);
 var corpidBytes = new Buffer(suiteKey, 'utf-8');
 var newBuffer = Buffer.concat([random_buffer, length_buffer, buffer, corpidBytes]);
 var cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
 //cipher.write(newBuffer);
 //var dec = cipher.read();
 var cipheredMsg = Buffer.concat([cipher.update(/*encoded*/newBuffer), cipher.final()]);
 return cipheredMsg.toString('base64');
};

签名方法

var arr =[token, timestamp, nonce, res_encrypt] //token 是配置钉钉套件的token,timestamp时间戳, nonce随机字符串, res_encrypt是上面加密后的结果
exports.signature_arr = function (arr) {
 arr.sort();
 var str = "";
 for (var i = 0; i < arr.length; i++) {
  str += arr[i];
 }
 var sha1 = crypto.createHash('sha1');
 sha1.update(str);
 var msg_signature = sha1.digest('hex');
 return msg_signature;
};

以上所述是小编给大家介绍的nodejs 实现钉钉ISV接入的加密解密方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com