13aal 13aal - 6 months ago 13
Ruby Question

Encrypting files with a decryption key

I'm writing a program that needs to encrypt a log file using

openssl
and have a decryption key. For example I want to encrypt this file:


This is a test of encrypting a file

This is a test of encrypting a file


Using
openssl
and I want to be able to decrypt it when I put in some decryption key, for sake of argument
123456789
will be the key.

def decrypt
print 'Enter key: '
key = gets.chomp
if key == decryption_key
# decrypt file
else
# don't decrypt file
end
end


I've read the docs on
openssl
but I still don't fully understand how it works, could someone give me an example of what I'm trying to do, along with an example of the decryption part please?

Answer

Using aes gem you can do something like:

key = AES.key    
b64 = AES.encrypt("A super secret message", key)
AES.decrypt(b64, key) # => "A super secret message"