On Jul 28, 3:05=A0am, Lloyd Zusman <l... / asfast.com> 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 =3D nil
  begin
    File.open(file, 'r') {
      |f|
      encrypted_data =3D f.read
    }
  rescue
    return nil
  end
  if encrypted_data.nil?        or
     encrypted_data.length < 16 or
     encrypted_data[0, 8] !=3D 'Salted__'
    return nil
  end
  salt =3D encrypted_data[8, 8]
  encrypted_data =3D encrypted_data[16..-1]
  totsize =3D keysize + ivsize
  keyivdata =3D ''
  temp =3D ''
  while keyivdata.length < totsize do
    temp =3D Digest::MD5.digest(temp + password + salt);
    keyivdata << temp
  end
  key =3D keyivdata[0, keysize]
  iv  =3D keyivdata[keysize, ivsize]
  c =3D OpenSSL::Cipher::Cipher.new(scheme)
  c.decrypt
  c.key =3D key
  c.iv  =3D iv
  result =3D c.update(encrypted_data)
  result << c.final
  return result
end

file     =3D 'encrypted.file'
password =3D '????????'
scheme   =3D 'rc4'
keysize  =3D 16
ivsize   =3D 0

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

if decrypted.nil?
  puts 'unable to decrypt'
else
  puts decrypted
end

--
 Lloyd Zusman
 ljz / asfast.com
 God bless you.