OpenSSL to Base64 codec specification support is poor, using it to encode and decode the results of other languages such as PHP processing is not convenient, notice a few of the following collation
1, if the PHP encryption results do base64 encoding length less than 64, you need to add a newline character OpenSSL to decode;
2,php needs to insert a newline character for Base64 encoding results every 64 characters, and OpenSSL decodes it. (The reason is that the OpenSSL default bufsize buffer size is 16k, but once the compute buffers involving base64 are only 80 bytes, the calculation fails once the encoding result exceeds 80 bytes, Base64 codec cannot change the buffer size)
The sample code is as follows:
One, PHP encrypted OpenSSL decryption
<?PHPfunctionStrtohex ($x) { $s= ' '; foreach(Str_split($x) as $c)$s.=sprintf("%02x",Ord($c)); return($s); } $source=file_get_contents("Phpencrypt.plain"); $iv= "1234567812345678"; $pass= ' a12ssdamqweret09 '; $method= ' AES-128-CBC '; $content=Base64_encode(Openssl_encrypt ($source,$method,$pass,true,$iv)); $newcontent= ""; for($i= 0,$len=strlen($content);$i<$len;$i+ = 64){ $newcontent=$newcontent.substr($content,$i, 64); if($i+ <$len){ $newcontent. = "\ n"; } if($len< 64){ $newcontent. = "\ n"; } } file_put_contents('./phpencrypt.encrypted ',$newcontent); $exec= "OpenSSL enc-".$method. "-d-a-bufsize 1-in phpencrypt.encrypted-out phpencrypt.plain2-nosalt-nopad-k". Strtohex ($pass). "-iv". Strtohex ($iv); Echo $exec." \ n "; exec($exec); $source=file_get_contents("Phpencrypt.plain2"); Echo Trim($source);?>
Two, OpenSSL encrypted PHP decryption
<?PHPfunctionStrtohex ($x) { $s= ' '; foreach(Str_split($x) as $c)$s.=sprintf("%02x",Ord($c)); return($s); } $iv= "1234567812345678"; $pass= ' a12ssdamqweret09 '; $method= ' AES-128-CBC '; Echo"OpenSSL enc-aes-128-cbc-a-in phpdecrypt.plain-out phpdecrypt.encrypted-k". Strtohex ($pass). "-iv". Strtohex ($iv)." \ n "; Echo exec("OpenSSL enc-aes-128-cbc-a-in phpdecrypt.plain-out phpdecrypt.encrypted-k". Strtohex ($pass). "-iv". Strtohex ($iv)); $content=file_get_contents('./phpdecrypt.encrypted '); $data= Openssl_decrypt (Base64_decode($content),$method,$pass,true,$iv); Echo Trim($data);?>
OpenSSL command line BASE64 codec