sofiane khoudour
sofiane khoudour

Reputation: 93

AES Encryption in dart 2

I've been trying to Encrypt a string using encrypt to send it to a webserver but i kept getting errors. this is my function :

void encRypt(String da){
 final key = "my32lengthsupersecretnooneknows1";
 final encrypter = new Encrypter(new AES(key));
 final encrypted = encrypter.encrypt(da);
 print(encrypted);
 }

this is how i used my function : encRypt(chatBody.text); and chatBody is a TextEditingController

the main errors

this my Debug Console :

Launching lib/main.dart on GT I9500 in debug mode...
    Built build/app/outputs/apk/debug/app-debug.apk.
    I/Timeline(19551): Timeline: Activity_idle id: android.os.BinderProxy@34db68a4 time:15254411
    E/        (19551): [android_ws] Format: 5, Width: 1080, Height: 1920
    E/        (19551): [android_ws] Format: 5, Width: 1080, Height: 1920
    D/ViewRootImpl(19551): ViewPostImeInputStage ACTION_DOWN
    D/ViewRootImpl(19551): ViewPostImeInputStage ACTION_DOWN
    D/ViewRootImpl(19551): ViewPostImeInputStage ACTION_DOWN
    E/flutter (19551): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
    E/flutter (19551): Invalid argument(s): Input buffer too short
    E/flutter (19551): #0      AESFastEngine.processBlock (package:pointycastle/block/aes_fast.dart:113:7)
    E/flutter (19551): #1      AES._processBlocks (package:encrypt/src/aes.dart:42:25)
    E/flutter (19551): #2      AES.encrypt (package:encrypt/src/aes.dart:22:20)
    E/flutter (19551): #3      Encrypter.encrypt (package:encrypt/encrypt.dart:17:17)
    E/flutter (19551): #4      StartPageState.encRypt (file:///home/sofiane/flutter/prog/testone/lib/pages/startpage.dart:28:29)
    E/flutter (19551): #5      StartPageState.getData (file:///home/sofiane/flutter/prog/testone/lib/pages/startpage.dart:39:6)
    E/flutter (19551): <asynchronous suspension>
    E/flutter (19551): #6      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
    E/flutter (19551): #7      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
    E/flutter (19551): #8      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
    E/flutter (19551): #9      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
    E/flutter (19551): #10     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
    E/flutter (19551): #11     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
    E/flutter (19551): #12     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
    E/flutter (19551): #13     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
    E/flutter (19551): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
    E/flutter (19551): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
    E/flutter (19551): #16     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
    E/flutter (19551): #17     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
    E/flutter (19551): #18     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
    E/flutter (19551): #19     _invoke1 (dart:ui/hooks.dart:153:13)
    E/flutter (19551): #20     _dispatchPointerDataPacket (dart:ui/hooks.dart:107:5)
    V/ActivityThread(19551): updateVisibility : ActivityRecord{1fd79a2f token=android.os.BinderProxy@34db68a4 {com.example.testone/com.example.testone.MainActivity}} show : true
    Application finished.
    Exited (sigint)

I tried everything as the example was shown on the github page of the package encrypt github it didn't work.

Upvotes: 7

Views: 8518

Answers (3)

AKushWarrior
AKushWarrior

Reputation: 544

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 own
var nonce = CryptKey().genDart(len: 12); //generate IV for AES with Dart Random.secure(); you can also enter your own
var aesEncrypter = AesCrypt(key: fortunaKey, padding: PaddingAES.pkcs7); //generate AES encrypter with key and PKCS7 padding
String encrypted = aesEncrypter.gcm.encrypt(inp: 'somedatahere', iv: nonce); //encrypt using GCM
String decrypted = aesEncrypter.gcm.decrypt(inp: encrypted, iv: nonce); //decrypt

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

Upvotes: 2

shyandsy
shyandsy

Reputation: 21

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

    }

Upvotes: 0

Raouf Rahiche
Raouf Rahiche

Reputation: 31386

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

Upvotes: 2

Related Questions