如何加密/解密在颤振使用ECB模式/AES256没有IV?



我正在尝试在flutter/Dart中加密字符串。我试过下面的代码,但看起来有IV是强制性的。

final key = Key.fromBase64("Some_Key");
final iv = IV.fromBase64("Some_Key"); // I do not need IV for encryption/decryption
final encrypter = Encrypter(AES(key, mode: AESMode.ecb, padding: 'PKCS7'));
final encrypted = encrypter.encrypt(employeeNumber, iv: iv); //No IV needed

有人可以让我知道如何加密和解密使用AES 256位/ECB模式/PKCS7填充和没有IV的字符串。

请注意,我暂时不需要静脉注射。请帮助…

我也想避免IV,然后我找到了以下解决方案:将虚拟IV设为

var ivBtyes = Uint8List.fromList([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
final iv = encrypt.IV(ivBtyes);

设置AES模式为ECB,填充为PKCS7"然后你的代码-

final encrypted = encrypter.encrypt(employeeNumber, iv: iv);

虽然你将IV作为dummy传递,但在ECB模式下它将被忽略。

对于那些仍然在寻找答案的人,如果你没有IV值,你可以使用

final iv = IV.fromLength(16);

下面是有效的代码

final iv = IV.fromLength(16);
final enc = Encrypter(AES(Key(Uint8List.fromList(keyBytes)),
mode: AESMode.ecb, padding: 'PKCS7'));
final encrypted = enc.encrypt(plaintext, iv: iv);
final base64String = base64.encode(encrypted.bytes);

最新更新