1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Error Invalid argument(s): Input data length must be a multiple of cipher's...

Discussão em 'Mobile' iniciado por Stack, Novembro 6, 2024 às 11:22.

  1. Stack

    Stack Membro Participativo

    I have an error when trying to decrypt in dart language. with message:


    Invalid argument(s): Input data length must be a multiple of cipher's block size

    Here is sample PHP code

    function encryptDomainWithToken($domain, $token)
    {
    // Encryption method using OpenSSL with AES-256-CBC
    $method = 'AES-256-CBC';
    $ivLength = openssl_cipher_iv_length($method); // IV length
    $iv = openssl_random_pseudo_bytes($ivLength); // Initialization Vector
    $key = hash('sha256', $token, true); // Derive encryption key from token

    // Data to encrypt
    $data = $domain;

    // Encrypt data
    $encryptedData = openssl_encrypt($data, $method, $key, 0, $iv);

    // Combine IV with encrypted data and encode in base64
    return base64_encode($iv . $encryptedData);
    }



    Here is dart code

    import 'dart:convert';
    import 'package:crypto/crypto.dart';
    import 'package:encrypt/encrypt.dart';

    String decryptDomainWithToken(String encrypted, String token) {
    final method = AESMode.cbc;

    // Derive key from token using SHA-256
    final key = sha256.convert(utf8.encode(token)).bytes;

    final encrypter = Encrypter(AES(Key(Uint8List.fromList(key)), mode: method));

    // Decode the base64 encrypted string
    final decodedData = base64.decode(encrypted);

    // Determine IV length for AES-256-CBC
    final ivLength = 16;
    final iv = IV(Uint8List.fromList(decodedData.sublist(0, ivLength)));

    final encryptedData = Encrypted(Uint8List.fromList(decodedData.sublist(ivLength)));

    // Decrypt the data
    return encrypter.decrypt(encryptedData, iv: iv);

    }


    How to fix it?

    Continue reading...

Compartilhe esta Página