encryptWallet static method
Implementation
static EncKeyBean encryptWallet(KeyBean kb, String walletDirectory, String walletName, String password) {
String jsonWallet = kb.toJson().toString();
final saltBytes = Uint8List.fromList(utf8.encode("TakamakaWallet"));
// Derive the key from the password and salt using PBKDF2
final key = _generatePbkdf2Key(password, saltBytes);
// Generate a random IV (Initialization Vector)
final iv = _generateRandomBytes(16);
String base64StringKeyBack = base64Encode(iv);
// Create an AES block cipher with CBC mode of operation
final encrypter =
Encrypter(AES(encrypt.Key(Uint8List.fromList(key)), mode: AESMode.cbc));
final encryptedSeed = encrypter.encrypt(kb.seed, iv: IV(Uint8List.fromList(iv)));
final encryptedWords = encrypter.encrypt(kb.words, iv: IV(Uint8List.fromList(iv)));
// Encrypt the plaintext using AES-256 with the derived key and IV
final encrypted =
encrypter.encrypt(jsonWallet, iv: IV(Uint8List.fromList(iv)));
FileSystemUtils.saveFileInWalletDir(
walletDirectory, walletName, 'words_enc', encryptedWords.base64);
FileSystemUtils.saveFileInWalletDir(
walletDirectory, walletName, 'seed_enc', encryptedSeed.base64);
EncKeyBean ekb = EncKeyBean("0.1", "AES", [
base64StringKeyBack,
encrypted.base64
]);
return ekb;
}