Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);
//    dynamic _toEncrypted = _encryptDat.base64;
    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!);
Posted
Updated 17-Jan-23 23:14pm
v2

1 solution

As per the documentation found HERE, try the following -

var source = 'flutter app';
    final decrypted = encrypter.decrypt64(source, iv: iv);


For normal Direct decryption, you can use the decrypt method directly, as:
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
    final iv = IV.fromLength(8);
    final encrypter = Encrypter(Salsa20(key));
    final encrypted = encrypter.encrypt(plainText, iv: iv);
    final decrypted = encrypter.decrypt(encrypted, iv: iv);


There is also a solution found as per above at Here
 
Share this answer
 
v2
Comments
Dan Sep2022 19-Jan-23 10:41am    
The same error appears again... the problem is when data is retrieved the value datatype is automatically changed to String.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900