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

时间:2019-06-24 09:52:34

标签: php encryption openssl php-openssl

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

我看了一些对我有帮助的解释,但没有发现任何有效的方法。 对于PHP和服务器,我的openssl版本是相同的(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:。 有人知道错误意味着什么吗?我做错了什么 ?我几乎可以肯定我错过了一些东西(我是加密世界中的新人……)

谢谢!

1 个答案:

答案 0 :(得分:0)

实际上,我只是意识到脚本中使用的密钥和Laravel中使用的密钥并不相同……愚蠢的我。 因此,上面的代码是正确的,可以解决我的问题。