encryptString method
Encrypt data using AES-256-CTR with deterministic IV.
Implementation
String encryptString(String data) {
final plainBytes = utf8.encode(data);
// Generate deterministic IV based on content + seed
final iv = _generateDeterministicIV(data);
// Handle empty string case
if (plainBytes.isEmpty) {
return base64.encode(iv);
}
// Setup AES cipher in CTR mode (handles any length, no padding needed)
final cipher = CTRStreamCipher(AESEngine());
final params = ParametersWithIV(KeyParameter(_key), iv);
cipher.init(true, params); // true = encrypt
final encryptedBytes = cipher.process(Uint8List.fromList(plainBytes));
// Combine IV + encrypted data
final result = Uint8List(iv.length + encryptedBytes.length);
result.setRange(0, iv.length, iv);
result.setRange(iv.length, result.length, encryptedBytes);
return base64.encode(result);
}