13aal 13aal - 1 year ago 60
Ruby Question

Encrypting files with a decryption key

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

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

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

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

I've read the docs on
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 Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download