Laravel - Encryption

Encryption is a process of converting a plain text to a message using some algorithms and third user party cannot read the information .it is very helpful for transmitting sensitive information . The text which is to be encrypted is termed as Plain Text and the text or the message obtained after the encryption is called Cipher Text. The process of converting cipher text to plain text is called Decryption.Laravel uses AES-256 and AES-128 encrypter, which uses Open SSL for encryption. All the values included in Laravel are signed using the protocol Message Authentication Code


The command used to generate the key in Laravel is shown below −

php artisan key:generate
The values for encryption are properly aligned in the config/app.php file, which includes two parameters for encryption namely key and cipher. If the value using this key is not properly aligned, all the values encrypted in Laravel will be insecure.

Encryption Process

Encryption of a value can be done by using the encrypt helper in the controllers of Laravel class. All the encrypted values are signed with Message Authentication code (MAC) to check for any modifications of the encrypted string.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DemoController extends Controller{
      * Store a secret message for the user.
      * @param Request $request
      * @param int $id
      * @return Response
   public function storeSecret(Request $request, $id){
      $user = User::findOrFail($id);
         'secret' => encrypt($request->secret)

Decryption Process

Decryption of the values is done with the decrypt helper. Observe the following lines of code −