-1

Need to substitute mcrypt_encrypt (php) by shell_exec openssl command with keys. No possible to add mcrypt lib (openssl_encrypt too) for exist sys configuration, but possible to run openssl in command line. But results is different. Need help.

     <?php
        # -----Encrypt -----
        $key = pack('H*',"189cebc45c7caec5c57894564c52ae5646ed4564565ccc4565ec555dd5dd4d54");
        file_put_contents("k.key",$key);
        echo "Key: " . $key . "\n";

        $key_size =  strlen($key);
        echo "Key size: " . $key_size . "\n";

        $iv = pack('H*', "e4554c4564a5454cc45654a45654ce44");
        echo "Vector: " . $iv . "\n";

        $plaintext = "Thisstringff";
        $block = 16;
        $pad   = $block - (strlen($plaintext) % $block);
        $plaintextn = $plaintext.str_repeat(chr($pad), $pad);

        echo "Source:" . $plaintext . "<-\n";
        echo "size:".strlen($plaintext). "<-\n";
        echo "Source padding:" . $plaintextn . "<-\n";
        echo "size padding:".strlen($plaintextn). "<-\n";

        file_put_contents("pt.in",$plaintext);
        file_put_contents("ptn.in",$plaintextn);

        $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$plaintextn, MCRYPT_MODE_CBC,$iv);

        $ciphertext_base64 = base64_encode($ciphertext);

        echo  "\n"."Encrypted:".$ciphertext_base64 . "\n\n";

# --- Decrypt ---

        $ciphertext_dec = base64_decode($ciphertext_base64);

        $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$ciphertext_dec, MCRYPT_MODE_CBC,$iv);

        echo  "Decrypted:".$plaintext_dec . "\n\n";

# --- Encrypt via openssl_encrypt ----

   function sslEncrypt128($str, $secret,$iv)
            {
                return base64_encode(openssl_encrypt($str, 'aes-256-cbc', $secret, OPENSSL_RAW_DATA,$iv));
            }

        echo  "\n"."Encrypted by openssl_encrypt:";
        var_dump(sslEncrypt128($plaintext, $key,$iv));

# --- Encrypt via command line ---         
            $shkey="189cebc45c7caec5c57894564c52ae5646ed4564565ccc4565ec555dd5dd4d54";
$shkeypack="k.key";
$shiv="e4554c4564a5454cc45654a45654ce44";

#$cmd='echo '.$plaintext.' | openssl aes-256-cbc -nosalt -a -k '.$shkey.' -iv '.$shiv;

$cmd='openssl aes-256-cbc  -in "pt.in" -nosalt -a -A -k "'.$shkey.'" -iv "'.$shiv.'"';
echo "\nCommand:".$cmd."\n";
$output = shell_exec($cmd);
echo "\n"."Encrypted openssl:$output"."\n";

$cmd='openssl aes-256-cbc  -in "pt.in" -nosalt -a -A -kfile "'.$shkeypack.'" -iv "'.$shiv.'"';
echo "\nCommand:".$cmd."\n";
$output = shell_exec($cmd);
echo "\n"."Encrypted openssl kfile:$output"."\n";
?>

Results is different:

mcrypt_encrypt:/+tHYRjnz2pvdljivqbDdQ==

openssl_encrypt:/+tHYRjnz2pvdljivqbDdQ==

openssl:qThMDYfZhk50rMWwj6j75w==

openssl (key packed in file):HbQjJ6iuaxCDrSr5T6wnkw==

May be padding problem, may be pack to hex, may be algorithm. Need the same in openssl. Tnx!

DJArty
  • 11
  • 2

1 Answers1

1

Oh.. no.. :)

Just need to use key -K not -k

DJArty
  • 11
  • 2
  • See [OpenSSL enc](https://www.openssl.org/docs/manmaster/man1/enc.html) for command line option descriptions. – zaph Apr 01 '18 at 15:24