25-1/28/1
This commit is contained in:
52
lib/controller/functions/encrypt_decrypt.dart
Normal file
52
lib/controller/functions/encrypt_decrypt.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'package:encrypt/encrypt.dart' as encrypt;
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../env/env.dart';
|
||||
|
||||
var secretKey = Env.keyOfApp
|
||||
.toString()
|
||||
.split('XrXlBl')[0]; // Must be 16 characters for AES-128
|
||||
String initializationVector = Env.initializationVector; // Must be 16 characters
|
||||
final encryptionHelper = EncryptionHelper(
|
||||
secretKey: secretKey,
|
||||
initializationVector: initializationVector,
|
||||
);
|
||||
|
||||
class EncryptionHelper {
|
||||
final encrypt.Key key;
|
||||
final encrypt.IV iv;
|
||||
|
||||
EncryptionHelper(
|
||||
{required String secretKey, required String initializationVector})
|
||||
: key = encrypt.Key.fromUtf8(secretKey),
|
||||
iv = encrypt.IV.fromUtf8(initializationVector);
|
||||
|
||||
// Initialize the helper
|
||||
|
||||
/// Encrypts the given plain text
|
||||
String encryptData(String plainText) {
|
||||
try {
|
||||
final encrypter = encrypt.Encrypter(encrypt.AES(key,
|
||||
mode: encrypt.AESMode.cbc)); // Explicitly use CBC mode
|
||||
final encrypted = encrypter.encrypt(plainText, iv: iv);
|
||||
return encrypted.base64;
|
||||
} catch (e) {
|
||||
debugPrint('Encryption Error: $e');
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/// Decrypts the given encrypted text
|
||||
String decryptData(String encryptedText) {
|
||||
try {
|
||||
final encrypter = encrypt.Encrypter(encrypt.AES(key,
|
||||
mode: encrypt.AESMode.cbc)); // Explicitly use CBC mode
|
||||
final encrypted = encrypt.Encrypted.fromBase64(encryptedText);
|
||||
final decrypted = encrypter.decrypt(encrypted, iv: iv);
|
||||
return decrypted;
|
||||
} catch (e) {
|
||||
debugPrint('Decryption Error: $e');
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user