From b73d40db62e61e24cab63cebbec15745bf8bec43 Mon Sep 17 00:00:00 2001 From: peng fan Date: Thu, 8 Aug 2024 19:17:44 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=8C=E6=88=90=E8=8E=B7=E5=8F=96=E6=98=9F?= =?UTF-8?q?=E9=94=81=E7=8A=B6=E6=80=81=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=88=E7=9B=AE=E5=89=8D=E5=86=99=E5=85=A5=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E6=97=A0=E5=9B=9E=E5=A4=8D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/demo/demo.vue | 88 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/pages/demo/demo.vue b/pages/demo/demo.vue index 6b4352b..5f6ebc6 100644 --- a/pages/demo/demo.vue +++ b/pages/demo/demo.vue @@ -10,6 +10,7 @@ + @@ -61,6 +62,92 @@ export default { }) }, methods: { + // 获取锁状态 + getLockStatus() { + // 头部数据 + let buffer = new ArrayBuffer(12) + let binaryData = new Uint8Array(buffer) + + // 固定包头 + binaryData[0] = 0xEF + binaryData[1] = 0x01 + binaryData[2] = 0xEE + binaryData[3] = 0x02 + + // 包类型 发送 + binaryData[4] = 0x01 + + // 包序号 + binaryData[5] = this.number / 256 + binaryData[6] = this.number % 256 + this.number++ + + // 包标识 + binaryData[7] = 0x23 + + // 数据长度 + binaryData[8] = 80 / 256 + binaryData[9] = 80 % 256 + binaryData[10] = 70 / 256 + binaryData[11] = 70 % 256 + + console.log('请求头', Array.from(binaryData)) + + // 入参 + let paramsBuffer = new ArrayBuffer(70) + let paramsBinaryData = new Uint8Array(paramsBuffer) + + // 命令 + paramsBinaryData[0] = 0x3040 / 256 + paramsBinaryData[1] = 0x3040 % 256 + + // 设备ID + for(let i=0; i < this.lockId.length; i++){ + paramsBinaryData[2 + i] = this.lockId.charCodeAt(i) + } + + // userId + const authUserId = '294' + for(let i=0; i < authUserId.length; i++){ + paramsBinaryData[42 + i] = authUserId.charCodeAt(i) + } + + const nowTime = parseInt(new Date().getTime() / 1000) + + paramsBinaryData[62] = (nowTime & 0xff000000) >> 24 + paramsBinaryData[62 + 1] = (nowTime & 0xff0000) >> 16 + paramsBinaryData[62 + 2] = (nowTime & 0xff00) >> 8 + paramsBinaryData[62 + 3] = (nowTime & 0xff) + + paramsBinaryData[66] = (nowTime & 0xff000000) >> 24 + paramsBinaryData[66 + 1] = (nowTime & 0xff0000) >> 16 + paramsBinaryData[66 + 2] = (nowTime & 0xff00) >> 8 + paramsBinaryData[66 + 3] = (nowTime & 0xff) + + console.log('ecb加密前入参', Array.from(paramsBinaryData)) + console.log('密钥', Array.from(this.commKey)) + const encrypted = sm4.encrypt(paramsBinaryData, this.commKey, { mode: 'ecb', output: 'array' }) + console.log('ecb加密后的数据', Array.from(encrypted)) + + let newBuffer = new ArrayBuffer(92) + let newBinaryData = new Uint8Array(newBuffer) + for(let i = 0; i < 12; i++){ + newBinaryData[i] = binaryData[i] + } + for(let i = 12; i < 92; i++){ + newBinaryData[i] = encrypted[i - 12] + } + console.log('crc前的数据', Array.from(newBinaryData)) + let resultBuffer = new ArrayBuffer(94) + let resultBinaryData = new Uint8Array(resultBuffer) + for(let i = 0; i < 92; i++){ + resultBinaryData[i] = newBinaryData[i] + } + + resultBinaryData[92] = crc.crc16kermit(resultBuffer) / 256 + resultBinaryData[93] = crc.crc16kermit(resultBuffer) % 256 + this.writeBLECharacteristicValue(resultBuffer) + }, // 十六进制字符串转Uint8Array hexToUint8Array(hex) { if (hex.length % 2 !== 0) { @@ -74,7 +161,6 @@ export default { }, // 获取私钥 getCommKey() { - const cmdId = 0x3091 const keyId = '0' const authUserId = '294' const nowTime = parseInt(new Date().getTime() / 1000)