Hello, I'm trying to decrypt a text from MySQL db (stored in BLOB column) and I encountered with this issue where when I tried to decrypt that value I got an error saying
Unhandled Exception: type 'String'
is not a subtype of type 'Encrypted' in type cast
And this error occurs obviously because the returned value is not Encrypted and is actually a String, but why? why does the retrieved value data type became String? and not Encrypted?.
This is how the value was encrypted
var _encryptCustString = EncryptionModel().aesEncrypt(mykey,"somestring");
var _tobase64 = _encryptCustString.base64;
How the value was retrieved and decrypt it
var _getString = await conn.execute("SELECT CustString FROM information WHERE CUST_USERNAME = :username",
{
"username": custUsername
});
Encryption.Encrypted? _valueEnc = null;
for(var _userPass in _getPass.rows) {
dynamic _getValue = _userPass.assoc()['CustString'];
_valueEnc = _getValue;
var decryptedCust = EncryptionModel().aesDecrypt(mykey,_valueEnc!);
EncryptionModel class
import 'package:encrypt/encrypt.dart' as encrypter;
class EncryptionModel {
encrypter.Encrypted aesEncrypt(String key, var Value) {
final _getKey = encrypter.Key.fromUtf8(key);
final encryptServ = encrypter.Encrypter(encrypter.AES(_getKey, mode: encrypter.AESMode.cbc));
final InitVect = encrypter.IV.fromUtf8(key.substring(0,16));
encrypter.Encrypted _encryptDat = encryptServ.encrypt(Value,iv: InitVect);
return _encryptDat;
}
Object aesDecrypt(String key, encrypter.Encrypted EncValue) {
var _decryptedObj;
final _getKey = encrypter.Key.fromUtf8(key);
final encryptServ = encrypter.Encrypter(encrypter.AES(_getKey, mode: encrypter.AESMode.cbc));
final InitVect = encrypter.IV.fromUtf8(key.substring(0,16));
final _getDecrypt = encryptServ.decrypt(EncValue, iv: InitVect);
_decryptedObj = _getDecrypt;
return _getDecrypt;
}
}
What I have tried:
(returns an error)
Encryption.Encrypted? _valueEnc = null;
for(var _userPass in _getPass.rows) {
dynamic _getValue = _userPass.assoc()['CUST_PASSWORD'] as Encryption.Encrypted;
_valueEnc = _getValue;
var decryptedCust = EncryptionModel().aesDecrypt("0123456789085746",_valueEnc!);