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

**URL:** <https://www.ruby-forum.com/t/encrypted-using-openssl-executable-decrypting-with-rubys-openssl-module/172799>\
**Category:** Ruby\
**Created:** [July 28, 2009, 10:21am UTC](https://www.ruby-forum.com/t/encrypted-using-openssl-executable-decrypting-with-rubys-openssl-module/172799 "2009-07-28T10:21:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lloyd\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@Lloyd\_Z](https://www.ruby-forum.com/u/Lloyd_Z)\
**Post date:** [July 28, 2009, 10:21am UTC](https://www.ruby-forum.com/t/encrypted-using-openssl-executable-decrypting-with-rubys-openssl-module/172799/1 "2009-07-28T10:21:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lloyd\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@Lloyd\_Z](https://www.ruby-forum.com/u/Lloyd_Z)\
**Post date:** [July 29, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/encrypted-using-openssl-executable-decrypting-with-rubys-openssl-module/172799/2 "2009-07-29T17:50:48Z")

</div>

On Jul 28, 3:05 am, Lloyd Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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
