了解更多开源内容请访问:51CTO开源基础软件社区https://ost.51cto.com项目背景随着智能家居的普及,方便快捷的智能门锁受到消费者的欢迎,成为家居应用领域的热点。在接入鸿蒙智联智能门锁领域时,我们的技术人员发现,当用户通过手机为智能门锁设置临时密码,APP将加密后的密码发送给门锁时,设备固件目前没有相应的解密工具,智能门锁都有拍照功能,可以保存异常情况的现场照片。使用的模块不能直接将照片发送到第三方服务进行存储。这些尚属空白技术领域。逻辑实现技术人员通过了解行业情况,查阅大量相关技术资料,开发了专用解密工具,成功实现了临时密码设置功能;自主研发照片编解码工具,以智能家居云为中转,实现手机App照片实时查看功能。接下来我们看看上面提到的技术难点是如何实现的。部分截图显示:一、临时密码设置流程图:流程说明:1、智能生活APP生成临时密码,发送至智能家居云端存储。APP加密算法采用RSA的PKSC8加密算法。2、智能家居云端将密文发送给门锁设备。3、门锁对密文进行解密,得到临时密码和有效时间,然后保存在锁中。临时密码的有效期最长为7天,最短为30分钟。4、临时密码器设置成功,门锁主动上报设置成功状态。5、智能家居云收到状态后,将状态转发给APP。H5代码实现片段:data(){return{TemporarypasswordObj:{Creationtime:'',action:'1',id:'001',sixteenbitSN:'',userpassword:'123456',//管理员密码Temporarypassword:'888888',//临时密码Availabletime:'',//使用次数effectivedate:'',Failuretime:''},publicKey:'',}},methods:{saveTemporaryPassword(){//构建密码哈希数据字符串lethashedData=this.TemporarypasswordObj.sixteenbitSN+this.TemporarypasswordObj.userpassword+this.TemporarypasswordObj.Creationtime//执行哈希混淆lethashedDatastr=window.hilink.sha256Encrypt(hashedData)//构建临时密码密文letencryptionstringstr=this.TemporarypasswordOb.Creationtime+this.TemporarypasswordObj.action+this.TemporarypasswordObj.id+hashedDatastr+this.TemporarypasswordObj.Temporarypassword+this.TemporarypasswordObj.Availabletime+this.TemporarypasswordObj.effectivedate+this.TemporarypasswordObj.Failuretime//调用hilink接口进行RSA加密letcipherText=window.hilink.rsaEncrypt(encryptionstringstr,this.publicKey)//发送临时密码this.sendCiphertext(cipherText)},sendCiphertext(cipherText){try{letdata={remoteCode:{cipherText:cipherText}}window.hilink.setDeviceInfo('0',JSON.stringify(data),'setInfocallback');window.setInfocallback=res=>{让数据=JSON.parse(res);if(data.errcode===0){console.log('临时密码发送成功');}else{console.log('临时密码发送失败');}}}catch(e){console.log(e)}}}}固件代码片段:{intret;size_tolen=0;size_tdec_len=0;constchar*pers="simple_rsa";无符号字符*base_dec=NULL;mbedtls_rsa_contextrsa;mbedtls_rsa_contexttempctx;mbedtls_entropy_context熵;mbedtls_ctr_drbg_contextctr_drbg;mbedtls_rsa_init(&rsa,MBEDTLS_RSA_PKCS_V21,MBEDTLS_MD_SHA256);//初始化RSA结构mbedtls_rsa_init(&tempctx,MBEDTLS_RSA_PKCS_V21,MBEDTLS_MD_SHA256);//初始化RSA结构mbedtls_entropy_init(&entropy);//初始化结构mbedtls_ctr_drbg_init(&ctr_drbg);//初始化随机数结构ret=mbedtls_ctr_drbg_seed(&ctr_drbg,mbedtls_entropy_func,&entropy,(constuint8_t*)pers,strlen(pers));//根据个性化字符串更新seed1assert_exit(ret==0,ret);base_dec=rsa_global_hooks.rsa_allocate(BASE_DEC_LEN);if(base_dec==NULL){rsa_print_dbg("mallocbase64buff失败\r\n");转到错误退出;}memset(base_dec,0,BASE_DEC_LEN);ret=smartlock_rsa_readkeys(&tempctx);assert_exit(ret==0,ret);ret=mbedtls_rsa_import(&rsa,&tempctx.N,&tempctx.P,&tempctx.Q,&tempctx.D,&tempctx.E);assert_exit(ret==0,ret);ret=mbedtls_rsa_complete(&rsa);assert_exit(ret==0,ret);mbedtls_base64_decode(base_dec,BASE_DEC_LEN,&dec_len,pchipertext,chiperlen);ret=mbedtls_rsa_pkcs1_decrypt(&rsa,mbedtls_ctr_drbg_random,&ctr_drbg,MBEDTLS_RSA_PRIVATE,&olen,base_dec,文本,textlen);assert_exit(ret==0,ret);文本[olen]=0;rsa_global_hooks.rsa_deallocate(base_dec);mbedtls_ctr_drbg_free(&ctr_drbg);mbedtls_entropy_free(&熵);mbedtls_rsa_free(&tempctx);mbedtls_rsa_free(&rsa);返回0;错误退出:rsa_global_hooks.rsa_deallocate(base_dec);mbedtls_ctr_drbg_free(&ctr_drbg);mbedtls_entropy_free(&熵);mbedtls_rsa_free(&tempctx);mbedtls_rsa_free(&rsa);return-1;}2.图片上报展示由于门锁模块无法将图片上报给第三方服务器,只能使用智能家居云进行传输,智能家居云配置文件字符串类型有长度限制,所以需要对数据进行解包分包发送,APP收到数据后发送数据包合并,最终完成图片展示工作流程:代码实现片段:">
