1

i am trying to decrypt an encrypted text in dart. I am getting the error Unhandled Exception: Invalid argument(s): Input data length must be a multiple of cipher's block size . I am not too sure what the error means. Google wasn't really helpful. Here is my code.


void main() {

  final key = Key.fromUtf8('put32charactershereeeeeeeeeeeee!'); //32 chars
  final iv = IV.fromUtf8('put16characters!'); //16 chars

  //encrypt
  String encryptMyData(String text) {
    final e = Encrypter(AES(key, mode: AESMode.cbc));
    final encrypted_data = e.encrypt(text, iv: iv);
    return encrypted_data.base64;
  }

//dycrypt
  String decryptMyData(String text) {
    final e = Encrypter(AES(key, mode: AESMode.cbc));
    final decrypted_data = e.decrypt(Encrypted.fromBase64(text), iv: iv);
    return decrypted_data;
  }

  decryptMyData("gAAAAABiDgiJAH4FI9E1aNLl1hv19VR61rLVEdjIrEElU-tcCDOvADPFbO1JhemTFyMG2S-YSsFgUxOJ-QjnNGBfyfJzytsEQUCDkBNdQUTSryj74tp7dV1gh_BzJOxTGPgVA3AGaVcKeL1_CLiIRzJc0l0L_Xa_3FHEG2MP7jdSUgqCvmpggp8wf5_7nANHIxlXN6ikcfGUbGDjDch4PovRnqLp5nEHGUl3l9zNG3JmK09yKEjjEkbGIf4VTIWuIvbdUae87jfIOhVTiil9T9BT1YYd2Ob6iKioVaTAVaVCmM8Oi9qNdrHqwdpXuUfpGBHWMXgsNtSMUAwJ7W19fHDgCzqVA0ENLp2rZqtIN7leL4JSHbGEEA2C4HYNt5wab6hPjiLxlvFaoPBfgIXiMjt7XqFrBy3koZuGj1iPOe8KRe0IxEArWMnGX_I_QIqcai0BU0GDQygQ5IsSud-GH3FuyGwGlWI3O4p5SkJEkaQ4B2Z0tbE0491qzNtt5SKaNc24KWHb-bkIR3ekjE6G6t8b3t_GYlKTvfYDebcO1PfoLjvh8Uy28xw3YRKAzwJCUhhom7jbyJnXvie9gQRSFNLx-59GuRVx5u0CJCQxH583yKuX2R1wj9Qb-xg8tD_IZCrdP-mf07qhnU-2X-G-YQfXiW1y2_BZnIolG6_gec5f9HbaPbZ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RuZ8XAmU5VLwuvWqj6u8JVUUs54vlylvLV9CAvMiAiHKxBCd8PsrjfNxMrDXutQlfBWRTwvaLsnY6j90iXYCryrUOp5WkblSfXtXcJcY1oxZNGzJCBWA831md_a0Aucqrny29XtyqJcpd6LLg8oo4uX6sGFz2IVrF0qthtxnTXwJhQfgy6CsQ5O_QJYejTv_09xA3BQr60LC0zPatcCynbyevw-7D5yXkWhS7ygQXSABQqtNjXIVzagfML7C1oJm1UsSaMV6-4kqHtkKTzfTQPB75eEgezxLLPG2JuBSVOJNBDJSO0wmE-w6M0kWo2mCrhDjFsRo-mC_C4alHnBWaBMN2EOnP-E6HIPt8kUZF_2q3mmC_lDkzIj1jYx9pJIL-KrhBH5Og9oGKrT7lCqtYIReOXJdNoo7tnzmtsxef9On8WvTibUvhbwJgXckmtDuSyS4=");

}
winfred adrah
  • 428
  • 6
  • 18
  • Since it seems to be a Fernet token, you should use the appropriate Fernet implementation of the package, see e.g. [Fernet class](https://pub.dev/documentation/encrypt/latest/encrypt/Fernet-class.html) and this [example](https://github.com/leocavalcante/encrypt/blob/5.x/example/fernet.dart). – Topaco Feb 17 '22 at 11:12
  • thank you for the reply, I have seen the example and the example works fine. My problem with the example is i can't decrypt a text unless i decrypt myself first. I have tried passing the text as a String but that wasn't successful. – winfred adrah Feb 17 '22 at 11:18
  • Use [`Encrypted`](https://pub.dev/documentation/encrypt/latest/encrypt/Encrypted-class.html) to import the token, and check the key format. Fernet actually uses a Base64url encoded 32 bytes key. Please post _valid_ test data so that the token can be decrypted with the key. – Topaco Feb 17 '22 at 12:23
  • @Topaco. Sorry i am new to encryption. Assuming i am to use the example provided. How do i decrypt a TEXT and Not an encrypted data like they have done? – winfred adrah Feb 17 '22 at 12:26
  • The code works as it is for an AES/CBC encryption/decryption, as you can easily verify e.g. with `print(decryptMyData(encryptMyData('Hello world')));`. `decryptMyData()` expects a Base64 encoded ciphertext as returned by `encryptMyData()`. – Topaco Feb 17 '22 at 12:39
  • If the ciphertext should have a different encoding, it must be compatible with [`Encrypted()`](https://pub.dev/documentation/encrypt/latest/encrypt/Encrypted-class.html), i.e. besides Base64 e.g. binary (`Uint8List`) or hex. – Topaco Feb 17 '22 at 12:44

0 Answers0