Laravel
Laravel

How to use Laravel Encryption

Laravel Programming encrypter uses  Open Secure Sockets Layer (SSL). It uses the AES-256 and AES-128 encryption Architecture for communication throw the client request and server response. encrypted values are signed using a message authentication code (MAC).
Configuration
You Must set a key option in your config/app.php configuration file
You should use the

 php artisan key: generate

a command to generate this key since this Artisan command will use PHP’s secure random bytes generator to build your key.
Using The Encrypter
Encrypting A Value
Encrypt a value using the encrypt helper. All encrypted values are encrypted using OpenSSL and the AES-256-CBC cipher.

<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\User; use Illuminate\Http\Request; class UserController 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); $user->fill([
            'secret' => encrypt($request->secret),
        ])->save();
    }
}

Encrypting Without Serialization

use Illuminate\Support\Facades\Crypt;

$encrypted = Crypt::encryptString('Hello world.');

$decrypted = Crypt::decryptString($encrypted);

Decrypting A Value
Decrypt values using the decrypt helper.

use Illuminate\Contracts\Encryption\DecryptException;

try {
    $decrypted = decrypt($encryptedValue);
} catch (DecryptException $e) {
    //
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.