【问题标题】:Substitute mcrypt_encrypt (php) by shell_exec openssl用 shell_exec openssl 替换 mcrypt_encrypt (php)
【发布时间】:2018-09-10 22:04:29
【问题描述】:

需要用 shell_exec openssl 命令用密钥替换 mcrypt_encrypt (php)。 无法为现有的 sys 配置添加 mcrypt 库(openssl_encrypt),但可以在命令行中运行 openssl。但结果不同。需要帮助。

     <?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";
?>

结果不同:

mcrypt_encrypt:/+tHYRjnz2pvdljivqbDdQ==

openssl_encrypt:/+tHYRjnz2pvdljivqbDdQ==

openssl:qThMDYfZhk50rMWwj6j75w==

openssl(密钥打包在文件中):HbQjJ6iuaxCDrSr5T6wnkw==

可能是填充问题,可能是打包到十六进制,可能是算法。 在 openssl 中需要相同的内容。天呐!

【问题讨论】:

    标签: shell openssl mcrypt


    【解决方案1】:

    哦..不.. :)

    只需要使用密钥-K而不是-k

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-18
    • 2016-11-16
    • 2016-09-12
    • 1970-01-01
    • 2012-03-20
    • 2012-04-17
    • 2019-07-20
    • 2016-10-07
    相关资源
    最近更新 更多