diff --git a/star_lock/lib/blue/blue_manage.dart b/star_lock/lib/blue/blue_manage.dart index d1b76427..52ae43c3 100644 --- a/star_lock/lib/blue/blue_manage.dart +++ b/star_lock/lib/blue/blue_manage.dart @@ -145,7 +145,7 @@ class BlueManage{ // 写入 Future writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List value) async { - // print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value hexStr:${radixString(value)}'); + print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value \nhexStr:${radixString(value)}'); try { List valueList = value; diff --git a/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart b/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart index 0215ef78..d9a35c33 100644 --- a/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart +++ b/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart @@ -29,7 +29,7 @@ class GetPrivateKeyCommand extends SenderProtocol { List messageDetail() { List data = []; List ebcData = []; - print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}"); + // print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}"); // 指令类型 int type = commandType!.typeValue; @@ -38,9 +38,9 @@ class GetPrivateKeyCommand extends SenderProtocol { int type2 = type % 256; data.add(type1); data.add(type2); - print("type:$type"); - print("type1:$type1"); - print("type2:$type2"); + // print("type:$type"); + // print("type1:$type1"); + // print("type2:$type2"); // 锁id int lockIDLength = utf8.encode(lockID!).length; @@ -71,12 +71,6 @@ class GetPrivateKeyCommand extends SenderProtocol { } else { List authCodeData = []; - //KeyID - // authCodeData.addAll(utf8.encode(keyID!)); - // - // //authUserID - // authCodeData.addAll(utf8.encode(authUserID!)); - //KeyID authCodeData.addAll(utf8.encode(keyID!)); @@ -92,15 +86,16 @@ class GetPrivateKeyCommand extends SenderProtocol { authCodeData.add((d1 & 0xff00) >> 8); authCodeData.add((d1 & 0xff)); - // var pubKey = Storage.getData("bluePublicKey"); - var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ=="; - List pubKeyData = base64.decode(pubKey); + var pubKey = Storage.getData("bluePublicKey"); + // var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ=="; + print("pubKey:$pubKey"); + List pubKeyData = base64.decode(pubKey.toString()); + print("pubKeyData:$pubKeyData"); authCodeData.addAll(pubKeyData); // 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode var authCode = a.md5.convert(authCodeData); - print( - "authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}"); + // print("authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}"); data.add(authCode.bytes.length); data.addAll(authCode.bytes); @@ -116,9 +111,9 @@ class GetPrivateKeyCommand extends SenderProtocol { // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 String key = SM4.createHexKey(key: 'TMH_c3570480da8d'); ebcData = SM4.encrypt(data, key: key, mode: SM4CryptoMode.ECB); + ebcData.removeRange(ebcData.length - 16, ebcData.length); // ebcData = utf8.encode(getSM4Str(data, "TMH_c3570480da8d")); print("ebcData:$ebcData"); - // String cbcEncryptData = SM4.encrypt(data: data, key: "TMH_c3570480da8d", mode: SM4CryptoMode.CBC, iv: iv,); return ebcData; } } @@ -127,13 +122,23 @@ class GetPrivateKeyReply extends Reply { GetPrivateKeyReply.parseData(CommandType commandType, List dataDetail) : super.parseData(commandType, dataDetail) { print('获取私钥'); - int index = 0; - // while(index < endIndex){ - // commandKey = byteUInt8(dataDetail, index); - // index += offset_1; - // switch(commandKey){ - // - // } - // } + switch(dataDetail[0]){ + case 0x00: + //成功 + + break; + case 0x07: + //无权限 + + break; + case 0x0f: + //用户已存在 + + break; + default: + //失败 + + break; + } } } diff --git a/star_lock/lib/blue/io_protocol/io_getPublicKey.dart b/star_lock/lib/blue/io_protocol/io_getPublicKey.dart index ad72c2f5..113dceb4 100644 --- a/star_lock/lib/blue/io_protocol/io_getPublicKey.dart +++ b/star_lock/lib/blue/io_protocol/io_getPublicKey.dart @@ -42,12 +42,12 @@ class GetPublicKeyCommand extends SenderProtocol { class GetPublicKeyReply extends Reply { GetPublicKeyReply.parseData(CommandType commandType, List dataDetail) : super.parseData(commandType, dataDetail) { - var tokenData = dataDetail.sublist(1); - print('获取公钥:$tokenData'); - switch(dataDetail[0]){ + var tokenData = dataDetail.sublist(3); + print('获取公钥:dataDetail:$dataDetail tokenData:$tokenData'); + switch(dataDetail[2]){ case 0x00: //成功 - var stringEncoded = base64.encode(tokenData); + String stringEncoded = base64.encode(tokenData); print('获取公钥成功 publickey:$stringEncoded'); // 储存公钥 Storage.setData("bluePublicKey", stringEncoded); diff --git a/star_lock/lib/blue/io_protocol/io_sender.dart b/star_lock/lib/blue/io_protocol/io_sender.dart index b3b050be..60cd5333 100644 --- a/star_lock/lib/blue/io_protocol/io_sender.dart +++ b/star_lock/lib/blue/io_protocol/io_sender.dart @@ -15,7 +15,8 @@ abstract class SenderProtocol extends IOData { final List header = [0XEF, 0X01, 0XEE, 0X02]; //帧头 固定取值 0XEF01EE02,长度 4 字节 final int ask = 0X01 ; // 包类型:0X01 表示请求包,0X11 表示应答包,长度 1 字节 int? _commandIndex = 1; //包序号 - final int identifier = 0x20; // 高 4 位表示包版本,低 4 位用来指示后面数据的加密类型,长度为 1 字节,加密类型取值说明,0:明文,1:AES128,2:SM4(事先约定密钥),3:SM4(设备指定密钥) + + int identifier = 0x20; // 高 4 位表示包版本,低 4 位用来指示后面数据的加密类型,长度为 1 字节,加密类型取值说明,0:明文,1:AES128,2:SM4(事先约定密钥),3:SM4(设备指定密钥) List? commandData = []; //数据块 // final int? tail = 0xFF; //用来校验包的完整性,采用 CRC 校验方法,长度为 2 个字节 @@ -47,19 +48,28 @@ abstract class SenderProtocol extends IOData { // print("_commandIndex:$_commandIndex commandIndexChang1$commandIndexChang1 commandIndexChang2:$commandIndexChang2"); // 包标识 + // 指令类型 + int type = commandType!.typeValue; + if(type == 0x3090){ + // 不加密 + identifier = 0x20; + }else if(type == 0x3091){ + // 不加密 + identifier = 0x22; + } commandList.add(identifier); // print("identifier:$identifier"); // 数据长度 - // int dataLength = dataSourceLength(); + int dataLen = dataSourceLength(); // commandList.add(dataLength); - // print("dataLength:$dataLength"); - var dataLen = 42; + print("dataLen:$dataLen"); + // var dataLen = 42; double dataLength = dataLen/256; commandList.add(dataLength.toInt()); - commandList.add(42%256); + commandList.add(dataLen%256); commandList.add(dataLength.toInt()); - commandList.add(42%256); + commandList.add(dataLen%256); // 数据块 commandList.addAll(commandData!); //数据块 diff --git a/star_lock/lib/blue/reciver_data.dart b/star_lock/lib/blue/reciver_data.dart index 0c9345c5..fca199c6 100644 --- a/star_lock/lib/blue/reciver_data.dart +++ b/star_lock/lib/blue/reciver_data.dart @@ -1,12 +1,14 @@ import 'io_protocol/io_addUser.dart'; +import 'io_protocol/io_getPrivateKey.dart'; import 'io_protocol/io_getPublicKey.dart'; import 'io_protocol/io_openDoor.dart'; import 'io_protocol/io_reply.dart'; import 'io_protocol/io_type.dart'; import 'io_tool/io_tool.dart'; import 'io_tool/manager_event_bus.dart'; +import 'sm4Encipher/sm4.dart'; class CommandReciverManager { @@ -26,31 +28,24 @@ class CommandReciverManager { print("temType:$tmpType"); var dataLen = data[8] * 256 + data[9];// 高16位用来指示后面数据块内容的长度 var oriLen = data[10] * 256 + data[11];// 低16位用来指示数据加密前的原长度 + print("dataLen:$dataLen oriLen:$oriLen"); List dataList = []; List oriDataList = []; switch(tmpType){ case 0: //不加密 - for (var i = 0; i < oriLen ; i++) { - oriDataList.add(data[12 + i]); - } - parseData(oriDataList).then((value) { - EventBusManager().eventBusFir(value); - }); - break; - case 1: //AES128 - break; - case 2: //SM4(事先约定密钥) - // for (var i = 0; i < dataLen ; i++) { - // dataList.add(data[12 + i]); - // } - //console.log("currCommStru.pairedName = ", currCommStru.pairedName); - // var d_cbc = cbc.decrypt_ecb(dataView, currCommStru.pairLockID, true, "nobase64"); - // console.log("d_cbc = ", d_cbc); - // console.log("oriLen = ", oriLen); // for (var i = 0; i < oriLen ; i++) { - // oriDataView[i] = d_cbc[i]; + // oriDataList.add(data[12 + i]); // } - // console.log("oriDataView = ", oriDataView); + oriDataList = data.sublist(12, 12 + oriLen); + print("oriDataList:$oriDataList"); + break; + case 1: + //AES128 + break; + case 2: + //SM4(事先约定密钥) + String key = SM4.createHexKey(key: 'TMH_c3570480da8d'); + oriDataList = SM4.encrypt(data, key: key, mode: SM4CryptoMode.ECB); break; case 3: //SM4(设备指定密钥) // for (var i = 0; i < dataLen ; i++) { @@ -70,8 +65,10 @@ class CommandReciverManager { break; } + parseData(oriDataList).then((value) { + EventBusManager().eventBusFir(value); + }); } - // print('✅ 执行结束 _buffer:${_buffer.length}'); } @@ -79,7 +76,7 @@ class CommandReciverManager { if(data.isNotEmpty){ var cmd = data[0] * 256 + data[1]; CommandType commandType = ExtensionCommandType.getCommandType(cmd); - data.removeRange(0, 2); + // data.removeRange(0, 2); print("111111data commandType:$commandType data:$data"); var reply; switch(commandType) { @@ -88,6 +85,11 @@ class CommandReciverManager { reply = GetPublicKeyReply.parseData(commandType, data); } break; + case CommandType.getLockPrivateKey: + { + reply = GetPrivateKeyReply.parseData(commandType, data); + } + break; case CommandType.addUser: { reply = AddUserReply.parseData(commandType, data); @@ -101,6 +103,5 @@ class CommandReciverManager { } return reply; } - } } \ No newline at end of file diff --git a/star_lock/lib/blue/sm4Encipher/sm4.dart b/star_lock/lib/blue/sm4Encipher/sm4.dart index c226412d..5c9392be 100644 --- a/star_lock/lib/blue/sm4Encipher/sm4.dart +++ b/star_lock/lib/blue/sm4Encipher/sm4.dart @@ -525,10 +525,18 @@ class SM4 { return output; } - static decrypt(String cipherText, + // static decrypt(String cipherText, + // {String? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) { + // if (key != null) setKey(key); + // List input = SMUtils.hexStringToBytes(cipherText); + // List output = _crypto(input, SM4_DECRYPT, mode, iv); + // return utf8.decode(output); + // } + + static decrypt(List data, {String? key, SM4CryptoMode mode = SM4CryptoMode.ECB, String? iv}) { if (key != null) setKey(key); - List input = SMUtils.hexStringToBytes(cipherText); + List input = data; List output = _crypto(input, SM4_DECRYPT, mode, iv); return utf8.decode(output); }