提交加解密代码
This commit is contained in:
parent
9947d7b8b3
commit
3b31e121da
@ -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');
|
||||
print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value hexStr:${radixString(value)}');
|
||||
|
||||
try {
|
||||
List<int> valueList = value;
|
||||
@ -156,7 +156,7 @@ class BlueManage{
|
||||
await _flutterReactiveBle!.writeCharacteristicWithResponse(characteristic, value: subData[i]).then((value) async {
|
||||
await Future.delayed(const Duration(milliseconds: 1))
|
||||
.then((value) async {
|
||||
print('成功了么');
|
||||
print('分包发送成功了');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,30 +1,33 @@
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import '../../tools/storage.dart';
|
||||
import '../io_tool/io_tool.dart';
|
||||
import 'io_reply.dart';
|
||||
import 'io_sender.dart';
|
||||
import 'io_type.dart';
|
||||
|
||||
import 'package:crypto/crypto.dart' as a;
|
||||
|
||||
class GetPrivateKeyCommand extends SenderProtocol {
|
||||
|
||||
String? lockID;
|
||||
String? keyID; // 钥匙ID
|
||||
String? authUserID; // 钥匙授权人ID
|
||||
int? nowTime;
|
||||
String? authCode;
|
||||
int? needAuthor;
|
||||
GetPrivateKeyCommand({
|
||||
this.lockID,
|
||||
this.keyID,
|
||||
this.authUserID,
|
||||
this.nowTime,
|
||||
this.authCode,
|
||||
this.needAuthor,
|
||||
}) : super(CommandType.getLockPrivateKey);
|
||||
|
||||
@override
|
||||
List<int> messageDetail() {
|
||||
List<int> data = [];
|
||||
List<int> ebcData = [];
|
||||
print("lockID:${lockID!} lockID.utf8.encode${utf8.encode(lockID!)}");
|
||||
|
||||
// 锁id
|
||||
@ -43,19 +46,51 @@ class GetPrivateKeyCommand extends SenderProtocol {
|
||||
data = getFixedLengthList(data, 20 - authUserIDLength);
|
||||
|
||||
//NowTime 4
|
||||
DateTime now = DateTime.now();
|
||||
int timestamp = now.millisecondsSinceEpoch;
|
||||
data.add((timestamp & 0xff000000) >> 24);
|
||||
data.add((timestamp & 0xff0000) >> 16);
|
||||
data.add((timestamp & 0xff00) >> 8);
|
||||
data.add((timestamp & 0xff));
|
||||
// DateTime now = DateTime.now();
|
||||
// int timestamp = now.millisecondsSinceEpoch;
|
||||
var d1 = 0x11223344;
|
||||
data.add((d1 & 0xff000000) >> 24);
|
||||
data.add((d1 & 0xff0000) >> 16);
|
||||
data.add((d1 & 0xff00) >> 8);
|
||||
data.add((d1 & 0xff));
|
||||
|
||||
if(needAuthor == 0){
|
||||
data.add(0);
|
||||
}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 '../io_tool/io_tool.dart';
|
||||
import '../sender_manage.dart';
|
||||
import 'io_reply.dart';
|
||||
import 'io_sender.dart';
|
||||
import 'io_type.dart';
|
||||
@ -38,7 +39,7 @@ class GetPublicKeyReply extends Reply {
|
||||
print('获取公钥成功 publickey:$stringEncoded');
|
||||
// 储存公钥
|
||||
Storage.setData("bluePublicKey", stringEncoded);
|
||||
|
||||
IoSenderManage.getPrivateKey("TMH_c3570480da8d", "1", "1", 1, 1);
|
||||
break;
|
||||
case 0x07:
|
||||
//无权限
|
||||
|
||||
@ -34,3 +34,64 @@ class cbc{
|
||||
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:typed_data';
|
||||
|
||||
import 'package:crypto/crypto.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) {
|
||||
final key = Key.fromBase64('BwwfHxgKDwcXAxkWDwEHDBseIREPIA4QDxYOEBIDIRY=');
|
||||
final iv = IV.fromBase64('FxIOBAcEEhISHgICCRYhEA==');
|
||||
final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: ""));
|
||||
final encrypted = encrypter.encrypt('hello world', iv: iv);
|
||||
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);
|
||||
|
||||
return encrypted.toString();
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
// static setDNSAPIStr(String value) {
|
||||
@ -22,6 +23,12 @@ import 'package:encrypt/encrypt.dart';
|
||||
// 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) {
|
||||
for(int i = 0; i<length; i++){
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
|
||||
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 'sender_data.dart';
|
||||
@ -13,6 +14,22 @@ class IoSenderManage {
|
||||
), 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:添加用户
|
||||
// static void senderAddUser({CommandSendCallBack? callBack}) {
|
||||
// CommandSenderManager().managerSendData(
|
||||
|
||||
@ -86,6 +86,7 @@ dependencies:
|
||||
lpinyin: ^2.0.3
|
||||
#加密解密
|
||||
encrypt: ^5.0.1
|
||||
crypto: ^3.0.3
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user