PHP Warning: openssl_decrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher

12,950

From the manual:

$iv is as in the case of $password, a String of bytes.

It looks like you have $iv encoded as a hexadecimal string:

$iv = '################################';

You probably just need to convert it to a binary byte string:

$iv = hex2bin('################################');

Ditto for $key.

Share:
12,950

Related videos on Youtube

Jonatas Rodrigues
Author by

Jonatas Rodrigues

Updated on June 04, 2022

Comments

  • Jonatas Rodrigues
    Jonatas Rodrigues almost 2 years

    I need to implement openSSL in my application, because we had to change the version from 5.6 to version 7.2

    I would like to understand how to solve this problem.

    Any doubts that arise about my environment, or how I am using the application, just ask that I will respond.

    <?php
    
    $dados = '#########################################################';
    
    $key = '################################';
    $iv = '################################';
    
    /**
     * running in version 7.2
     */
    $data = openssl_decrypt($dados, 'AES-128-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
    /**
     * Erro:
     * $ clear && ./php descriptografa-senha.php
     *
     * PHP Warning:  openssl_decrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher, truncating in ./descriptografa-senha.php on line 12
     * PHP Stack trace:
     * PHP   1. {main}() ./descriptografa-senha.php:0
     * PHP   2. openssl_decrypt() ./descriptografa-senha.php:12
     *
     * Warning: openssl_decrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher, truncating in ./descriptografa-senha.php on line 12
     *
     * Call Stack:
     *     0.4121     125736   1. {main}() ./descriptografa-senha.php:0
     *     0.4121     126312   2. openssl_decrypt() ./descriptografa-senha.php:12
     */
    
    /**
     * running in version 5.6
     */
    $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
    $mcrypt_mode = MCRYPT_MODE_CBC;
    $encrypted = $dados;
    
    $iv_utf = mb_convert_encoding($iv, 'UTF-8');
    $decrypted = mcrypt_decrypt($mcrypt_cipher, $key, base64_decode($encrypted), $mcrypt_mode, $iv_utf);
    
    print_r($decrypted);
    /**
     * performed normally
     */
    

    This was the code that generated the encryption, see if you can understand something and help me in the problem, the language to use to encrypt is C # and the language to decrypt and PHP 7.2

    public static string Cryptography(string text, string pass = "#############################")
    {
        if (string.IsNullOrEmpty(pass))
            return string.Empty;
    
        var rijndaelmanaged = new RijndaelManaged();
        rijndaelmanaged.KeySize = 256;
        rijndaelmanaged.BlockSize = 256;
        rijndaelmanaged.Padding = PaddingMode.PKCS7;
        rijndaelmanaged.Key = Convert.FromBase64String(Base64Encode(pass));
    
        rijndaelmanaged.IV = Convert.FromBase64String(Base64Encode(vetorInicializacao));
    
        var encrypt = rijndaelmanaged.CreateEncryptor(rijndaelmanaged.Key, rijndaelmanaged.IV);
        byte[] xBuff = null;
        using (var ms = new MemoryStream())
        {
            using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
            {
                byte[] xXml = Encoding.UTF8.GetBytes(text);
                cs.Write(xXml, 0, xXml.Length);
            }
    
            xBuff = ms.ToArray();
        }
    
        String out = Convert.ToBase64String(xBuff);
        return out;
    }
    
    • DimeCadmium
      DimeCadmium over 5 years
      mcrypt is still available in PHP7, through PECL: pecl.php.net/package/mcrypt .
    • Jonatas Rodrigues
      Jonatas Rodrigues over 5 years
      Strange, because in the documentation itself it was removed in version 7.2 [php.net]php.net/manual/en/function.mcrypt-decrypt.php
    • Jonatas Rodrigues
      Jonatas Rodrigues over 5 years
      DimeCadmium, brother left there in the comments to help the guys, how would I use this, for example I have two linux and windows environments, could show how could generate the dll and so
    • Jonatas Rodrigues
      Jonatas Rodrigues over 5 years
      I was checking here and have a library on github that does that would be the ideial ?? Its name is phpseclib/mcrypt_compat.
  • Jonatas Rodrigues
    Jonatas Rodrigues over 5 years
    I tested your suggestion and presented this error, I am trying to fix this, but informing the error that is happening in my maqui: PHP Warning: hex2bin(): Input string must be hexadecimal string in ./descriptografa-senha.php on line 8\n PHP Stack trace: PHP 1. {main}() ./descriptografa-senha.php:0 PHP 2. hex2bin() ./descriptografa-senha.php:8
  • Alex Howansky
    Alex Howansky over 5 years
    Ok... so what's the string then? Base64?
  • Jonatas Rodrigues
    Jonatas Rodrigues over 5 years
    Alex Howansky, brother I put the code in C # to see if I can explain the problem to you, and you can help me do it the right way
  • Jonatas Rodrigues
    Jonatas Rodrigues over 5 years
    Alex Howansky, brother C # code is just below, I will put your name to know that it is referenced to you