From e3b963b76a6f16abe798a8d9b2b92e71bcb8b4ab Mon Sep 17 00:00:00 2001 From: peng fan Date: Fri, 9 Aug 2024 09:24:50 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/demo/demo.vue | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/pages/demo/demo.vue b/pages/demo/demo.vue index 5f6ebc6..7c17357 100644 --- a/pages/demo/demo.vue +++ b/pages/demo/demo.vue @@ -34,7 +34,8 @@ export default { commKey: new Uint8Array(16), signKey: new Uint8Array(16), showList: true, - number: 1 + number: 1, + lockStatus: new Uint8Array(1) } }, onLoad() { @@ -134,8 +135,8 @@ export default { for(let i = 0; i < 12; i++){ newBinaryData[i] = binaryData[i] } - for(let i = 12; i < 92; i++){ - newBinaryData[i] = encrypted[i - 12] + for(let i = 0; i < 80; i++){ + newBinaryData[i + 12] = encrypted[i] } console.log('crc前的数据', Array.from(newBinaryData)) let resultBuffer = new ArrayBuffer(94) @@ -144,8 +145,8 @@ export default { resultBinaryData[i] = newBinaryData[i] } - resultBinaryData[92] = crc.crc16kermit(resultBuffer) / 256 - resultBinaryData[93] = crc.crc16kermit(resultBuffer) % 256 + resultBinaryData[92] = crc.crc16kermit(newBuffer) / 256 + resultBinaryData[93] = crc.crc16kermit(newBuffer) % 256 this.writeBLECharacteristicValue(resultBuffer) }, // 十六进制字符串转Uint8Array @@ -402,6 +403,15 @@ export default { console.log('commKey', Array.from(that.commKey)) console.log('signKey', Array.from(that.signKey)) } + } else if(binaryData[7] === 0x23) { + const cebBinaryData = binaryData.slice(12, binaryData.length - 2) + console.log('sm4返回参数', Array.from(cebBinaryData)) + const decrypted = sm4.decrypt(cebBinaryData, that.commKey, { mode: 'ecb', output: 'array' }) + console.log('ecb解密后的数据', decrypted) + if(decrypted[0] === 48 && decrypted[1] === 64) { + that.lockStatus = decrypted + console.log('锁状态', that.lockStatus) + } } }, // 监听特征值变化 @@ -418,12 +428,10 @@ export default { success(res) { console.log(res) uni.onBLECharacteristicValueChange(function (res) { - console.log('设备特征值改变') - console.log(res) let binaryData = new Uint8Array(res.value) + console.log('设备返回的数据', Array.from(binaryData), Array.from(binaryData, byte => + byte.toString(16)).join(',')) if(binaryData[0] === 239 && binaryData[1] === 1 && binaryData[2] === 238 && binaryData[3] === 2) { - console.log('设备返回的数据', Array.from(binaryData), Array.from(binaryData, byte => - byte.toString(16)).join(',')) length = binaryData[8] * 256 + binaryData[9] if(length + 14 > binaryData.length) { completeData = binaryData