demo添加获取私钥功能
This commit is contained in:
parent
61ccbc125c
commit
4a2394d57f
23
package-lock.json
generated
23
package-lock.json
generated
@ -6,8 +6,10 @@
|
||||
"": {
|
||||
"dependencies": {
|
||||
"crc": "^4.3.2",
|
||||
"js-md5": "^0.8.3",
|
||||
"pinia": "^2.2.0",
|
||||
"pinia-plugin-unistorage": "^0.1.2",
|
||||
"sm-crypto": "^0.3.13",
|
||||
"uview-plus": "^3.3.12"
|
||||
}
|
||||
},
|
||||
@ -134,6 +136,18 @@
|
||||
"optional": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/js-md5": {
|
||||
"version": "0.8.3",
|
||||
"resolved": "https://registry.npmjs.org/js-md5/-/js-md5-0.8.3.tgz",
|
||||
"integrity": "sha512-qR0HB5uP6wCuRMrWPTrkMaev7MJZwJuuw4fnwAzRgP4J4/F8RwtodOKpGp4XpqsLBFzzgqIO42efFAyz2Et6KQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/jsbn": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-1.1.0.tgz",
|
||||
"integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/pinia": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/pinia/-/pinia-2.2.0.tgz",
|
||||
@ -172,6 +186,15 @@
|
||||
"integrity": "sha512-OwpTSOfy6xSs1+pwcNrv0RBMOzI39Lp3qQKUTPVVPRjCdNa5JH/oPRiqsesIskK8TVgmRiHwO4KXlV2Li9dANA==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/sm-crypto": {
|
||||
"version": "0.3.13",
|
||||
"resolved": "https://registry.npmjs.org/sm-crypto/-/sm-crypto-0.3.13.tgz",
|
||||
"integrity": "sha512-ztNF+pZq6viCPMA1A6KKu3bgpkmYti5avykRHbcFIdSipFdkVmfUw2CnpM2kBJyppIalqvczLNM3wR8OQ0pT5w==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"jsbn": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/tiny-emitter": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.1.0.tgz",
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"crc": "^4.3.2",
|
||||
"js-md5": "^0.8.3",
|
||||
"pinia": "^2.2.0",
|
||||
"pinia-plugin-unistorage": "^0.1.2",
|
||||
"sm-crypto": "^0.3.13",
|
||||
"uview-plus": "^3.3.12"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
<template>
|
||||
<view>
|
||||
<button @click="getList">获取设备列表</button>
|
||||
<button @click="stopGetList">停止获取</button>
|
||||
<view v-if="showList">
|
||||
<button class="device" v-for="item in list"
|
||||
:key="item.deviceId" @click="connect(item)">{{item.name}}
|
||||
@ -10,28 +9,27 @@
|
||||
</view>
|
||||
<view v-else>
|
||||
<button @click="getPublicKey">获取锁公钥</button>
|
||||
<button @click="getTest">获取锁私钥</button>
|
||||
<button @click="getCommKey">获取锁私钥</button>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import crc from 'crc'
|
||||
import { md5 } from 'js-md5'
|
||||
import { sm4 } from 'sm-crypto'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
list: [],
|
||||
getting: false,
|
||||
// deviceId: '6C3CD148-F456-1C61-F35E-C176B0CC644A',
|
||||
// serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB',
|
||||
// characteristicId1: '0000FFF1-0000-1000-8000-00805F9B34FB',
|
||||
// characteristicId2: '0000FFF2-0000-1000-8000-00805F9B34FB',
|
||||
deviceId: '',
|
||||
serviceId: '',
|
||||
characteristicId1: '',
|
||||
characteristicId2: '',
|
||||
lockId: '',
|
||||
publicKey: new Uint8Array(16),
|
||||
showList: true,
|
||||
number: 1
|
||||
}
|
||||
@ -61,10 +59,54 @@ export default {
|
||||
})
|
||||
},
|
||||
methods: {
|
||||
GetCommKey() {
|
||||
const userId = 294
|
||||
// 十六进制字符串转Uint8Array
|
||||
hexToUint8Array(hex) {
|
||||
if (hex.length % 2 !== 0) {
|
||||
throw new Error('Invalid hex string')
|
||||
}
|
||||
const array = new Uint8Array(hex.length / 2)
|
||||
for (let i = 0; i < hex.length; i += 2) {
|
||||
array[i / 2] = parseInt(hex.substr(i, 2), 16)
|
||||
}
|
||||
return array
|
||||
},
|
||||
// 获取私钥
|
||||
getCommKey() {
|
||||
const cmdId = 0x3091
|
||||
const keyId = '0'
|
||||
const authUserId = '294'
|
||||
const nowTime = parseInt(new Date().getTime() / 1000)
|
||||
|
||||
let buffer = new ArrayBuffer(54)
|
||||
const originAuthCodeLen = keyId.length + authUserId.length + 4 + 16
|
||||
|
||||
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[keyId.length + i] = authUserId.charCodeAt(i)
|
||||
}
|
||||
const number = keyId.length + authUserId.length
|
||||
authCodeBinaryData[number + 0] = (nowTime & 0xff000000) >> 24
|
||||
authCodeBinaryData[number + 1] = (nowTime & 0xff0000) >> 16
|
||||
authCodeBinaryData[number + 2] = (nowTime & 0xff00) >> 8
|
||||
authCodeBinaryData[number + 3] = (nowTime & 0xff)
|
||||
|
||||
for(let i=0; i < 16; i++){
|
||||
authCodeBinaryData[number + 4 + i] = this.publicKey[i]
|
||||
}
|
||||
|
||||
console.log('md5前的数据', Array.from(authCodeBinaryData, byte =>
|
||||
byte.toString(10)).join(','))
|
||||
const codeMd5 = md5(authCodeBinaryData)
|
||||
console.log('md5后的数据', codeMd5, codeMd5.length)
|
||||
|
||||
const bufferLength = codeMd5.length + 12 + 107
|
||||
|
||||
let buffer = new ArrayBuffer(bufferLength)
|
||||
let binaryData = new Uint8Array(buffer)
|
||||
|
||||
// 固定包头
|
||||
@ -82,8 +124,82 @@ export default {
|
||||
this.number++
|
||||
|
||||
// 包标识
|
||||
binaryData[7] = 0x22
|
||||
|
||||
// 数据长度
|
||||
binaryData[10] = (codeMd5.length + 107) / 256
|
||||
binaryData[11] = (codeMd5.length + 107) % 256
|
||||
|
||||
// 命令
|
||||
binaryData[12] = 0x3091 / 256
|
||||
binaryData[13] = 0x3091 % 256
|
||||
|
||||
for(let i=0; i < this.lockId.length; i++){
|
||||
binaryData[14 + i] = this.lockId.charCodeAt(i)
|
||||
}
|
||||
let flagNumber = 14 + 40
|
||||
for (let i = 0; i < keyId.length; i++) {
|
||||
binaryData[flagNumber + i] = keyId.charCodeAt(i)
|
||||
}
|
||||
flagNumber += 40
|
||||
for (let i = 0; i < authUserId.length; i++) {
|
||||
binaryData[flagNumber + i] = authUserId.charCodeAt(i)
|
||||
}
|
||||
|
||||
flagNumber += 20
|
||||
binaryData[flagNumber + 0] = (nowTime & 0xff000000) >> 24
|
||||
binaryData[flagNumber + 1] = (nowTime & 0xff0000) >> 16
|
||||
binaryData[flagNumber + 2] = (nowTime & 0xff00) >> 8
|
||||
binaryData[flagNumber + 3] = (nowTime & 0xff)
|
||||
|
||||
flagNumber += 4
|
||||
binaryData[flagNumber] = codeMd5.length
|
||||
flagNumber += 1
|
||||
for (let i = 0; i < codeMd5.length; i++) {
|
||||
binaryData[flagNumber + i] = codeMd5.charCodeAt(i)
|
||||
}
|
||||
|
||||
binaryData[8] = Math.ceil((bufferLength - 12)/16) * 16 / 256
|
||||
binaryData[9] = Math.ceil((bufferLength - 12)/16) * 16 % 256
|
||||
|
||||
let cebBuffer = new ArrayBuffer(bufferLength - 12)
|
||||
let cebBinaryData = new Uint8Array(cebBuffer)
|
||||
|
||||
for(let i = 0; i < bufferLength - 12; i++){
|
||||
cebBinaryData[i] = binaryData[i + 12]
|
||||
}
|
||||
|
||||
const key = new Uint8Array(16)
|
||||
for (let i = 0; i < this.lockId.length; i++) {
|
||||
key[i] = this.lockId.charCodeAt(i)
|
||||
}
|
||||
|
||||
console.log('未加密的数据', Array.from(cebBinaryData))
|
||||
const encrypted = sm4.encrypt(cebBinaryData, key, { mode: 'ecb' })
|
||||
const newBinaryData = this.hexToUint8Array(encrypted)
|
||||
console.log('ecb加密后的数据', Array.from(newBinaryData))
|
||||
|
||||
const encrypteBuffer = new ArrayBuffer(12 + newBinaryData.length)
|
||||
const encrypteBinaryData = new Uint8Array(encrypteBuffer)
|
||||
for(let i = 0; i< 12; i++){
|
||||
encrypteBinaryData[i] = binaryData[i]
|
||||
}
|
||||
for(let i = 12; i < encrypteBinaryData.length; i++){
|
||||
encrypteBinaryData[i] = newBinaryData[i - 12]
|
||||
}
|
||||
console.log('crc前的数据', Array.from(encrypteBinaryData))
|
||||
|
||||
const resultBuffer = new ArrayBuffer(encrypteBinaryData.length + 2)
|
||||
const resultBinaryData = new Uint8Array(resultBuffer)
|
||||
for(let i = 0; i< encrypteBinaryData.length; i++){
|
||||
resultBinaryData[i] = encrypteBinaryData[i]
|
||||
}
|
||||
resultBinaryData[encrypteBinaryData.length] = crc.crc16kermit(encrypteBuffer) / 256
|
||||
resultBinaryData[1 + encrypteBinaryData.length] = crc.crc16kermit(encrypteBuffer) % 256
|
||||
|
||||
this.writeBLECharacteristicValue(resultBuffer)
|
||||
},
|
||||
// 获取公钥
|
||||
getPublicKey() {
|
||||
let buffer = new ArrayBuffer(54)
|
||||
let binaryData = new Uint8Array(buffer)
|
||||
@ -131,6 +247,7 @@ export default {
|
||||
newBinaryData[55] = crc.crc16kermit(buffer) % 256
|
||||
this.writeBLECharacteristicValue(newBuffer)
|
||||
},
|
||||
// 写入输入
|
||||
writeBLECharacteristicValue(data) {
|
||||
const that = this
|
||||
console.log('写入设备的设备 ID',that.deviceId)
|
||||
@ -140,9 +257,7 @@ export default {
|
||||
|
||||
let Uint8ArrayData = new Uint8Array(data)
|
||||
|
||||
const hexString = Array.from(Uint8ArrayData, byte =>
|
||||
byte.toString(10)).join(',')
|
||||
console.log('未加工的数据', hexString)
|
||||
console.log('未加工的数据', Array.from(Uint8ArrayData))
|
||||
|
||||
for(let i = 0; i < Math.ceil(data.byteLength / 20); i++){
|
||||
let buffer = new ArrayBuffer(20)
|
||||
@ -170,6 +285,7 @@ export default {
|
||||
})
|
||||
}
|
||||
},
|
||||
// 监听特征值变化
|
||||
notifyBLECharacteristicValueChange() {
|
||||
const that = this
|
||||
uni.notifyBLECharacteristicValueChange({
|
||||
@ -184,13 +300,29 @@ export default {
|
||||
console.log('设备特征值改变')
|
||||
console.log(res)
|
||||
let binaryData = new Uint8Array(res.value)
|
||||
const hexString = Array.from(binaryData, byte =>
|
||||
byte.toString(10)).join(',')
|
||||
console.log(hexString)
|
||||
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
|
||||
}
|
||||
} 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
// 连接蓝牙设备
|
||||
connect(item) {
|
||||
const that = this
|
||||
if(item.pair) {
|
||||
@ -251,6 +383,7 @@ export default {
|
||||
}
|
||||
})
|
||||
},
|
||||
// 监听蓝牙设备列表变化
|
||||
startGetList() {
|
||||
const that = this
|
||||
this.getting = true
|
||||
@ -265,15 +398,7 @@ export default {
|
||||
}
|
||||
})
|
||||
},
|
||||
stopGetList(){
|
||||
const that = this
|
||||
uni.stopBluetoothDevicesDiscovery({
|
||||
success: function () {
|
||||
that.getting = false
|
||||
console.log('停止获取设备列表成功')
|
||||
}
|
||||
})
|
||||
},
|
||||
// 获取蓝牙设备列表
|
||||
getList() {
|
||||
const that = this
|
||||
uni.getBluetoothDevices({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user