• 微信小程序 加密解密算法的nodejs实现


    加密数据解密算法

    接口如果涉及敏感数据(如wx.getUserInfo当中的 openid ),接口的明文内容将不包含敏感数据。开发者如需要获取敏感数据,需要对接口返回的加密数据( encryptData )进行对称解密。 解密算法如下:

    1. 对称解密使用的算法为 AES-128-CBC,数据采用PKCS#7填充。
    2. 对称解密的目标密文为 Base64_Decode(encryptData),
    3. 对称解密秘钥 aeskey = Base64_Decode(session_key), aeskey 是16字节
    4. 对称解密算法初始向量 iv = aeskey, 同样是16字节
    
    
    module.exports={
    getSessionKeyByCode:{
    url:"https://api.weixin.qq.com/sns/jscode2session",
    method:"GET",
    params: {
    appid:"wx408ea534cb79567e",
    secret:"e4fe5b9c97b2d7e1a68e14163e48ac8b",
    js_code:'',
    grant_type:"authorization_code"
    }
    }}



    exports.service = function (req, res) { var code = req.query.code; var encryptData = decodeURIComponent(req.query.encryptData); reqInfo.getSessionKeyByCode.params.js_code = code; httpUtil.get(reqInfo.getSessionKeyByCode).then(function (data) { var aeskey = new Buffer(data.session_key, 'base64'); var iv = aeskey; // AES-128-CBC对称解密算法 var decrypt = function (a, b, crypted){ crypted = new Buffer(crypted, 'base64'); var decipher = crypto.createDecipheriv('aes-128-cbc', a, b); var decoded = decipher.update(crypted,'base64','utf8'); decoded += decipher.final('utf8'); return decoded; }; var dec = decrypt(aeskey,iv,encryptData); var result = {}; try{ result = JSON.parse(dec); }catch(e){ logger.error(e); result = {}; } res.json({ code: 1, data: result }); }).catch(function(err){ logger.error(err); res.json({ code: 0, data: {} }); }) };

     PS 目前微信小程序开发者文档中,已给出各种语言的解密代码。并且解密密钥规定也有所调整。请依开发者文档为准。https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html

  • 相关阅读:
    C的xml编程文章链接
    《Android内核剖析》读书笔记 第13章 View工作原理【View树遍历】
    在MyEclipse中编写Web Project,编码设置全集合
    “克强经济学”绝非是通缩经济学
    VS2008--无法找到“XXX.exe”的调试信息,或者调试信息不匹配
    Dubbo架构设计详解--转载
    Beyond MySQL --Branching the popular database--转载
    eclipse中不能找到dubbo.xsd解决方法
    Java + MongoDB Hello World Example--转载
    Dubbo入门实例--转载
  • 原文地址:https://www.cnblogs.com/lightsun/p/5985501.html
Copyright © 2020-2023  润新知