when I was using Flutter’s encrypt: ^5.0.3
dependence to encrypt and decrypt string data, I encountered the occasional unsolvable RangeError
in release mode:
RangeError: Value not in range: -16
Here is my code:
import 'package:encrypt/encrypt.dart' as encrypt;
class EncryptedStorage {
static final key = encrypt.Key.fromUtf8(randomString32);
static final iv = encrypt.IV.fromUtf8(randomString16);
static final encrypt.Encrypter encryptor = encrypt.Encrypter(
encrypt.AES(
key,
mode: encrypt.AESMode.cbc,
padding: "PKCS7",
),
);
static String getEncryptedString(String originalString) {
final encrypt.Encrypted encrypted = encryptor.encrypt(originalString, iv: iv);
return encrypted.base64;
}
static String getStringByEncryptData(String encryptedData) {
final String decrypted = encryptor.decrypt64(encryptedData, iv: iv);
return decrypted;
}
}
I’ve gotten this RangeError
while I calling getOriginalStringByEncryptData()
. And it’s always the value -16.
Does anyone know the cause of the error?
This error appears on some iPhone devices. It’s occasional. I don’t know how to reproduce this error.
Here is the detail of flutter doctor
[✓] Flutter (Channel stable, 3.22.1, on macOS 14.5 23F79 darwin-x64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.3)
[✓] Android Studio (version 2021.2)
[✓] Android Studio (version 2021.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.4)
[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.1)
[✓] VS Code (version 1.89.1)