How to encrypt files with Ruby?

encryption, openssl, ruby, security

Solution

Ruby's OpenSSL is a thin wrapper around OpenSSL itself and provides almost all the functionality that OpenSSL itself does, so yes, there's a one-to-one mapping for all your examples:

openssl rand -base64 2048 > secret_key

That's actually exaggerated, you are using AES-256, so you only need a 256 bit key, you are not using RSA here. Ruby OpenSSL takes this decision off your shoulders, it will automatically determine the correct key size given the algorithm you want to use.

You are also making the mistake of using a deterministic IV during your encryption. Why? Because you don't specify an IV at all, OpenSSL itself will default to an IV of all zero bytes. That is not a good thing, so I'll show you the correct way to do it, for more information have a look at the Cipher documentation.

require 'openssl'

# encryption
cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.encrypt
key = cipher.random_key
iv = cipher.random_iv

buf = ""
File.open("file.enc", "wb") do |outf|
  File.open("file", "rb") do |inf|
    while inf.read(4096, buf)
      outf << cipher.update(buf)
    end
    outf << cipher.final
  end
end

# decryption
cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.decrypt
cipher.key = key
cipher.iv = iv # key and iv are the ones from above

buf = ""
File.open("file.dec", "wb") do |outf|
  File.open("file.enc", "rb") do |inf|
    while inf.read(4096, buf)
      outf << cipher.update(buf)
    end
    outf << cipher.final
  end
end

As you can see, encryption and decryption are fairly similar, so you can probably combine the streaming reading/writing into one shared method and just pass it a properly configured `Cipher` plus the corresponding file names, I just stated them explicitly for the sake of clarity.

If you'd like to Base64-encode the key (and probably the IV, too), you can use the Base64 module:

base64_key = Base64.encode64(key)

Problem

I need to write a simple tool which encrypts/decrypts files. I guess the best way is to use OpenSSL: Generate a key: ``` openssl rand -base64 2048 > secret_key ``` Encrypt a file: ``` openssl aes-256-cbc -a -e -in file -out file.enc -k secret_key ``` Decrypt a file: ``` openssl aes-256-cbc -d -in file.enc -out file -k secret_key ``` Is there an easy way to implement this in Ruby? Is there a better way to do that? Using PGP maybe?

Original source

Related problems