I'm trying to write a Rails application that will let you store encrypted passwords in a Rails database. When you want to store a password, you'll call encrypt on a phrase and it will show you exactly what the resulting encrypted string is and you can store it along with a particular website or account name and you'll be able to reference it on a table. Then when you need the password, you can select that website/password pair and decrypt it to retrieve the password. My question is whether or not this following scheme would be safe or not.
- I'll have this model (called SecretKeeper, for example) that will be tied to a User.
- When a SecretKeeper is initialized, it is initialized with an instance of a FastAES object and a passphrase (e.g.
@aes = FastAES.new(secret_phrase)
) - Then when the user wants to encrypt a password, SecretKeeper would then call
Base64.urlsafe_encode64(@aes.encrypt([password].pack("m"))).slice(0..-2)
to return the encoded string, which is then stored in the database. To decode, the user must pass in the encrypted string along with a "master" password, which is separate from the secret_phrase used to create the FastAES object. This master password is stored in the database associated to the user as an encrypted string (using the same method as described in number 3 above). To validate this master password, when the user attempts to decrypt a password I encrypt the master password and compare it to the hashed value in the database to see if it matches. If it does, I then proceed to call
@aes.decrypt(Base64.urlsafe_decode64("#{encoded}=")).ljust(8, "\x00").unpack("m").first
and return the resulting string- So is this a good idea? Or is there something I should be doing to make it more secure?
- I am also planning to add a password salt, that will be stored in the database in plaintext. Should I plan to encrypt this as well?