cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

AES-CBC native mode encryption result is different from Android(WRONG) and IOS on some special string

Open Pricx opened this issue 3 years ago • 2 comments

Hi, when FlutterCryptography.enable(); aka the native mode is activated, here is the problem:

String to be encrypted: 1111111111|11111111-1111-1111-1111-111111111111|11124c2c8662c5a3210a111111111111

flutter code:

static SecretKey secretKey = SecretKey(utf8.encode("1234567890123456"));
static List<int> iv = utf8.encode("1234567890123456");
var encryptAlgorithm = AesCbc.with128bits(macAlgorithm: Hmac.sha256());
static Future<String> aesEncrypt({required message}) async {
    final bytesMes = utf8.encode(message);
    final secretBox = await encryptAlgorithm.encrypt(bytesMes,
        secretKey: secretKey, nonce: iv);
    String cipher = base64.encode(secretBox.cipherText);
    return cipher;
  }

Result(different from Android(WRONG) and IOS):

  • In Android (WRONG RESULT): oUfakBKtZwgaMUjTvhrIniB/21AvG7hrlARQmGrqzw2098QxuKCL7JOJDoiL+UECO8RP8KUeU9pYvFmcoDs9gyXxyXcbHng0lZSud9bUn6s=
  • IOS (CORRECT): oUfakBKtZwgaMUjTvhrIniB/21AvG7hrlARQmGrqzw2098QxuKCL7JOJDoiL+UECO8RP8KUeU9pYvFmcoDs9gyXxyXcbHng0lZSud9bUn6uzZY3Lxyqe5DFx1khUj8TL

Seems like the last fragment of the cipher is lost.

More: This inconsistent problem doesn't occur when I use AES-CBC native mode encryption result different from Android(WRONG) and IOS. AES-CBC native mode encryption result different from Android(WRONG) and IOS. as the message to be encrypted.

This is all I have, but I don't know how to solve it when I still want to use native mode. Thanks for help :).

Pricx avatar Apr 14 '22 12:04 Pricx

Interesting. Tests also revealed that javax.security truncates outputs sometimes when using 192 bit keys (not 128 bit and 256 bit keys). Perhaps these issues could be avoided by using another API (like tink) in Android.

terrier989 avatar Mar 03 '23 20:03 terrier989

Interesting. Tests also revealed that javax.security truncates outputs sometimes when using 192 bit keys (not 128 bit and 256 bit keys). Perhaps these issues could be avoided by using another API (like tink) in Android.

Thanks for answering

Pricx avatar Mar 20 '23 12:03 Pricx