Search code examples
laravelencryptionmailercrypt

Laravel encrypt password before storing on DB and decrypt it to use on email settings


I followed this tutorial to create dynamic email settings stored on db.

https://kayike.medium.com/enable-unique-and-dynamic-smtp-mail-settings-for-each-user-laravel-48e320d381ec

The only problem is that the password is not encrypted. I would like to encrypt it before storing on db and decrypt it before using on MailServiceProvider.

I tried to use bcrypt but it can't be de-crypted. Any suggestions?

Thanks


Solution

  • see the docs for encryption: https://laravel.com/docs/8.x/encryption

    encrypting password:

    $encrypted = crypt::encryptString($password);
    

    //store this to database

    decrypting password:

    $decrypted_password = crypt::decryptString($encrypted);
    //use this for mailer settings
    

    Note: don't forget to use namespace Illuminate\Support\Facades\Crypt; in the controller

    Additional Note for bcrypt: encryption-decryption is different than hashing, bcrypt is a hashing formula which can't be decrypted (one way process).