Encrypted using openssl executable, decrypting with ruby's OpenSSLmodule?

L

Lloyd Zusman

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.
 
L

ljz

[ ... ]

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/intern...t-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top