OpenSSL command line BASE64 codec

Source: Internet
Author: User
Tags openssl enc

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.