33 lines
1.2 KiB
Dart
33 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
import 'package:encrypt/encrypt.dart' as encrypt;
|
|
|
|
import '../../constant/api_key.dart';
|
|
|
|
class KeyEncryption {
|
|
// استخدم مفتاح بطول 32 حرفًا
|
|
static final _key = encrypt.Key.fromUtf8(AK.keyOfApp);
|
|
static final _iv =
|
|
encrypt.IV.fromLength(16); // توليد تهيئة عشوائية بطول 16 بايت
|
|
|
|
static String encryptKey(String key) {
|
|
final encrypter =
|
|
encrypt.Encrypter(encrypt.AES(_key, mode: encrypt.AESMode.cbc));
|
|
final encrypted = encrypter.encrypt(key, iv: _iv);
|
|
final result = _iv.bytes + encrypted.bytes; // تضمين التهيئة مع النص المشفر
|
|
return base64Encode(result);
|
|
}
|
|
|
|
static String decryptKey(String encryptedKey) {
|
|
print('encryptedKey: ${AK.keyOfApp}');
|
|
|
|
final decoded = base64Decode(encryptedKey);
|
|
print('encryptedKey: $encryptedKey');
|
|
final iv = encrypt.IV(decoded.sublist(0, 16)); // استخراج التهيئة
|
|
final encrypted =
|
|
encrypt.Encrypted(decoded.sublist(16)); // استخراج النص المشفر
|
|
final encrypter =
|
|
encrypt.Encrypter(encrypt.AES(_key, mode: encrypt.AESMode.cbc));
|
|
return encrypter.decrypt(encrypted, iv: iv);
|
|
}
|
|
}
|