According to the documentation of OpenSSL
( https://www.openssl.org/docs/apps/enc.html#OPTIONS ) they expect a hex-digit
value for key
and iv
; does that mean only numbers? or will a md5
hash do? (Because a md5
doesn't seem reversible)
- Note i'm mentioning
key
andiv
because$password
in thePHP
functionopenssl_encrypt
is actually the key.
(Almost) straight from PHP
comments ( http://php.net/manual/en/function.openssl-encrypt.php )
function strtohex($x)
{
$s='';
foreach (str_split($x) as $c) $s.=sprintf("%02X",ord($c));
return($s);
}
$source = 'It works !';
$iv = substr( md5( "123sdfsdf4567812345678" ), 0, 16 );
$pass = '1234567812345678';
$method = 'aes-256-cbc';
echo "\niv in hex to use: ".$iv;
echo "\nkey in hex to use: ".strtohex($pass);
echo "\n";
file_put_contents ('./file.encrypted',openssl_encrypt ($source, $method, $pass, true, $iv));
$exec = "openssl enc -".$method." -d -in file.encrypted -nosalt -nopad -K ".strtohex($pass)." -iv ".$iv;
echo 'executing: '.$exec."\n\n";
echo exec ($exec);
echo "\n";