Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I make "OpenSSL with Ruby" and "Command line OpenSSL" interoperable?

While trying to setup an interoperable encryption system, I met a weird situation during a light "proof-of-concept".

I wrote the following code in Ruby to:

  • create an encrypted file from a dummy text file on my file system
  • decrypt the encrypted file
  • compare with the original file and check if they are the same

Here is the code:

require 'openssl'
require 'base64'

# Read the dummy file
data = File.read("test.txt")

# Create an encrypter
cipher = OpenSSL::Cipher::AES.new(256, :CBC)
cipher.encrypt
key = "somethingreallyreallycomplicated"
cipher.key = key

# Encrypt and save to a file
encrypted = cipher.update(data) + cipher.final
open "encrypted.txt", "w" do |io| io.write Base64.encode64(encrypted) end

# Create a decrypter
decipher = OpenSSL::Cipher::AES.new(256, :CBC)
decipher.decrypt
decipher.key = key

# Decrypt and save to a file
encrypted_data = Base64.decode64(File.read("encrypted.txt"))
plain = decipher.update(encrypted_data) + decipher.final
open "decrypted.txt", "w" do |io| io.write plain end

# Compare original message and decrypted message
puts data == plain #=> true

Everything works fine, this script outputs "true"

Then I tried to use the openssl command-line to decrypt my file with the following command:

openssl aes-256-cbc -d -a -in encrypted.txt -k somethingreallyreallycomplicated

But I got: bad magic number

How come?

like image 831
Dirty Henry Avatar asked Jan 30 '13 10:01

Dirty Henry


1 Answers

You need to use the -K (upper case) and -iv options on the command line to specify key and IV explicitly as a string of hex digits. If you use -k (lower case), OpenSSL will derive key and IV from the password using a key derivation function. When OpenSSL derives a key, it will also use a "salted" ciphertext format which is incompatible with the plain blockwise CBC you are expecting.

Note that in your Ruby code, you are using the first 256 bits (32 bytes) of an ASCII string directly as a key, which is almost certainly not what you want for a real world application where security is an issue. You should use a (randomly generated) binary key, or derive a key from a password using a key derivation function such as PBKDF2, bcrypt or scrypt.

like image 168
Daniel Roethlisberger Avatar answered Nov 15 '22 04:11

Daniel Roethlisberger