Encrypted using openssl executable, decrypting with ruby's OpenSSL module?

I have some files on my system that have been encrypted using the
“openssl”
executable (via “openssl rc4 -e”), and I would like to decrypt them
using
the “OpenSSL” ruby module.

I know that the salt and iv values are stored in the files that are
encrypted
by the openssl executable, but I haven’t been able to properly extract
that
information from these files so that I can use this salt and iv with the
ruby
OpenSSL decryption functions.

Can anyone point me to a ruby example for extracting this salt and iv
info from an openssl-encrypted file, so I can then decrypt it via ruby’s
OpenSSL module?

Thanks in advance.

On Jul 28, 3:05 am, Lloyd Z. [email protected] wrote:

[ … ]

Can anyone point me to a ruby example for extracting this salt and iv
info from an openssl-encrypted file, so I can then decrypt it via ruby’s
OpenSSL module?

Well, I figured it out. First of all, I need the key size and iv size
for the encryption scheme. According to the chart in Table 15.6 at
this
site,
http://codeidol.com/security/internet-and-intranet-security/Transport-Layer-Security-Protocols/15.2-SSL-PROTOCOL/,
these values are 16 and 0,
respectively, for the RC4 encryption scheme. Armed with these values,
I
came up with the following ruby code (more error checking is needed):

#!/usr/bin/
ruby

require ‘openssl’
require ‘digest/md5’

def decrypt_from_openssl_encrypted(file, password, scheme, keysize,
ivsize)
encrypted_data = nil
begin
File.open(file, ‘r’) {
|f|
encrypted_data = f.read
}
rescue
return nil
end
if encrypted_data.nil? or
encrypted_data.length < 16 or
encrypted_data[0, 8] != ‘Salted__’
return nil
end
salt = encrypted_data[8, 8]
encrypted_data = encrypted_data[16…-1]
totsize = keysize + ivsize
keyivdata = ‘’
temp = ‘’
while keyivdata.length < totsize do
temp = Digest::MD5.digest(temp + password + salt);
keyivdata << temp
end
key = keyivdata[0, keysize]
iv = keyivdata[keysize, ivsize]
c = OpenSSL::Cipher::Cipher.new(scheme)
c.decrypt
c.key = key
c.iv = iv
result = c.update(encrypted_data)
result << c.final
return result
end

file = ‘encrypted.file’
password = ‘???’
scheme = ‘rc4’
keysize = 16
ivsize = 0

decrypted = decrypt_from_openssl_encrypted(file,
password,
scheme,
keysize,
ivsize)

if decrypted.nil?
puts ‘unable to decrypt’
else
puts decrypted
end