nodejs 实现钉钉ISV接入的加密解密方法
程序员文章站
2022-06-29 20:09:20
这是我开发worktile钉钉版本的时候遇到的当时感觉比较难的地方,现在写下来方法供大家学习交流
解密方法
exports.decryptmsg = funct...
这是我开发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接入的加密解密方法,希望对大家有所帮助