Home > Net >  How to encrypt and decrypt messages in laravel?
How to encrypt and decrypt messages in laravel?

Time:12-15

I want to work on a project where users can send messages to others users that are subscribe to their channels.

I want to ask if it's very necessary to encrypt the messages before storing them in the database? Although some messages may contain some sensitive information.

If yes, how do I go about encrypting the message in laravel and also decrypting it when showing it to the end user?

CodePudding user response:

try this

public function encryption($data,  $type){
        $ciphering = "AES-256-CBC";
        $options = 0;
        $encryption_key = "1111111111111111"; // Should Come From Your .env
        $encryption_iv = "sdafdsaffregdghrthbgjhfgjghjghjghjgjghjfgjfgjfjfjcfth"; // Should Come From Your .env


        if($type=='encrypt')
            return openssl_encrypt($data, $ciphering, $encryption_key, $options, $encryption_iv);

        else if($type=='decrypt')
            return openssl_decrypt($data, $ciphering, $encryption_key, $options, $encryption_iv);

    }

CodePudding user response:

You can check Laravel's encryption methods.

Crypt::encryptString('<string>')
Crypt::decryptString($encryptedValue)

CodePudding user response:

use Illuminate\Support\Facades\Crypt;
$encrypted = Crypt::encryptString('Hello world.');
$decrypted = Crypt::decryptString($encrypted);
  • Related