note 104438 added to function.openssl-encrypt
| From: | biohazard dot ge at gmail dot com | Date: | Wed, 15 Jun 2011 13:48:56 +0000 |
| Subject: | note 104438 added to function.openssl-encrypt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180548@lists.php.net to get a copy of this message | ||
Many users give up with handilng problem when openssl command line tool cant decrypt php openssl
encrypted file which is encrypted with openssl_encrypt function.
For example how beginner is encrypting data:
<?php
$string = 'It works ? Or not it works ?';
$pass = '1234';
$method = 'aes128';
file_put_contents ('./file.encrypted', openssl_encrypt ($string, $method, $pass));
?>
And then how beginner is trying to decrypt data from command line:
# openssl enc -aes-128-cbc -d -in file.encrypted -pass pass:123
Or even if he/she determinates that openssl_encrypt output was base64 and tries:
# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -pass pass:123
Or even if he determinates that base64 encoded file is represented in one line and tries:
# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -A -pass pass:123
Or even if he determinates that IV is needed and adds some string iv as encryption function`s fourth
parameter and than adds hex representation of iv as parameter in openssl command line :
# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -pass pass:123 -iv -iv
31323334353637383132333435363738
Or even if he determinates that aes-128 password must be 128 bits there fore 16 bytes and sets $pass
= '1234567812345678' and tries:
# openssl enc -aes-128-cbc -d -in file.encrypted -base64 -pass pass:1234567812345678 -iv -iv
31323334353637383132333435363738
All these troubles will have no result in any case.
BECAUSE THE PASSWORD PARAMETER DOCUMENTED HERE IS NOT THE PASSWORD.
It means that the password parameter of the function is not the same string used as [-pass pass:]
parameter with openssl cmd tool for file encryption decryption.
IT IS THE KEY !
And now how to correctly encrypt data with php openssl_encrypt and how to correctly decrypt it from
openssl command line tool.
<?php
function strtohex($x)
{
$s='';
foreach (str_split($x) as $c) $s.=sprintf("%02X",ord($c));
return($s);
}
$source = 'It works !';
$iv = "1234567812345678";
$pass = '1234567812345678';
$method = 'aes-128-cbc';
echo "\niv in hex to use: ".strtohex ($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 ".strtohex($iv);
echo 'executing: '.$exec."\n\n";
echo exec ($exec);
echo "\n";
?>
IV and Key parameteres passed to openssl command line must be in hex representation of string.
The correct command for decrypting is:
# openssl enc -aes-128-cbc -d -in file.encrypted -nosalt -nopad -K 31323334353637383132333435363738
-iv 31323334353637383132333435363738
As it has no salt has no padding and by setting functions third parameter we have no more base64
encoded file to decode. The command will echo that it works...
: /
----
Server IP: 69.147.83.197
Probable Submitter: 95.104.113.130
----
Manual Page -- http://www.php.net/manual/en/function.openssl-encrypt.php
Edit -- https://master.php.net/note/edit/104438
Del: integrated -- https://master.php.net/note/delete/104438/integrated
Del: useless -- https://master.php.net/note/delete/104438/useless
Del: bad code -- https://master.php.net/note/delete/104438/bad+code
Del: spam -- https://master.php.net/note/delete/104438/spam
Del: non-english -- https://master.php.net/note/delete/104438/non-english
Del: in docs -- https://master.php.net/note/delete/104438/in+docs
Del: other reasons-- https://master.php.net/note/delete/104438
Reject -- https://master.php.net/note/reject/104438
Search -- https://master.php.net/manage/user-notes.php