家庭智能网关elinkc

流程图及数据格式如下:

这里写图片描述

这里写图片描述

这里写图片描述

流程如上图:

代码框架

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")
end

function 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
    end
end
--第三步
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
    end
end

协商后发送加密数据代码:

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
    end
end
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值