title: Working with encrypted files in Laravel (how to download decrypted file)
body: <p>In my webapp, users can upload files. Before being saved and stored, the contents of the file are encrypted using something like this:</p>
  
  <pre><code>Crypt::encrypt(file_get_contents($file-&gt;getRealPath()));
  </code></pre>
  
  <p>I then use the file system that comes with Laravel to move the file</p>
  
  <pre><code>Storage::put($filePath, $encryptedFile);
  </code></pre>
  
  <p>I have a table to store information about each file with columns such as:</p>
  
  <ul>
  <li>id</li>
  <li>file_path</li>
  <li>file_name</li>
  <li>original_name (includes the extension)</li>
  </ul>
  
  <p>Now I want the user to be able to download this encrypted file. However, I'm having trouble decrypting the file and returning it to the user. In the <a href="https://laravel.com/docs/5.1/responses#file-downloads" rel="nofollow">file downloads response section</a> of the Laravel documentation, it suggests to do this:</p>
  
  <pre><code>return response()-&gt;download($pathToFile, $name, $headers);
  </code></pre>
  
  <p>It wants a file path which is fine, but at which point can I decrypt the file contents so that it is actually readable? </p>
  
  <p>I do seem to be able to do this: </p>
  
  <pre><code>$encryptedContents = Storage::get($fileRecord-&gt;file_path);
  $decryptedContents = Crypt::decrypt($encryptedContents);
  </code></pre>
  
  <p>... but I don't know how to return it as a download with a specified file name.</p>