dowbwrr3590709 2019-06-24 09:52
浏览 241

如何解密用Openssl在bash脚本中加密的PHP文件

I have to decrypt a file in my Laravel backend with PHP's openssl_decrypt. The file is encrypted outside Laravel fro ma BASH script and openssl 1.1.

I looked at some explanations that would help me but found nothing that worked. My openssl versions are the same (1.1) for both PHP and the server. I tried many combinations of options but nothing worked.

Here is my BASH encrypting script:

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'

And here's my PHP's script for decrypting the file:

// 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 . "
";
            }
            print($decrypted);
        }
    }

I expect the decrypted file to be readable. With the code above, the output file contains this line: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt I tried with the OPENSSL_ZERO_PADDING options on the openssl_decrypt() method. The output file then contains binary data but the file is not readable (I guess the ecryption did not work as expected). I also tried to set the -nopad option in the bash script but then this error is thrown: 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:. Does someone know what there errors mean ? What did I do wrong ? I'm almost sure I missed something (I'm kind of new in the encryption world...)

Thank you!

  • 写回答

1条回答 默认 最新

  • drxvjnx58751 2019-06-24 11:28
    关注

    Actually I just realized that the Key used in the script and the one used in Laravel weren't the same... Stupid me. The code above is therefore correct and resolves my problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号