Home> PHP Framework> Laravel> body text

Laravel - Encryption

PHPz
Release: 2024-08-27 11:59:54
Original
631 people have browsed it

Encryption is a process of converting a plain text to a message using some algorithms such that any third user cannot read the information. This is helpful for transmitting sensitive information because there are fewer chances for an intruder to target the information transferred.

Encryption is performed using a process calledCryptography. The text which is to be encrypted is termed asPlain Textand the text or the message obtained after the encryption is calledCipher Text. The process of converting cipher text to plain text is calledDecryption.

Laravel usesAES-256andAES-128encrypter, which uses Open SSL for encryption. All the values included in Laravel are signed using the protocolMessage Authentication Codeso that the underlying value cannot be tampered with once it is encrypted.

Configuration

The command used to generate thekeyin Laravel is shown below −

php artisan key:generate
Copy after login

Please note that this command uses the PHP secure random bytes’ generator and you can see the output as shown in the screenshot given below −

Artisan Key

The command given above helps in generating the key which can be used in web application. Observe the screenshot shown below −

Note

The values for encryption are properly aligned in theconfig/app.phpfile, which includes two parameters for encryption namelykeyandcipher. 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 theencrypt helperin the controllers of Laravel class. These values are encrypted using OpenSSL and AES-256 cipher. All the encrypted values are signed with Message Authentication code (MAC) to check for any modifications of the encrypted string.

Laravel - Encryption

The code shown below is mentioned in a controller and is used to store a secret or a sensitive message.

fill([ 'secret' => encrypt($request->secret) ])->save(); } }
Copy after login

Decryption Process

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

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

Please note that if the process of decryption is not successful because of invalid MAC being used, then an appropriate exception is thrown.

The above is the detailed content of Laravel - Encryption. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!