Encryption of a string with a password in ruby

I would like to receive some data from the user and encrypt it with a password. For example, a user gives me:

String: 'Whackabad'
Password 'Holycrap'

I would like to keep their string encrypted, but be able to decrypt the string again when the user enters his password. What is the best way to do this?

+4
source share
2 answers

You can use encrypted_stringsgem:

data = 'Whackabad'
password = 'Holycrap'
encrypted = data.encrypt(:symmetric, :algorithm => 'des-ecb', :password => password) 
# => "N6gLAAL9d9lRjHUbh54Ctw==\n"

encrypted.decrypt(:symmetric, :algorithm => 'des-ecb', :password => password) 
# => "Whackabad"

This solution does not encrypt and does not save the password anywhere - it encrypts this data, and only those who know the password will be able to decrypt the data.

This is a good solution if you do not want anyone (including the site administrator) to decrypt the data without the correct password.

+5
0

Source: https://habr.com/ru/post/1532004/


All Articles