【问题标题】:How to decrypt PHP Openssl encryption with BASH command如何使用 BASH 命令解密 PHP Openssl 加密
【发布时间】:2016-04-22 11:30:21
【问题描述】:

我正在用 PHP 加密密码,并想在不同的盒子上解密它。我没有运气,我希望能够直接从 bash 解密并回显它。下面是一个 PHP 测试的 sn-p。

$textToEncrypt    = "My super secret information.";
$encryptionMethod = "AES-256-CBC";  
$secretHash       = "Testkey";

//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash);

//To Decrypt
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash);

//Result
echo "Encrypted: $encryptedMessage <br>Decrypted: $decryptedMessage";

我已经尝试了多种方法在 Ubuntu 上对其进行解密,甚至将数据存储到文件中并将其输出到文件中。尝试的命令是:

openssl aes-256-cbc -a -d -k Testkey -in foo.txt -out secrets.txt

其中foo.txt是PHP加密返回的值,secrets.txt是输出。我该怎么做?

【问题讨论】:

  • 不使用初始化向量 (iv) 不是一个好主意;也许它不起作用,因为它害怕。
  • 如果我添加了这个,我的 php 会是什么样子,我认为它在 php 和 bash 之间是如何格式化的。
  • -k Testkey 将从密码Testkey 中派生出一个密钥,而在php 中openssl_encrypt(...,..., ' Testkey') 将直接使用Testkey 作为密钥。是否真的有必要以这种方式(即a)直接使用openssl命令行工具b)直接,中间没有另一个程序/脚本)?
  • 我不一定需要直接在命令行中运行它,我希望是从我的数据库中读取这个密钥并通过bash脚本解密它,因此需要任何工具来这样做,我很乐意答应。我只是认为通过 shell 的命令很容易,但我错了。如果任何其他工具也能做到这一点,请告诉我!
  • a) 命令行工具也可以是 php 脚本吗?可能是#!/bin/php shebang? b) 你不关心文件格式,只要它可以工作并且不是很糟糕?

标签: php bash encryption openssl


【解决方案1】:

值得重申,就像在 cmets 中一样,没有 IV 的加密是危险的。事实上,当前版本的 PHP 会发出警告。 IV可以使用openssl_random_pseudo_bytes()函数随机生成,并与加密文本一起以明文形式传输。它们不必保密,重要的是不要重复使用相同的密钥和IV组合,并且具有随机IV。

因此,除此之外,如果您查看 the source for the function,它不会将 password 参数作为密码传递,而是作为密钥传递。因此,要在命令行上使用openssl,它需要是十六进制并传递给-K 选项,而不是-k 选项。但是,你会得到一个错误,说“iv undefined”,所以你的 PHP 需要调整以包含一个:

<?php
$textToEncrypt    = "My super secret information.\n";
$encryptionMethod = "AES-256-CBC";  
$key              = "Testkey";
$iv               = openssl_random_pseudo_bytes(
                        openssl_cipher_iv_length($encryptionMethod)
                    );

$keyHex           = bin2hex($key);
$ivHex            = bin2hex($iv);

//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $key, 0, $iv);

//To Decrypt
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $key, 0, $iv);

//Result
printf(
    "Decrypted message: %s\n\nkeyHex=%s\nivHex=%s\nencryptedMessage=%s\n",
    $decryptedMessage,
    escapeshellarg($keyHex),
    escapeshellarg($ivHex),
    escapeshellarg($encryptedMessage)
);

获得这些详细信息后,您可以从命令行解密(在此处重新使用 PHP 变量名):

echo -n "$encryptedMessage" | openssl aes-256-cbc -d -a -A -K "$keyHex" -iv "$ivHex"

【讨论】:

  • 我已经按原样尝试了这段代码,但是在 linux 端,由于“错误的最终块长度”,我得到了一个 openssl 错误“错误解密”。命令: echo -n "o69dxUStEeJffwdvfiTjt0vOliLcNXZNbRrH18JbyRU=" | openssl aes-256-cbc -d -a -K "546573746b6579" -iv "34746f696a3335756e766f75716f7b32"
  • 是的,我也在尝试这个解决方案,并得到了与@QuickPrototype 相同的错误消息
  • @QuickPrototype 为 2019 年修复。迟到总比没有好,对吧?
【解决方案2】:

反过来

#!/bin/bash
#  create in bash keys
echo "generating private key"
openssl genrsa -out privkey.pem 2048
echo "signing private key"
openssl req -new -key privkey.pem -out certreq.csr -subj "/C=RO/ST=AB L=AB/O=None/OU=Department/CN=someweb.com"

echo "create a sign request"
openssl x509 -req -in certreq.csr -signkey privkey.pem -out newcert.pem
# end-of-bash-script
cp ./privkey.pem /path/to/apache/root/<some>

加密一些 json 文件

openssl smime -encrypt -aes256 -in ./json.txt -binary -outform DER -out ./json.xxx newcert.pem
# test decrypt here in bash
# openssl smime -decrypt -in json.xxx -inform DER -inkey privkey.pem -out json.dec

将其作为二进制文件发布到 php

curl --request POST --data-binary @./json.xxx http://localhost/<some/>json.php

然后是json.php脚本@apache root

<?php

    $rkey = file_get_contents("/var/www/html/privkey.pem");
    $pkey = file_get_contents("/var/www/html/newcert.pem");
    $data = file_get_contents("php://input");
    $fenc = tempnam("", "enc"); 
    $fdec = tempnam("", "dec");
    file_put_contents($fenc,$data);
    // openssl_pkcs7_decrypt ($fenc , $fdec , $pkey, $rkey ); unable to coerce parameter 3 to x509 cert 
    system("openssl smime -decrypt -in ${fenc} -inform DER -inkey privkey.pem -out ${fdec}");
    echo  file_get_contents($fdec);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2018-12-04
    • 2019-11-06
    • 2015-11-27
    • 1970-01-01
    相关资源
    最近更新 更多