Encryption

In this you will see how you can use encryption and decryption in PHP level. For this new project is created and you are going to run the project through command “PHP artisan serve”.

So now the project will run. Let’s have a look at the code-:

– As in config folder you have “app.php” file which has a key variable which is reffering to “.env file” as-:

‘key’ => env(‘APP_KEY’),

‘cipher’ => ‘AES-256-CBC’,

Let’s open env file to see where the app key is and it has security key which will be used for the encryption and decryption and this is the free security in level.

– But if you don’t have the key variable in your env file then you can run this command-:

“PHP artisan key generate”

– This will generate a key and now you need to make a new controller with php artisan command as-:

“php artisan make: controller EncryptionController”

– You can find this encrypt controller in App folder> Http>controller>Encryptioncontroller.php.

– In encryption controller you have to include the crypt library as-:

Use Illuminate\Support\Facades\Crypt;

– Then create the two methods- one is encrypt and other is decrypt. Include method will encrypt the text.


     Class EncryptionController extends Controller
{
       Public function encrypt () {
          $encrypted = Crypt :: encryptString(‘Hello, this is a test message’);
          Return $encrypted;
}

Public function decrypt () {
}

– In “web.php” create two routes – one for the encrypt and the other for decrypt as-:

Route::get (‘/encrypt’, ‘EncryptionController@encrypt’);

Route::get (‘/decrypt’, ‘EncryptionController@decrypt’);

– Now open the browser as- “localhost8000/encrypt” and output is as-:

So this is the encrypted version of the string and you can save this string in your database so after run the code is as-:

And if you need to decrypt the encrypted string to its original format, you can use decrypt string method as above so the output is-:

So let’s move to some example as-:
Like registration of user with encrypted password-:

So let’s know about that how you can submit the value in the database with the encrypted password and after just logged in how you can hide the login and registration and get the username.

So the steps you will follow in the following content is-:

  1. Import the user model
  2. Encryption of Password
  3. Saving the data to the database
  4. Save the name in the session
  5. Hiding of login value or registration
  1. Importing of user model only because the database table name is users here. For which you need name, password, email and contact us information id and update and created date are automatically updated so after that you will encrypt your passwords. Save the data in the database then you will save the name in the session and hide the login and registration menu.
  2. Let’s come to the controller now- “RestoController.php” file in controller folder.

    – With which you also have to import that two things- one is user model in the app.

    Use App\User;

    After that you have to import the crypto class- why you have to choose crypto class because it will help you to encrypt and decrypt the password-:

    Use Crypt;

  3. Let’s make an object of the user model as-:

       Function register (Request $req)
{
     $user = new User;
     $user -> name = $req->input (‘name’);
     $user -> email = $req ->input (‘email’);
     $user -> password = crypt:: encrypt ($req ->input(‘name’));
     $user -> contact = $req -> input (‘contact’);
     $user -> save ();
     }
}

Check the condition if it’s working. So after run to browser the output is -:

Now enter the data and register and the output will be blank because you don’t return anything specific.


Function register (Request $req)
{
     $user = new User;
     $user -> name = $req->input (‘name’);
     $user -> email = $req ->input (‘email’);
     $user -> password = crypt:: encrypt ($req ->input(‘name’));
     $user -> contact = $req -> input (‘contact’);
     $user -> save ();
    $req -> session() -> get (‘user’, $req -> input (‘name’));
   Return redirect (‘/’);
     }
}

Now let’s go back to browser and put some new entries-:

Output is good but there is still shown login and register option in header after registration too so what you will do is, go to “layout.blade.php” file and put a condition as-:


@if (Session::get (‘user’))
< li class=”nav-item”>
< a class = “nav-link” href = “#”>Welcome {{Session::get (‘user’)}} < /a>
< /li>
@else
< li class = “nav-item”>
  < a class = “nav-link” href = “#”> Login < /a>
< /li>

Now again go to browser and refresh the page, so the output is as-:

Now to store the value apply “put” in code as-:


Function register (Request $req)
{
     $user = new User;
     $user -> name = $req->input (‘name’);
     $user -> email = $req ->input (‘email’);
     $user -> password = crypt:: encrypt ($req ->input(‘name’));
     $user -> contact = $req -> input (‘contact’);
     $user -> save ();
    $req -> session() -> put (‘user’, $req -> input (‘name’));
   Return redirect (‘/’);
     }
}

And refresh the page.

Now go to register and register with new entries and you will get the output as-:

So the result is successful and finally checks the database with stored value.


Copyright 1999- Ducat Creative, All rights reserved.