diff --git a/pages/demo/demo.vue b/pages/demo/demo.vue index f57f0c7..158eb9b 100644 --- a/pages/demo/demo.vue +++ b/pages/demo/demo.vue @@ -30,6 +30,8 @@ export default { characteristicId2: '', lockId: '', publicKey: new Uint8Array(16), + commKey: new Uint8Array(16), + signKey: new Uint8Array(16), showList: true, number: 1 } @@ -82,13 +84,14 @@ export default { let authCodeBuffer = new ArrayBuffer(originAuthCodeLen) let authCodeBinaryData = new Uint8Array(authCodeBuffer) - for(let i=0; i < keyId.length; i++){ - authCodeBinaryData[i] = keyId.charCodeAt(i) + for(let i=0; i < authUserId.length; i++){ + authCodeBinaryData[i] = authUserId.charCodeAt(i) } - for(let i=0; i < authUserId.length; i++){ - authCodeBinaryData[keyId.length + i] = authUserId.charCodeAt(i) + for(let i=0; i < keyId.length; i++){ + authCodeBinaryData[authUserId.length + i] = keyId.charCodeAt(i) } + const number = keyId.length + authUserId.length authCodeBinaryData[number + 0] = (nowTime & 0xff000000) >> 24 authCodeBinaryData[number + 1] = (nowTime & 0xff0000) >> 16 @@ -99,9 +102,11 @@ export default { authCodeBinaryData[number + 4 + i] = this.publicKey[i] } + console.log('钥ID', keyId, '用户ID', authUserId, '时间戳', nowTime,'公钥', Array.from(this.publicKey)) console.log('md5前的数据', Array.from(authCodeBinaryData, byte => byte.toString(10)).join(',')) - const codeMd5 = md5(authCodeBinaryData) + const md5Hex = md5(authCodeBinaryData) + const codeMd5 = new Uint8Array(md5Hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))) console.log('md5后的数据', codeMd5, codeMd5.length) const bufferLength = codeMd5.length + 12 + 107 @@ -156,7 +161,7 @@ export default { binaryData[flagNumber] = codeMd5.length flagNumber += 1 for (let i = 0; i < codeMd5.length; i++) { - binaryData[flagNumber + i] = codeMd5.charCodeAt(i) + binaryData[flagNumber + i] = codeMd5[i] } binaryData[8] = Math.ceil((bufferLength - 12)/16) * 16 / 256 @@ -300,23 +305,32 @@ export default { console.log('设备特征值改变') console.log(res) let binaryData = new Uint8Array(res.value) - console.log('ecb解密前的数据', Array.from(binaryData)) - // 明文 - if(binaryData[7] === 0x20) { - if(binaryData[12] === 48 && binaryData[13] === 144){ - const publicKey = binaryData.slice(15, 31) - console.log('公钥', Array.from(publicKey)) - that.publicKey = publicKey + 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(',')) + // 明文 0x20 SM4事先约定密钥 0x22 + if(binaryData[7] === 0x20) { + if(binaryData[12] === 48 && binaryData[13] === 144){ + const publicKey = binaryData.slice(15, 31) + console.log('公钥', Array.from(publicKey)) + that.publicKey = publicKey + } + } else if(binaryData[7] === 0x22) { + const cebBinaryData = binaryData.slice(12, binaryData.length - 2) + console.log('sm4返回参数', Array.from(cebBinaryData)) + const key = new Uint8Array(16) + for (let i = 0; i < that.lockId.length; i++) { + key[i] = that.lockId.charCodeAt(i) + } + const decrypted = sm4.decrypt(cebBinaryData, key, { mode: 'ecb', output: 'array' }) + console.log('ecb解密后的数据', decrypted) + if(decrypted[0] === 48 && decrypted[1] === 145) { + that.commKey = decrypted.slice(3, 19) + that.signKey = decrypted.slice(19, 35) + console.log('commKey', Array.from(that.commKey)) + console.log('signKey', Array.from(that.signKey)) + } } - } else if(binaryData[7] === 0x22) { - const cebBinaryData = binaryData.slice(12, 28) - console.log('sm4返回参数', Array.from(cebBinaryData)) - const key = new Uint8Array(16) - for (let i = 0; i < that.lockId.length; i++) { - key[i] = that.lockId.charCodeAt(i) - } - const decrypted = sm4.decrypt(cebBinaryData, key, { mode: 'ecb', output: 'array' }) - console.log('ecb解密后的数据', decrypted) } }) } @@ -359,6 +373,18 @@ export default { } that.showList = false that.notifyBLECharacteristicValueChange() + if(uni.$u.os() === 'android') { + uni.setBLEMTU({ + deviceId: that.deviceId, + mtu: 256, + success() { + console.log('mtu设置成功') + }, + fail(res) { + console.log('mtu设置失败', res) + } + }) + } uni.showToast({ title: '数据获取成功', icon: 'none',