Search code examples
flutterdartencryptionaes

FLUTTER /encrypt getting 32o string when encrypting 16o String with NO PADDING


i have a problem with my AES128-cbc with no padding, when I give it a 16o String it give me back a 32o String (I have no padding)

Could you help me understand what's going on ? Thank you very much ^^

PS : I use the Encrypt library : https://pub.dev/packages/encrypt

Here is a part of my Encrypter class

class AESEncryptor{
      var iv;
      var encrypter;
      var key;
    
      AESEncryptor(){
        this.key = Key.fromSecureRandom(16);
        this.iv = IV.fromLength(16);
        this.encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: null));
      }
    
      void setIV(iv){
        this.iv=iv;
      }
    
      IV getIv(){
        return(this.iv);
      }
    
      void setKey(key){
        this.key=key;
      }
    
      Key getKey(){
        return(this.key);
      }
    
      dynamic encrypt(data){ 
        if (data.runtimeType==Key){
          data=data.base16;
    
        }
        return encrypter.encrypt(data, iv: this.iv);
      }
    }

Solution

  • Thanks to Topaco I found out how to correct it

    New code

     dynamic encrypt(data){ 
        if (data.runtimeType==Key){
          data=toInt(combineByTwo(data.base16.split('')));
        }
        return encrypter.encryptBytes(data, iv: this.iv);
      }
    

    The two (dirty for the moment) functions I use :

    List<int> toInt(chaine){
      List<int> tab=[];
      for (var i=0;i<chaine.length;i++){
        tab.add(int.parse(chaine[i],radix:16));
      }
      return tab;
    }
    
    List combineByTwo(list){ 
      List tab=[];
      for (var i=0;i<list.length;i+=2){
        tab.add(list[i]+list[i+1]);
      }
      return tab;
    }