Developer
Developer

Reputation: 41

How to encrypt/decrypt in flutter using ECB mode/AES256 without having IV?

I am trying to encrypt string in flutter/Dart. I have tried the below code but looks like having IV is mandatory.

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

Could someone please let me know how to encrypt and decrypt the strings using AES 256 bit/ ECB mode / PKCS7 padding and without IV.

Please note that I do not need IV at the moment. Kindly help...

Upvotes: 1

Views: 3488

Answers (2)

mangesh
mangesh

Reputation: 574

For those who are still looking for the answer if you don't have IV value you can use

final iv = IV.fromLength(16);

here is the code that worked

  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);

Upvotes: 0

Sohail Ansari
Sohail Ansari

Reputation: 131

I also wanted to avoid IV, and then I found the following solution : Make the dummy IV as

      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);

set the AES mode as ECB and padding as "PKCS7". then your code -

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

Though you are passing IV as dummy it will be ignored in ECB mode.

Upvotes: 1

Related Questions