Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenSSL error: EVP_DecryptFinal_ex:wrong final block length

I'm going back over some code [included at the bottom of this post] that I had previously written for a bit of encryption and I'm running into an error I can't find a way around. Whenever I try to decrypt my data I get the following error from OpenSSL:

error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

The output of my encrypt function is:

$AES-256-CBC$SHA256$kJ+DIShnFOs0cGsDiVnXXMdBj1GoLYywrhz+lv3W/dk=

The raw encypted data being:

00000000  90 9f 83 21 28 67 14 eb  34 70 6b 03 89 59 d7 5c  |...!(g..4pk..Y.\|
00000010  c7 41 8f 51 a8 2d 8c b0  ae 1c fe 96 fd d6 fd d9  |.A.Q.-..........|

And my IV being:

00000000  d7 4f f0 ee 8d a3 b9 80  6b 18 c8 77 db f2 9b bd  |.O......k..w....|

I can't figure out what's going wrong here. Most of what I can google boils down to "you didn't remove the IV from the beginning of the data", but it was never there in the first place.

Code:

Class MyEncrypt {

private $method, $iv_size, $hashfunc;

public function __construct($method='AES-256-CBC', $hashfunc='SHA512') {
    if( ! function_exists('openssl_encrypt') ) {
        Throw new Exception('openssl_encrypt() not supported.');
    } else if( ! in_array($method, openssl_get_cipher_methods()) ) {
        Throw new Exception('Encryption method ' . $method . ' not supported.');
    } else if( ! in_array(strtolower($hashfunc), hash_algos()) ) {
        Throw new Exception('Hashing method ' . $hashfunc . ' not supported.');
    }
    $this->method = $method;
    $this->hashfunc = $hashfunc;
    $this->iv_size = openssl_cipher_iv_length($this->method);
}

public function encrypt($password, $data) {
    $iv = $this->hashIV($password);
    $infostr = sprintf('$%s$%s$', $this->method, $this->hashfunc);
    return $infostr . openssl_encrypt($data, $this->method, $password, 0, $iv);
}

public function decrypt($password, $data) {
    $data_arr = explode('$', $data);
    if( ! count($data_arr) == 4 ) {
        throw new Exception('Bad input data.');
    }
//  var_export($data_arr);
    $iv = $this->hashIV($password);
//  echo base64_encode($iv);
    if( ! $res = openssl_decrypt($data, $this->method, $password, 0, $iv) ) {
        throw new Exception(openssl_error_string());
    } else { return $res; }
}

private function hashIV($password, $method=NULL) {
    if( is_null($method) || ! in_array(strtolower($method), hash_algos())) {
        $hashfunc = $this->hashfunc;
    } else {
        $hashfunc = $method;
    }
    $myhash = hash($hashfunc, $password, TRUE);
    while( strlen($myhash) < $this->iv_size ) {
        $myhash .= hash($hashfunc, $myhash, TRUE);
    }
    return substr($myhash, 0, $this->iv_size);
}

} // -- end class MyEncrypt -- //

$c = new MyEncrypt("AES-256-CBC", "SHA256");
$msg_enc = $c->encrypt('pass', 'blah blah this is my data!');

echo $msg_enc . "\n" . var_export($c->decrypt('pass', $msg_enc), true);
like image 656
Sammitch Avatar asked Sep 01 '25 02:09

Sammitch


1 Answers

The encrypt function that you have in the class MyEncrypt returns a '$' separated string of which only the last part is the base64-encoded encrypted data. You need to pass ONLY that part and not the prefix ($AES-256-CBC$SHA512).

So, if you pass $data_arr[3] to the openssl_decrypt function instead of $data, things work. I tried it with a php interpreter and below was the output.

> php -f x.php
$AES-256-CBC$SHA256$kJ+DIShnFOs0cGsDiVnXXMdBj1GoLYywrhz+lv3W/dk=
'blah blah this is my data!'
like image 154
Karthik Avatar answered Sep 02 '25 16:09

Karthik