Laravel - Encryption

Encryption is a process of changing over a plain text to a message utilizing a few algorithms and third client party can't peruse the data .it is useful for transmitting touchy data . The text which is to be encrypted is named as Plain Text and the text or the message acquired after the encryption is called Cipher Text. The way toward changing over figure text to plain text is called Decryption.Laravel utilizes AES-256 and AES-128 encrypter, which utilizes Open SSL for encryption. Every one of the qualities incorporated into Laravel are marked utilizing the convention Message Authentication Code


The command used to produce the key in Laravel is appeared beneath −

php artisan key:generate 

The qualities for encryption are legitimately adjusted in the config/app.php record, which incorporates two parameters for encryption to be specific key and figure. On the off chance that the esteem utilizing this key isn't appropriately adjusted, every one of the qualities encrypted in Laravel will be uncertain.

Encryption Process

Encryption of an esteem should be possible by utilizing the encrypt helper in the controllers of Laravel class. All the encryptd qualities are marked with Message Authentication code (MAC) to check for any adjustments of the encrypted string.

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Controllers\Controller; 

class DemoController extends Controller{ 


* Store a mystery 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) 

])- >save(); 



Decryption Process

Decryption of the qualities is finished with the decrypt helper. Observe the following lines of code −

use Illuminate\Contracts\Encryption\DecryptException;

// Exception for decryption thrown in facade
try {
   $decrypted = decrypt($encryptedValue);
} catch (DecryptException $e) {