1

I tried to migrate my encryption from mcrypt to openssl but the decryption keeps failing. The code Below shows my encryption function. I placed the var_dump in the encryption function to verify decryption with identical variables works, which it does not.

I tried the following changes to the code, none worked:

  • base64_decode($encrypted)
  • just out of curiosity base64_encode($encrypted)
  • all these variants with options set to:0(default value),OPENSSL_RAW_DATA,OPENSSL_ZERO_PADDING

function encryptString($data){  
    $key = "1A534";

    do{
        $iv = random_bytes(100);
    }while(strpos($iv,"|Z|")!==false);

    $encrypted = openssl_encrypt($data,"aes-256-gcm",$key,false,$iv);   
    var_dump(openssl_decrypt($encrypted,"aes-256-gcm",$key,OPENSSL_RAW_DATA,$iv));
    $output = $iv . "|Z|" . $encrypted;
    $output = base64_encode($output);   
    return $output;
}

PHP Version is 7.0.5-1+deb.sury.org~trusty+1

OpenSSL Version is OpenSSL 1.0.2g 1 Mar 2016

Why keeps openssl_decrypt returning false? I successfully encrypted with the same variables just the line above.

3
  • 1
    I think this won't work until PHP 7.1, because there is a bug with the authentication tag. Commented Mar 31, 2016 at 20:29
  • Seems right. None GCM ciphers work correctly. Commented Mar 31, 2016 at 20:39
  • I'm using 7.1 in this scenario but still getting a false returned in all scenarios. Commented Oct 18, 2017 at 15:15

1 Answer 1

1

It will not work for PHP < 7.1 as AEAD is not supported.

You can use the library I created (PHP 5.4+ and 7.0+).

Depending on your environment, it will test and use the following methods:

Note that the pure PHP method is very slow compared to the other methods.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.