'Invalid argument(s): Input buffer too short AND Invalid or corrupted pad block

I am trying to encrypt and decrypt chat messages for my app using this code

String decrypt(String encrypted, {String key, String iv}) {
  final key = Key.fromUtf8(key); //hardcode combination of 16 character
  final iv = IV.fromUtf8(iv); //hardcode combination of 16 character

  final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
  Encrypted enBase64 = Encrypted.from64(encrypted);
  final decrypted = encrypter.decrypt(enBase64, iv: iv);
  return decrypted;
}

String encrypt(String value, {String key, String iv}) {
  final key = Key.fromUtf8(key); //hardcode
  final iv = IV.fromUtf8(iv); //hardcode

  final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
  final encrypted = encrypter.encrypt(value, iv: iv);

  return encrypted.base64;
}

I generated a unique 16 characters for key and iv for each chat room u know for security purpose Eg

String dec = decrypt("encrypted text" {key: "1204581692165412",  iv: "e16ca718048594ce"});
String enc = encrypt("plain text" {key: "1204581692165412",  iv: "e16ca718048594ce"});

But i keep getting the error

Invalid argument(s): Invalid or corrupted pad block
When the exception was thrown, this was the stack
#0      
PKCS7Padding.padCount   
package:pointycastle/paddings/pkcs7.dart:42
#1      
PaddedBlockCipherImpl.doFinal   
package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart:112
#2  
PaddedBlockCipherImpl.process   
package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart:74
#3      
AES.decrypt   
package:encrypt/…/algorithms/aes.dart:63
#4      
Encrypter.decryptBytes   
package:encrypt/src/encrypter.dart:25
#5      
Encrypter.decrypt   
package:encrypt/src/encrypter.dart:31

Which successfully encypts but throws the error when decrypting The error is comming from the decrypt function

final decrypted = encrypter.decrypt(enBase64, iv: iv);

So i added padding to be null

final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: null));

I still get another error

E/flutter ( 5888): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument(s): Input buffer too short
E/flutter ( 5888): #0      CBCBlockCipher._encryptBlock (package:pointycastle/block/modes/cbc.dart:72:7)
E/flutter ( 5888): #1      CBCBlockCipher.processBlock (package:pointycastle/block/modes/cbc.dart:67:13)
E/flutter ( 5888): #2      AES._processBlocks (package:encrypt/src/algorithms/aes.dart:73:25)
E/flutter ( 5888): #3      AES.encrypt (package:encrypt/src/algorithms/aes.dart:41:22)
E/flutter ( 5888): #4      Encrypter.encryptBytes (package:encrypt/src/encrypter.dart:12:19)
E/flutter ( 5888): #5      Encrypter.encrypt (package:encrypt/src/encrypter.dart:20:12)
E/flutter ( 5888): #6      encrypt (package:quelib/src/handlers/helpers/encryption.dart:18:31)
E/flutter ( 5888): #7      _ChatBottomInputState.sendChatData (package:quelib/src/pages/home/chat/chatBtm.dart:293:11)
E/flutter ( 5888): #8      _ChatBottomInputState.build.<anonymous closure> (package:quelib/src/pages/home/chat/chatBtm.dart:244:44)
E/flutter ( 5888): #9      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
E/flutter ( 5888): #10     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
E/flutter ( 5888): #11     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
E/flutter ( 5888): #12     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
E/flutter ( 5888): #13     BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:230:7)
E/flutter ( 5888): #14     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:563:9)
E/flutter ( 5888): #15     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:94:12)
E/flutter ( 5888): #16     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:139:9)
E/flutter ( 5888): #17     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
E/flutter ( 5888): #18     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:137:18)
E/flutter ( 5888): #19     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:123:7)
E/flutter ( 5888): #20     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:439:19)
E/flutter ( 5888): #21     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
E/flutter ( 5888): #22     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:322:11)
E/flutter ( 5888): #23     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
E/flutter ( 5888): #24     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
E/flutter ( 5888): #25     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
E/flutter ( 5888): #26     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
E/flutter ( 5888): #27     _rootRunUnary (dart:async/zone.dart:1444:13)
E/flutter ( 5888): #28     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 5888): #29     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 5888): #30     _invoke1 (dart:ui/hooks.dart:169:10)
E/flutter ( 5888): #31     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:293:7)
E/flutter ( 5888): #32     _dispatchPointerDataPacket (dart:ui/hooks.dart:88:31)

Which is coming from encrypt function

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

And if i use a particular non-generated dummy key

String decrypt(String encrypted) {
  final key =
      Key.fromUtf8("1245714587458888"); //hardcode combination of 16 character
  final iv =
      IV.fromUtf8("e16ce888a20dadb8"); //hardcode combination of 16 character

  final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
  Encrypted enBase64 = Encrypted.from64(encrypted);
  final decrypted = encrypter.decrypt(enBase64, iv: iv);
  return decrypted;
}

String encrypt(String value) {
  final key = Key.fromUtf8("1245714587458888"); //hardcode
  final iv = IV.fromUtf8("e16ce888a20dadb8"); //hardcode

  final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
  final encrypted = encrypter.encrypt(value, iv: iv);

  return encrypted.base64;
}

It works perfectly without any issue, but this isn't secured. So i want to ask if my problem is from the keys i generated or from somewhere else. Please help me out.

Note: Am using the package encrypt: ^5.0.1

UPDATE: Finally fixed I used the same key value for both IV and Key



Solution 1:[1]

I had the same problem. For it to work for me, you have 2 options, removed the mode setting and let it use the default cic AESMode when setting up the instance of your Encrypter. If you want to use or have to use cbc mode, you need to set the padding:

_encrypter = Encrypter(AES(_key, mode: AESMode.cbc, padding: "PKCS7"));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DharmanBot