Laravel provides inbuilt library to encrypt and decrypt the strings. Laravel usage OpenSSL to provide AES-256 encryption which creates long encrypted string. Encryption and decryption is a technique to hide the useful some information in a form which is not readable.

In this tutorial i will show you to encrypt and decrypt the string using laravel `crypt` library. I will take simple example of a string and then will encrypt and decrypt in controller.

Below is the syntax

``````<?php
encrypt(\$string);
decrypt(\$encrypted_string);

//OR

Crypt::encryptString(\$string);
Crypt::decryptString(\$encrypted_string);

``````

So let’s take an simple example of encrypt and decrypt string in laravel 8

Step 1 : Generate App key

Before using the crypt class make sure you have generated the app key and if not then generate using artisan command as below.

``php artisan key:generate``

Step 2 : Create a controller

Next step is to create the controller and add the necessary imports and class. You can create by Laravel artisan or manually.

``php artisan make:controller PostController``

Now, add `use Illuminate\Support\Facades\Crypt;` and methods

``````<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

class PostController extends Controller
{

public function index()
{
\$decrypted_string = Crypt::decryptString(\$encrypted_string);

//or using helper function

\$decrypted_string = decrypt(\$encrypted_string);

dump(\$encrypted_string);
dump(\$decrypted_string);
return  "DONE";
}

}
``````

Here we used `Crypt::encryptString` and `encrypt` to encrypt the string and `Crypt::decryptString` and `decrypt` to decrypt the string.

Step 3 : Create Routes

Thirst step is to create the routes to check our implementation

``````<?php
{"ft_lang_label":"__label__en","ft_lang_prob":0.55474347,"math_prob":0.5663215,"size":2131,"snap":"2023-14-2023-23","text_gpt3_token_len":449,"char_repetition_ratio":0.19746122,"word_repetition_ratio":0.015384615,"special_character_ratio":0.20412952,"punctuation_ratio":0.16893733,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95687264,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-04T14:38:11Z\",\"WARC-Record-ID\":\"<urn:uuid:d0594aad-4d68-4541-b6c1-da7709104858>\",\"Content-Length\":\"207419\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dc1f1b7c-78d2-4c51-9846-41d6ee0b7058>\",\"WARC-Concurrent-To\":\"<urn:uuid:3690bd5b-3e28-48b3-8931-d1ba421481c6>\",\"WARC-IP-Address\":\"89.117.157.191\",\"WARC-Target-URI\":\"https://readerstacks.com/how-to-encrypt-and-decrypt-string-in-laravel-8/\",\"WARC-Payload-Digest\":\"sha1:I3OFRQ4IU3FKMEVHZTAR5JEBQF6LRY7G\",\"WARC-Block-Digest\":\"sha1:Q6NCZP3WTIBTIJUEH6ECECCLJRQL27YU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224649986.95_warc_CC-MAIN-20230604125132-20230604155132-00076.warc.gz\"}"}