【问题标题】:How to decrypt a file in PHP that was encrypted in bash script with Openssl如何解密使用 Openssl 在 bash 脚本中加密的 PHP 文件
【发布时间】:2019-11-06 02:40:02
【问题描述】:

我必须在我的 Laravel 后端使用 PHP 的 openssl_decrypt 解密一个文件。 该文件在 Laravel 外部通过 BASH 脚本和 openssl 1.1 加密。

我查看了一些对我有帮助的解释,但没有发现任何有用的。 我的 openssl 版本对于 PHP 和服务器都是相同的(1.1)。 我尝试了许多选项组合,但没有任何效果。

这是我的 BASH 加密脚本:

APP_KEY='**************'
FILES_PATH='****/app/files/'
# We're looking for ".decrypted" files, which gonna be encrypted next
FILES_LIST=$(find $FILES_PATH -type f -name '*.decrypted' )
# We base64-decode the key then display it as Hex. string
KEY=$(echo $APP_KEY | base64 -d -i | xxd -p -c 64)

# For each file to encrypt :
while read -r file; do
  # If there is actually a file :
  if [ ! -z "$file" ]; then
    output=${file%.decrypted}
    chunkspath="${output}.chunk."
    chunksname="${chunkspath##*/}*"
    # We have to split the files into 32M. chunks because our server can't decrypt big files without memory leaks in php.
    split -b 32M -d $file -a 3 $chunkspath
    chunkslist=$(find $FILES_PATH -type f -name $chunksname)
    touch $output
    # For each chunk :
    while read -r chunk; do
      # Generate a random IV of 16 bytes (output is 32 characters in Hex.)
      iv=$(openssl rand -hex 16)
      temp="${chunk}.enc"
      openssl AES-256-CBC -K $KEY -iv $iv -in $chunk -out $temp
      # We prefix each chunk with his IV to let the server retrieve it when decrypting. See the PHP code bellow.
      echo -n $iv >> $output
      # Then we append each IV+Chunk in the final output file.
      cat $temp >> $output
      rm $temp $chunk
    done < <(echo "${chunkslist}")
    # I commented the next line to let me run some tests but the original file will have to be removed eventually.
    # rm $file
  fi
done < <(echo "${FILES_LIST}")

echo 'Done'

这是我解密文件的 PHP 脚本:

// This function is inside a Laravel's stream download response 
    function () use ($file, $log) { // $file is the Laravel model representation of a stored file.
        $cipher = 'AES-256-CBC';
        $key = base64_decode(substr(config('app.key'), 7));
        $ivLen = 32; // Actually 16 bytes but 32 characters in hex format
        $chunckLen = 32 * 1024 * 1024; // Chunks are 32Mo long or less for the last one
        $fpIn = fopen(Storage::disk('files')->path($file->path), 'rb');
        while (!feof($fpIn)) {
            // IV and Key must be in binary format
            $iv = hex2bin(fread($fpIn, $ivLen));
            // We read the file chunks by chunks, decrypt it and print it.
            $encrypted = fread($fpIn, $chunckLen);
            $decrypted = openssl_decrypt($encrypted, $cipher, $key, OPENSSL_RAW_DATA, $iv);
            while ($msg = openssl_error_string()) {
                print $msg . "\n";
            }
            print($decrypted);
        }
    }

我希望解密后的文件是可读的。 使用上面的代码,输出文件包含这一行:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt 我尝试使用 openssl_decrypt() 方法上的 OPENSSL_ZERO_PADDING 选项。然后输出文件包含二进制数据,但文件不可读(我猜加密没有按预期工作)。 我还尝试在 bash 脚本中设置 -nopad 选项,但随后抛出此错误:4960:error:0607F08A:digital envelope routines:EVP_EncryptFinal_ex:data not multiple of block length:../openssl-1.1.1c/crypto/evp/evp_enc.c:425:。 有人知道错误是什么意思吗?我做错了什么 ?我几乎可以肯定我错过了一些东西(我是加密世界的新手......)

谢谢!

【问题讨论】:

    标签: php encryption openssl php-openssl


    【解决方案1】:

    实际上我才意识到脚本中使用的 Key 和 Laravel 中使用的 Key 不一样... 愚蠢的我。 因此上面的代码是正确的并且解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2012-04-07
      • 2020-04-14
      • 2018-11-28
      • 2019-04-20
      • 2013-05-21
      相关资源
      最近更新 更多