修改SM4加密算法
This commit is contained in:
parent
3b31e121da
commit
83daf04a11
@ -42,7 +42,7 @@ class BlueManage{
|
||||
if(device.name.isEmpty){
|
||||
return;
|
||||
}
|
||||
|
||||
// print("startScanDevice:${device}");
|
||||
if (((device.serviceUuids.isNotEmpty ? device.serviceUuids[0] : "").toString().contains("758824")) && (device.rssi >= -100)) {
|
||||
// print("11111111111111111:${device}");
|
||||
final knownDeviceIndex = _scanDevices.indexWhere((d) => d.id == device.id);
|
||||
@ -145,7 +145,7 @@ class BlueManage{
|
||||
|
||||
// 写入
|
||||
Future<void> writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List<int> 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 hexStr:${radixString(value)}');
|
||||
|
||||
try {
|
||||
List<int> valueList = value;
|
||||
|
||||
@ -8,6 +8,8 @@ import 'io_sender.dart';
|
||||
import 'io_type.dart';
|
||||
|
||||
import 'package:crypto/crypto.dart' as a;
|
||||
import 'package:convert/convert.dart';
|
||||
import 'package:sm_crypto/sm_crypto.dart';
|
||||
|
||||
class GetPrivateKeyCommand extends SenderProtocol {
|
||||
|
||||
@ -30,6 +32,17 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
List<int> ebcData = [];
|
||||
print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
|
||||
|
||||
// 指令类型
|
||||
int type = commandType!.typeValue;
|
||||
double typeDouble = type/256;
|
||||
int type1 = typeDouble.toInt();
|
||||
int type2 = type%256;
|
||||
data.add(type1);
|
||||
data.add(type2);
|
||||
print("type:$type");
|
||||
print("type1:$type1");
|
||||
print("type2:$type2");
|
||||
|
||||
// 锁id
|
||||
int lockIDLength = utf8.encode(lockID!).length;
|
||||
data.addAll(utf8.encode(lockID!));
|
||||
@ -59,6 +72,12 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
}else{
|
||||
List<int> authCodeData = [];
|
||||
|
||||
//KeyID
|
||||
// authCodeData.addAll(utf8.encode(keyID!));
|
||||
//
|
||||
// //authUserID
|
||||
// authCodeData.addAll(utf8.encode(authUserID!));
|
||||
|
||||
//KeyID
|
||||
authCodeData.addAll(utf8.encode(keyID!));
|
||||
|
||||
@ -74,22 +93,30 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
authCodeData.add((d1 & 0xff00) >> 8);
|
||||
authCodeData.add((d1 & 0xff));
|
||||
|
||||
var pubKey = Storage.getData("bluePublicKey");
|
||||
List<int> pubKeyData = utf8.encode(pubKey.toString());
|
||||
// var pubKey = Storage.getData("bluePublicKey");
|
||||
var pubKey = "ovOvAHuL5+dBCw7L3Qt7IQ==";
|
||||
List<int> pubKeyData = base64.decode(pubKey);
|
||||
authCodeData.addAll(pubKeyData);
|
||||
|
||||
// var stringEncoded = md5Crypto(authCodeData);
|
||||
// data.add(stringEncoded.length);
|
||||
|
||||
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
||||
var authCode = a.md5.convert(authCodeData);
|
||||
print("authCodeData:$authCodeData authCode:$authCode");
|
||||
print("authCodeData:$authCodeData \nauthCode:$authCode \nauthCodeBytes:${authCode.bytes}");
|
||||
|
||||
data.add(authCode.bytes.length);
|
||||
data.addAll(authCode.bytes);
|
||||
|
||||
print("authCode:$authCode authCode.bytes.length:${authCode.bytes.length} data:$data ebcData$ebcData");
|
||||
}
|
||||
ebcData = getDNSAPIStr(data, "TMH_c3570480da8d").bytes;
|
||||
|
||||
if((data.length % 16) != 0){
|
||||
int add = (16 - data.length % 16);
|
||||
for(int i = 0; i<add; i++){
|
||||
data.add(0);
|
||||
}
|
||||
}
|
||||
print("SM4Data:$data");
|
||||
// 拿到数据之后通过LockId进行SM4 ECB加密
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,18 @@ class GetPublicKeyCommand extends SenderProtocol {
|
||||
@override
|
||||
List<int> messageDetail() {
|
||||
List<int> data = [];
|
||||
|
||||
// 指令类型
|
||||
int type = commandType!.typeValue;
|
||||
double typeDouble = type/256;
|
||||
int type1 = typeDouble.toInt();
|
||||
int type2 = type%256;
|
||||
data.add(type1);
|
||||
data.add(type2);
|
||||
print("type:$type");
|
||||
print("type1:$type1");
|
||||
print("type2:$type2");
|
||||
|
||||
print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
|
||||
int length = utf8.encode(lockID!).length;
|
||||
data.addAll(utf8.encode(lockID!));
|
||||
|
||||
@ -31,11 +31,11 @@ abstract class SenderProtocol extends IOData {
|
||||
|
||||
// 帧头
|
||||
commandList.addAll(header);
|
||||
print("header:$header");
|
||||
// print("header:$header");
|
||||
|
||||
//包类型
|
||||
commandList.add(ask); //包类型
|
||||
print("ask:$ask");
|
||||
// print("ask:$ask");
|
||||
|
||||
// 包序号
|
||||
int commandIndexChange = _commandIndex!;
|
||||
@ -44,11 +44,11 @@ abstract class SenderProtocol extends IOData {
|
||||
int commandIndexChang2 = commandIndexChange%256;
|
||||
commandList.add(commandIndexChang1);
|
||||
commandList.add(commandIndexChang2);
|
||||
print("_commandIndex:$_commandIndex commandIndexChang1$commandIndexChang1 commandIndexChang2:$commandIndexChang2");
|
||||
// print("_commandIndex:$_commandIndex commandIndexChang1$commandIndexChang1 commandIndexChang2:$commandIndexChang2");
|
||||
|
||||
// 包标识
|
||||
commandList.add(identifier);
|
||||
print("identifier:$identifier");
|
||||
// print("identifier:$identifier");
|
||||
|
||||
// 数据长度
|
||||
// int dataLength = dataSourceLength();
|
||||
@ -61,20 +61,9 @@ abstract class SenderProtocol extends IOData {
|
||||
commandList.add(dataLength.toInt());
|
||||
commandList.add(42%256);
|
||||
|
||||
// 指令类型
|
||||
int type = commandType!.typeValue;
|
||||
double typeDouble = type/256;
|
||||
int type1 = typeDouble.toInt();
|
||||
int type2 = type%256;
|
||||
commandList.add(type1);
|
||||
commandList.add(type2);
|
||||
print("type:$type");
|
||||
print("type1:$type1");
|
||||
print("type2:$type2");
|
||||
|
||||
// 数据块
|
||||
commandList.addAll(commandData!); //数据块
|
||||
print("commandData:$commandData");
|
||||
// print("commandData:$commandData");
|
||||
|
||||
// 校验位
|
||||
var mcrc = crc_16(commandList);
|
||||
@ -83,9 +72,9 @@ abstract class SenderProtocol extends IOData {
|
||||
int mcrcDouble2 = mcrc%256;
|
||||
commandList.add(mcrcDouble1); //帧尾
|
||||
commandList.add(mcrcDouble2);
|
||||
print("mcrc:$mcrc");
|
||||
print("mcrcDouble1:$mcrcDouble1");
|
||||
print("mcrcDouble2:$mcrcDouble2");
|
||||
// print("mcrc:$mcrc");
|
||||
// print("mcrcDouble1:$mcrcDouble1");
|
||||
// print("mcrcDouble2:$mcrcDouble2");
|
||||
return commandList;
|
||||
}
|
||||
|
||||
|
||||
@ -94,4 +94,4 @@ List<int> EncryptRoundKeys(key) {
|
||||
// }
|
||||
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,25 +4,29 @@ import 'dart:typed_data';
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:encrypt/encrypt.dart' as ddd;
|
||||
import 'package:sm_crypto/sm_crypto.dart';
|
||||
|
||||
ddd.Encrypted getDNSAPIStr(List<int>data, String key) {
|
||||
// final key = Key.fromBase64('BwwfHxgKDwcXAxkWDwEHDBseIREPIA4QDxYOEBIDIRY=');
|
||||
final iv = ddd.IV.fromLength(16);
|
||||
final encrypter = ddd.Encrypter(ddd.AES(ddd.Key.fromUtf8(key), mode: ddd.AESMode.ecb, padding: 'Zero'));
|
||||
final encrypted = encrypter.encrypt(key, iv: iv);
|
||||
String getSM4Str(List<int>data, String key) {
|
||||
String dataStr = radixString(data);
|
||||
String iv = SM4.createHexKey(key: key);
|
||||
|
||||
return encrypted;
|
||||
String cbcEncryptData = SM4.encrypt(
|
||||
data: dataStr,
|
||||
key: iv,
|
||||
mode: SM4CryptoMode.ECB,
|
||||
iv: iv,
|
||||
);
|
||||
|
||||
print("getDNSAPIStrData:$data \ngetDNSAPIStrkey:$key \ngetDNSAPIStrDataStr:$dataStr \niv:$iv\ncbcEncryptData:$cbcEncryptData");
|
||||
return cbcEncryptData;
|
||||
|
||||
// final iv = ddd.IV.fromLength(32);
|
||||
// final encrypter = ddd.Encrypter(ddd.AES(ddd.Key.fromUtf8(key), mode: ddd.AESMode.ecb, padding: 'PKCS7'));
|
||||
// final encrypted = encrypter.encrypt(key, iv: iv);
|
||||
//
|
||||
// return encrypted;
|
||||
}
|
||||
|
||||
// static setDNSAPIStr(String value) {
|
||||
// final key = Key.fromUtf8(RKNetworkManager().mSeeSkyLocalPrivateKey);
|
||||
// final iv = IV.fromUtf8(RKNetworkManager().mSeeSkyLocalPrivateKey);
|
||||
// final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: "PKCS7"));
|
||||
// final encrypt = encrypter.encrypt(value, iv: iv);
|
||||
//
|
||||
// setCacheString(RKEnum.CACHE_STRING_APISTRURL, encrypt.base16);
|
||||
// }
|
||||
|
||||
String md5Crypto(List<int> data){
|
||||
final dig = md5.convert(data);
|
||||
var keyStr = dig.toString();
|
||||
@ -40,9 +44,9 @@ List<int> getFixedLengthList(List<int>data, int length) {
|
||||
//int ---> 指定长度的hex (如指定长度为6的情况,0x000001 0x001234, 0xefab23)
|
||||
String intToFormatHex(int num, int length) {
|
||||
String hexString = num.toRadixString(16);
|
||||
print("hexString=$hexString");
|
||||
// print("hexString=$hexString");
|
||||
String formatString = hexString.padLeft(length, "0");
|
||||
print("formatHexString=$formatString");
|
||||
// print("formatHexString=$formatString");
|
||||
return formatString;
|
||||
}
|
||||
|
||||
@ -214,52 +218,6 @@ List<List<T>> splitList<T>(List<T> list, int len) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//TODO:获取数据包分包后包的数量
|
||||
// int getPackageCount(List<int> data, {required int averageLen}) {
|
||||
// int len = data.length;
|
||||
// return len % averageLen > 0 ? (len ~/ averageLen + 1) : len ~/ averageLen;
|
||||
// }
|
||||
//
|
||||
// List<int> getSubData(
|
||||
// {required int index, required int average, required List<int> data}) {
|
||||
// if (data.isEmpty) {
|
||||
// return [];
|
||||
// }
|
||||
// int totalLength = data.length;
|
||||
// if (average >= totalLength) {
|
||||
// return data;
|
||||
// }
|
||||
//
|
||||
// if (index * average > totalLength) {
|
||||
// if ((index - 1) * average > totalLength) {
|
||||
// return [];
|
||||
// } else {
|
||||
// int tempCount = average * (index - 1);
|
||||
// return data.sublist(tempCount, totalLength);
|
||||
// }
|
||||
// } else {
|
||||
// return data.sublist(average * (index - 1), average * index);
|
||||
// }
|
||||
// }
|
||||
|
||||
// List<int> reverseList(List<int> srcData) {
|
||||
// print('srcData:$srcData'); //[1,3]
|
||||
// int srcLen = srcData.length;
|
||||
// int halfLen = srcLen ~/ 2;
|
||||
// List<int> dstData = [];
|
||||
// dstData.addAll(srcData);
|
||||
// for (int i = 0; i < halfLen; i++) {
|
||||
// int begin = i;
|
||||
// int end = srcLen - i - 1;
|
||||
// List<int> beginData = srcData.sublist(begin, begin + 1);
|
||||
// List<int> endData = srcData.sublist(end, end + 1);
|
||||
// dstData.replaceRange(begin, begin + 1, endData);
|
||||
// dstData.replaceRange(end, end + 1, beginData);
|
||||
// }
|
||||
// print('dstData:$dstData');
|
||||
// return dstData;
|
||||
// }
|
||||
|
||||
//TODO:int->两个字节 List 高字节在前,低字节在后(小端存储) 本工程只有配置 WiFi用到的!!!!
|
||||
List<int> intToByte2ListLow(int value) => [value >> 8, value];
|
||||
|
||||
|
||||
@ -44,7 +44,6 @@ class CommandSenderManager {
|
||||
}
|
||||
|
||||
void _sendNormalData(List<int> data) async {
|
||||
print("lllllll:${data}");
|
||||
if (data.isNotEmpty) {
|
||||
EventBusManager().eventBusFir(EventSendModel(data: data, sendChannel: DataChannel.ble));
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ class NearbyLockLogic extends BaseGetXController{
|
||||
_sendStreamSubscription = EventBusManager().eventBus!.on<EventSendModel>().listen((
|
||||
EventSendModel model) {
|
||||
if (model.sendChannel == DataChannel.ble) {
|
||||
print("fsdfgsdfgsdfgsdfgsdfgsdfg:${BlueManage().qualifiedCharacteristic}");
|
||||
// print("fsdfgsdfgsdfgsdfgsdfgsdfg:${BlueManage().qualifiedCharacteristic}");
|
||||
BlueManage().writeCharacteristicWithResponse(QualifiedCharacteristic(
|
||||
deviceId:BlueManage().qualifiedCharacteristic!.deviceId,
|
||||
characteristicId: BlueManage().qualifiedCharacteristic!.characteristicId,
|
||||
|
||||
@ -87,6 +87,7 @@ dependencies:
|
||||
#加密解密
|
||||
encrypt: ^5.0.1
|
||||
crypto: ^3.0.3
|
||||
sm_crypto: ^1.0.3
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user