AES Encryption in dart 2 AES Encryption in dart 2 dart dart

AES Encryption in dart 2


AES has a fixed block size of 128 bits and a key size of 128, 192, or 256 bits, So you can try to use Salsa20 instead


For anyone coming here now, I wrote a custom package based off PointyCastle and written entirely in Dart which can greatly simplify AES for you.

https://pub.dev/packages/steel_crypt

It looks something like this implemented:

var fortunaKey = CryptKey().genFortuna(); //generate 32 byte key with Fortuna; you can also enter your ownvar nonce = CryptKey().genDart(len: 12); //generate IV for AES with Dart Random.secure(); you can also enter your ownvar aesEncrypter = AesCrypt(key: fortunaKey, padding: PaddingAES.pkcs7); //generate AES encrypter with key and PKCS7 paddingString encrypted = aesEncrypter.gcm.encrypt(inp: 'somedatahere', iv: nonce); //encrypt using GCMString decrypted = aesEncrypter.gcm.decrypt(inp: encrypted, iv: nonce); //decrypt

This solves any issues with block size that may have occurred earlier.


try to use the cipher2 plugin for AES encrytion and decrytion

package:

this package support the following AES modes for both ios and android now

  • 128bit cbc padding 7
  • 128bit gcm

and will be continue updating and refactoring

Sample and test case for how to use it:

https://github.com/shyandsy/cipher2/blob/master/example/lib/main.dart

AES 128bit cbc encrytion and description sample code

try {      // encrytion      encryptedString = await Cipher2.encryptAesCbc128Padding7(plainText, key, iv);      // decrytion      //encryptedString = "hello";      decryptedString = await Cipher2.decryptAesCbc128Padding7(encryptedString, key, iv);    } on PlatformException catch(e) {      encryptedString = "";      decryptedString = "";      print("exception code: " + e.code);      print("exception message: " + e.message);    }