提交加解密代码
This commit is contained in:
parent
9947d7b8b3
commit
3b31e121da
@ -145,7 +145,7 @@ class BlueManage{
|
|||||||
|
|
||||||
// 写入
|
// 写入
|
||||||
Future<void> writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List<int> value) async {
|
Future<void> writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List<int> value) async {
|
||||||
print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value');
|
print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value hexStr:${radixString(value)}');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<int> valueList = value;
|
List<int> valueList = value;
|
||||||
@ -156,7 +156,7 @@ class BlueManage{
|
|||||||
await _flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: subData[i]).then((value) async {
|
await _flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: subData[i]).then((value) async {
|
||||||
await Future.delayed(const Duration(milliseconds: 1))
|
await Future.delayed(const Duration(milliseconds: 1))
|
||||||
.then((value) async {
|
.then((value) async {
|
||||||
print('成功了么');
|
print('分包发送成功了');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,30 +1,33 @@
|
|||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import '../../tools/storage.dart';
|
||||||
import '../io_tool/io_tool.dart';
|
import '../io_tool/io_tool.dart';
|
||||||
import 'io_reply.dart';
|
import 'io_reply.dart';
|
||||||
import 'io_sender.dart';
|
import 'io_sender.dart';
|
||||||
import 'io_type.dart';
|
import 'io_type.dart';
|
||||||
|
|
||||||
|
import 'package:crypto/crypto.dart' as a;
|
||||||
|
|
||||||
class GetPrivateKeyCommand extends SenderProtocol {
|
class GetPrivateKeyCommand extends SenderProtocol {
|
||||||
|
|
||||||
String? lockID;
|
String? lockID;
|
||||||
String? keyID; // 钥匙ID
|
String? keyID; // 钥匙ID
|
||||||
String? authUserID; // 钥匙授权人ID
|
String? authUserID; // 钥匙授权人ID
|
||||||
int? nowTime;
|
int? nowTime;
|
||||||
String? authCode;
|
|
||||||
int? needAuthor;
|
int? needAuthor;
|
||||||
GetPrivateKeyCommand({
|
GetPrivateKeyCommand({
|
||||||
this.lockID,
|
this.lockID,
|
||||||
this.keyID,
|
this.keyID,
|
||||||
this.authUserID,
|
this.authUserID,
|
||||||
this.nowTime,
|
this.nowTime,
|
||||||
this.authCode,
|
this.needAuthor,
|
||||||
}) : super(CommandType.getLockPrivateKey);
|
}) : super(CommandType.getLockPrivateKey);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<int> messageDetail() {
|
List<int> messageDetail() {
|
||||||
List<int> data = [];
|
List<int> data = [];
|
||||||
|
List<int> ebcData = [];
|
||||||
print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
|
print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
|
||||||
|
|
||||||
// 锁id
|
// 锁id
|
||||||
@ -43,19 +46,51 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
|||||||
data = getFixedLengthList(data, 20 - authUserIDLength);
|
data = getFixedLengthList(data, 20 - authUserIDLength);
|
||||||
|
|
||||||
//NowTime 4
|
//NowTime 4
|
||||||
DateTime now = DateTime.now();
|
// DateTime now = DateTime.now();
|
||||||
int timestamp = now.millisecondsSinceEpoch;
|
// int timestamp = now.millisecondsSinceEpoch;
|
||||||
data.add((timestamp & 0xff000000) >> 24);
|
var d1 = 0x11223344;
|
||||||
data.add((timestamp & 0xff0000) >> 16);
|
data.add((d1 & 0xff000000) >> 24);
|
||||||
data.add((timestamp & 0xff00) >> 8);
|
data.add((d1 & 0xff0000) >> 16);
|
||||||
data.add((timestamp & 0xff));
|
data.add((d1 & 0xff00) >> 8);
|
||||||
|
data.add((d1 & 0xff));
|
||||||
|
|
||||||
if(needAuthor == 0){
|
if(needAuthor == 0){
|
||||||
data.add(0);
|
data.add(0);
|
||||||
}else{
|
}else{
|
||||||
|
List<int> authCodeData = [];
|
||||||
|
|
||||||
|
//KeyID
|
||||||
|
authCodeData.addAll(utf8.encode(keyID!));
|
||||||
|
|
||||||
|
//authUserID
|
||||||
|
authCodeData.addAll(utf8.encode(authUserID!));
|
||||||
|
|
||||||
|
//NowTime 4
|
||||||
|
// DateTime now = DateTime.now();
|
||||||
|
// int timestamp = now.millisecondsSinceEpoch;
|
||||||
|
var d1 = 0x11223344;
|
||||||
|
authCodeData.add((d1 & 0xff000000) >> 24);
|
||||||
|
authCodeData.add((d1 & 0xff0000) >> 16);
|
||||||
|
authCodeData.add((d1 & 0xff00) >> 8);
|
||||||
|
authCodeData.add((d1 & 0xff));
|
||||||
|
|
||||||
|
var pubKey = Storage.getData("bluePublicKey");
|
||||||
|
List<int> pubKeyData = utf8.encode(pubKey.toString());
|
||||||
|
authCodeData.addAll(pubKeyData);
|
||||||
|
|
||||||
|
// var stringEncoded = md5Crypto(authCodeData);
|
||||||
|
// data.add(stringEncoded.length);
|
||||||
|
|
||||||
|
var authCode = a.md5.convert(authCodeData);
|
||||||
|
print("authCodeData:$authCodeData authCode:$authCode");
|
||||||
|
|
||||||
|
data.add(authCode.bytes.length);
|
||||||
|
data.addAll(authCode.bytes);
|
||||||
|
|
||||||
|
print("authCode:$authCode authCode.bytes.length:${authCode.bytes.length} data:$data ebcData$ebcData");
|
||||||
}
|
}
|
||||||
return data;
|
ebcData = getDNSAPIStr(data, "TMH_c3570480da8d").bytes;
|
||||||
|
return ebcData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import 'dart:typed_data';
|
|||||||
|
|
||||||
import '../../tools/storage.dart';
|
import '../../tools/storage.dart';
|
||||||
import '../io_tool/io_tool.dart';
|
import '../io_tool/io_tool.dart';
|
||||||
|
import '../sender_manage.dart';
|
||||||
import 'io_reply.dart';
|
import 'io_reply.dart';
|
||||||
import 'io_sender.dart';
|
import 'io_sender.dart';
|
||||||
import 'io_type.dart';
|
import 'io_type.dart';
|
||||||
@ -38,7 +39,7 @@ class GetPublicKeyReply extends Reply {
|
|||||||
print('获取公钥成功 publickey:$stringEncoded');
|
print('获取公钥成功 publickey:$stringEncoded');
|
||||||
// 储存公钥
|
// 储存公钥
|
||||||
Storage.setData("bluePublicKey", stringEncoded);
|
Storage.setData("bluePublicKey", stringEncoded);
|
||||||
|
IoSenderManage.getPrivateKey("TMH_c3570480da8d", "1", "1", 1, 1);
|
||||||
break;
|
break;
|
||||||
case 0x07:
|
case 0x07:
|
||||||
//无权限
|
//无权限
|
||||||
|
|||||||
@ -34,3 +34,64 @@ class cbc{
|
|||||||
0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc
|
0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ECB加密模式
|
||||||
|
* @example encrypt_cbc("1234", "1234567890123456") => "woPrxebr8Xvyo1qG8QxAUA=="
|
||||||
|
* @param {any} plaintext 要加密的数据
|
||||||
|
* @param {String} key
|
||||||
|
* @param {String} iv
|
||||||
|
* @param {String} mode base64 | "text"
|
||||||
|
* @returns {String} 加密后的字符串
|
||||||
|
*/
|
||||||
|
|
||||||
|
List<int> encryptEcb (plaintext, key, keyIsStr, {mode = "base64"}) {
|
||||||
|
List<int> list = [];
|
||||||
|
//let encryptRoundKeys = EncryptRoundKeys(stringToArray(key));
|
||||||
|
var encryptRoundKeys;
|
||||||
|
// if (keyIsStr) {
|
||||||
|
// encryptRoundKeys = EncryptRoundKeys(stringToArray(key));
|
||||||
|
// } else {
|
||||||
|
// encryptRoundKeys = EncryptRoundKeys(key);
|
||||||
|
// }
|
||||||
|
// let plainByteArray = plaintext; //stringToArray(plaintext);
|
||||||
|
// let padded = padding(plainByteArray);
|
||||||
|
// let blockTimes = padded.length / UINT8_BLOCK;
|
||||||
|
// let outArray = [];
|
||||||
|
// // CBC mode
|
||||||
|
// // init chain with iv (transform to uint32 block)
|
||||||
|
// for (let i = 0; i < blockTimes; i++) {
|
||||||
|
// // extract the 16 bytes block data for this round to encrypt
|
||||||
|
// let roundIndex = i * UINT8_BLOCK;
|
||||||
|
// let block = getChainBlock(padded, roundIndex);
|
||||||
|
// let cipherBlock = doBlockCrypt(block, encryptRoundKeys);
|
||||||
|
// for (let l = 0; l < UINT8_BLOCK; l++) {
|
||||||
|
// outArray[roundIndex + l] =
|
||||||
|
// cipherBlock[parseInt(l / 4)] >> ((3 - l) % 4 * 8) & 0xff;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<int> EncryptRoundKeys(key) {
|
||||||
|
var keys = <int>[];
|
||||||
|
var mk = [
|
||||||
|
key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3],
|
||||||
|
key[4] << 24 | key[5] << 16 | key[6] << 8 | key[7],
|
||||||
|
key[8] << 24 | key[9] << 16 | key[10] << 8 | key[11],
|
||||||
|
key[12] << 24 | key[13] << 16 | key[14] << 8 | key[15]
|
||||||
|
];
|
||||||
|
|
||||||
|
// var k = List(36);
|
||||||
|
// k[0] = mk[0] ^ FK[0];
|
||||||
|
// k[1] = mk[1] ^ FK[1];
|
||||||
|
// k[2] = mk[2] ^ FK[2];
|
||||||
|
// k[3] = mk[3] ^ FK[3];
|
||||||
|
//
|
||||||
|
// for (int i = 0; i < 32; i++) {
|
||||||
|
// k[i + 4] = k[i] ^ tTransform2(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i]);
|
||||||
|
// keys[i] = k[i + 4];
|
||||||
|
// }
|
||||||
|
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
@ -1,16 +1,17 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:crypto/crypto.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:encrypt/encrypt.dart';
|
import 'package:encrypt/encrypt.dart' as ddd;
|
||||||
|
|
||||||
String getDNSAPIStr(List<int>data, String key, String value) {
|
ddd.Encrypted getDNSAPIStr(List<int>data, String key) {
|
||||||
final key = Key.fromBase64('BwwfHxgKDwcXAxkWDwEHDBseIREPIA4QDxYOEBIDIRY=');
|
// final key = Key.fromBase64('BwwfHxgKDwcXAxkWDwEHDBseIREPIA4QDxYOEBIDIRY=');
|
||||||
final iv = IV.fromBase64('FxIOBAcEEhISHgICCRYhEA==');
|
final iv = ddd.IV.fromLength(16);
|
||||||
final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: ""));
|
final encrypter = ddd.Encrypter(ddd.AES(ddd.Key.fromUtf8(key), mode: ddd.AESMode.ecb, padding: 'Zero'));
|
||||||
final encrypted = encrypter.encrypt('hello world', iv: iv);
|
final encrypted = encrypter.encrypt(key, iv: iv);
|
||||||
|
|
||||||
return encrypted.toString();
|
return encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static setDNSAPIStr(String value) {
|
// static setDNSAPIStr(String value) {
|
||||||
@ -22,6 +23,12 @@ import 'package:encrypt/encrypt.dart';
|
|||||||
// setCacheString(RKEnum.CACHE_STRING_APISTRURL, encrypt.base16);
|
// setCacheString(RKEnum.CACHE_STRING_APISTRURL, encrypt.base16);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
String md5Crypto(List<int> data){
|
||||||
|
final dig = md5.convert(data);
|
||||||
|
var keyStr = dig.toString();
|
||||||
|
return keyStr.substring(0, 16).toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
// 获取固定长度的数组
|
// 获取固定长度的数组
|
||||||
List<int> getFixedLengthList(List<int>data, int length) {
|
List<int> getFixedLengthList(List<int>data, int length) {
|
||||||
for(int i = 0; i<length; i++){
|
for(int i = 0; i<length; i++){
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
import 'io_protocol/io_addUser.dart';
|
import 'io_protocol/io_addUser.dart';
|
||||||
|
import 'io_protocol/io_getPrivateKey.dart';
|
||||||
import 'io_protocol/io_getPublicKey.dart';
|
import 'io_protocol/io_getPublicKey.dart';
|
||||||
import 'io_protocol/io_openDoor.dart';
|
import 'io_protocol/io_openDoor.dart';
|
||||||
import 'sender_data.dart';
|
import 'sender_data.dart';
|
||||||
@ -13,6 +14,22 @@ class IoSenderManage {
|
|||||||
), callBack:callBack);
|
), callBack:callBack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//todo:获取私钥
|
||||||
|
static void getPrivateKey(
|
||||||
|
String? lockId,
|
||||||
|
String? keyID, // 钥匙ID
|
||||||
|
String? authUserID,// 钥匙授权人ID
|
||||||
|
int? nowTime,
|
||||||
|
int? needAuthor, {CommandSendCallBack? callBack}) {
|
||||||
|
CommandSenderManager().managerSendData(command: GetPrivateKeyCommand(
|
||||||
|
lockID: lockId,
|
||||||
|
keyID: keyID,
|
||||||
|
authUserID: authUserID,
|
||||||
|
nowTime: nowTime,
|
||||||
|
needAuthor: needAuthor,
|
||||||
|
), callBack:callBack);
|
||||||
|
}
|
||||||
|
|
||||||
//todo:添加用户
|
//todo:添加用户
|
||||||
// static void senderAddUser({CommandSendCallBack? callBack}) {
|
// static void senderAddUser({CommandSendCallBack? callBack}) {
|
||||||
// CommandSenderManager().managerSendData(
|
// CommandSenderManager().managerSendData(
|
||||||
|
|||||||
@ -86,6 +86,7 @@ dependencies:
|
|||||||
lpinyin: ^2.0.3
|
lpinyin: ^2.0.3
|
||||||
#加密解密
|
#加密解密
|
||||||
encrypt: ^5.0.1
|
encrypt: ^5.0.1
|
||||||
|
crypto: ^3.0.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user