家庭智能网关elinkc
来源:互联网 发布:孝感广电网络领导班子 编辑:程序博客网 时间:2024/04/29 05:21
流程图及数据格式如下:
流程如上图:
代码框架
function send2GwDH() local reqinfo={} reqinfo.type="dh" reqinfo.sequence=devinfo.sequence reqinfo.mac=devinfo.mac reqinfo.data={} local p, pub, priv=dh.gkey() devinfo.pubkey=pub devinfo.privkey=priv reqinfo.data.dh_key=encodeBase64(devinfo.pubkey) reqinfo.data.dh_p=encodeBase64(p) reqinfo.data.dh_g=encodeBase64(string.pack('B','5')) print("sendto gw dh") data=json.encode(reqinfo) print(json.encode(reqinfo)) tcpSendData(data) print("recive from gw dh")endfunction reg() while true do local recdata = tcpRecivedData() if recdata then recjson = json.decode(recdata) if recjson then if recjson.type == "keyngack" then send2GwDH() --第四,五步 elseif recjson.type == "dh" then devinfo.secret=dh.gsecret(decodeBase64(recjson.data.dh_key), devinfo.privkey)--第六步根据网关的公钥和自己的私钥生成共享密钥(共享密钥同网关共享密钥),组网终端与智能家庭网关采用共享密钥加解密通讯参数。 send2GwReg() --发送设备注册信息 return keepRun() end end else return nil end endend--第三步function send2GwKeyngreq() local reqinfo={} reqinfo.type="keyngreq" reqinfo.sequence=devinfo.sequence reqinfo.mac=devinfo.mac reqinfo.keymodelist={} reqinfo.keymodelist[1]={} reqinfo.keymodelist[1].keymode="dh" data=json.encode(reqinfo) print(json.encode(reqinfo)) tcpSendData(data) print("recive from gw Keyngreq") --print(tcpRecivedData())end--第三步while true do if buildDevinfo() then if fromSysGet("uttcli get sysConf brideg_mode_flag") == "0" then os.execute("uttcli set sysConf sysConf brideg_mode_flag 1 ") os.execute("elink_route_bridge.sh ") end send2GwKeyngreq() reg() tcp:close() end sleep(2)end
第三步开始,如上图发送的协商密钥生成方式格式组包lua代码如下:
function send2GwKeyngreq() local reqinfo={} reqinfo.type="keyngreq" reqinfo.sequence=devinfo.sequence reqinfo.mac=devinfo.mac reqinfo.keymodelist={} reqinfo.keymodelist[1]={} reqinfo.keymodelist[1].keymode="dh" data=json.encode(reqinfo) print(json.encode(reqinfo)) tcpSendData(data) print("recive from gw Keyngreq") --print(tcpRecivedData())end
第四步,智能家庭网关完成双方密钥协商,组网终端采用DH加密算法,发送组网终端公钥给智能家庭网关:
function send2GwDH() local reqinfo={} reqinfo.type="dh" reqinfo.sequence=devinfo.sequence reqinfo.mac=devinfo.mac reqinfo.data={} --生成公钥和私钥 local p, pub, priv=dh.gkey() devinfo.pubkey=pub devinfo.privkey=priv --生成公钥和私钥 --经过base64编码后根据指定格式发送数据给家庭智能网关 reqinfo.data.dh_key=encodeBase64(devinfo.pubkey) reqinfo.data.dh_p=encodeBase64(p) reqinfo.data.dh_g=encodeBase64(string.pack('B','5')) print("sendto gw dh") --经过base64编码后根据指定格式发送数据给家庭智能网关 data=json.encode(reqinfo) print(json.encode(reqinfo)) tcpSendData(data) print("recive from gw dh")end
协商前发送数据代码:
function tcpSendData(data) devinfo.sequence=devinfo.sequence+1 tcp:send(string.pack('>I>I',0x3f721fb5,#data)..data)end
接收数据代码:
function tcpRecivedData() local data=tcp:receive(8) if data then flag, datalen=string.unpack('>I>I',data) --print("tcp recive 4 byte len "..datalen) data=tcp:receive(datalen) return data else return nil endend
协商后发送加密数据代码:
function sendSecretData(reqinfo) local jsondata= json.encode(reqinfo) print("send: "..jsondata) local aesdata= aes.encrypt_cbc(jsondata,devinfo.secret) --私钥devinfo.secret参与加密 tcpSendData(aesdata)end
解密收到数据代码:
function recivedSecretData() local data = tcpRecivedData() if data then local respjson=aes.decrypt_cbc(data, devinfo.secret) --私钥devinfo.secret参与解密 print("get:"..respjson) local jsondata= json.decode(respjson) return jsondata else print("get rec error") return nil endend
阅读全文
0 0
- 家庭智能网关elinkc
- 【WIZnet解决方案】Comnet智能网关让城市、家庭更智能
- DIY个人智能家庭网关—— 前言
- 智能家庭
- 联通家庭网关使用
- 什么是家庭网关
- 浅谈智慧家庭小优机器人通过WIFI+SSDP方式接入华为智能网关
- DIY个人智能家庭网关—— 路由器篇之刷机
- DIY个人智能家庭网关—— 路由器篇之安装python
- DIY个人智能家庭网关—— 路由器篇之申请公网IP
- DIY个人智能家庭网关—— 路由器篇之配置DDNS
- DIY个人智能家庭网关—— python篇之获取路由器外网地址
- DIY个人智能家庭网关—— 手机篇之Jpush推送平台
- DIY个人智能家庭网关——python篇之推送消息到手机
- DIY个人智能家庭网关——python篇之推送手机上下线事件
- DIY个人智能家庭网关——硬件篇之WIFI灯带DIY
- DIY个人智能家庭网关——手机篇之客户端设计
- 智能家庭网关将普及 捷稀推"骇客H3"深耕高端市场
- BeautifulSoup 知识小点
- Redis的安装
- PHP学习日记1_环境搭建
- 项目实战总结
- linux中文件操作函数
- 家庭智能网关elinkc
- Android开发-五大布局
- 与不知道什么学ping校ce的ji联zha考le(11.5)
- c++stl的queue和priority_queue
- es6笔记·····entries(),keys(),values()
- Spring 知识点提炼
- angular中copy和extend用法实例
- 超级台阶
- 洛谷 P2516 [HAOI2010]最长公共子序列