Sergio Vilchis Sergio Vilchis - 2 months ago 25
PHP Question

PHP OpenSSL encrypt with DES-CBC algorithm unexpected output

I want to change all the passwords of a given DB. All this passwords are cipher with DES CBC PKCS7, I have the key and the initialization vector. I've used the next web page http://www.txtwizard.net/crypto to decrypt one password and the result was the expected (using options DES, CBC and PKCS7).

key: RRZy0njZDzw=

iv: p/34qWLNYfg=

plain text 123123

crypted text x541kJ4KvJo=

But when the next code written in PHP I can't replicate the result:

<?php
$key = "RRZy0njZDzw=";
$iv = "p/34qWLNYfg=";
$data = "123123";
$cipher='DES-CBC';
var_dump(openssl_get_cipher_methods());
$encrypted = openssl_encrypt($data,$cipher, $key,OPENSSL_RAW_DATA,$iv);
echo base64_encode($encrypted);
?>


The output of this code is:

PHP Warning: openssl_encrypt(): IV passed is 12 bytes long which is longer than the 8 expected by selected cipher, truncating in /home/sergio/Documents/DevEnvTest/siaf/aes2hash.php on line 7

Warning: openssl_encrypt(): IV passed is 12 bytes long which is longer than the 8 expected by selected cipher, truncating in /home/sergio/Documents/DevEnvTest/siaf/aes2hash.php on line 7
gB7ahDoYZqI=


Is there a way to get the same string that the online tool?

Answer

You need to decode your key and IV from Base64:

$key = base64_decode("RRZy0njZDzw=");
$iv = base64_decode("p/34qWLNYfg=");
Comments