Working with openssl and files

Discussion in 'Ruby' started by Juston Davies, Aug 25, 2009.

  1. Let me start out by saying all I do is in the name of science, I only
    say this because what Im about to ask seems unreasonable. I'm trying
    to:

    1.) Take an existing ruby script
    2.) Encrypt it
    3.) Store the encrypted copy in another file
    4.) Reopen that file
    5.) Decrypt the contents
    6.) eval the contents

    I have some code so far, here is what it looks like:
    -----------------------------------------------------------------------------------------------------------------------------------
    require 'openssl'

    def encrypt(data, key, iv, cipher_type)
    aes = OpenSSL::Cipher::Cipher.new(cipher_type)
    aes.encrypt
    aes.key = key
    aes.iv = iv if iv != nil
    aes.update(data) + aes.final
    end

    def decrypt(encrypted_data, key, iv, cipher_type)
    aes = OpenSSL::Cipher::Cipher.new(cipher_type)
    aes.decrypt
    aes.key = key
    aes.iv = iv if iv != nil
    aes.update(encrypted_data) + aes.final
    end

    @key = "borgeddd"*4
    @salt = nil
    @algorithm = "AES-256-ECB"

    file = File.new("./Ruby_Script.rb")

    encrypted_file = File.open("borged", 'w')
    encrypted_output = encrypt(file.read, @key, @salt, @algorithm)
    encrypted_file.write(encrypted_output)

    encrypted_file.close

    file = File.open("borged")
    decrypted_output = decrypt(file.read, @key, @salt, @algorithm) #works
    if I just use the string encrypted_output
    puts decrypted_output

    eval decrypted_output
    -----------------------------------------------------------------------------------------------------------------------------------

    The code works if I just encrypt and decrypt strings, but when I write
    to a file it appears its losing data. After encrypting the data and
    looking at the length of the string its roughly about 1,000
    characters, however, after I write to a file and close/reopen it the
    new content is only about 300 characters long. That could be a bad
    measurement, but it seems to me like Im losing something in the
    translation. When I actually run this code as it is I get:

    "C:/Workspace/Hob-nobbery/init.rb:16:in `final': wrong final block
    length (OpenSSL::CipherError)"
    " from C:/Workspace/Hob-nobbery/init.rb:16:in `decrypt'"

    Any ideas?
     
    Juston Davies, Aug 25, 2009
    #1
    1. Advertising

  2. On Wed, Aug 26, 2009 at 01:33:02AM +0900, Juston Davies wrote:
    > Let me start out by saying all I do is in the name of science, I only
    > say this because what Im about to ask seems unreasonable. I'm trying
    > to:
    >
    > 1.) Take an existing ruby script
    > 2.) Encrypt it
    > 3.) Store the encrypted copy in another file
    > 4.) Reopen that file
    > 5.) Decrypt the contents
    > 6.) eval the contents
    >
    > I have some code so far, here is what it looks like:
    > -----------------------------------------------------------------------------------------------------------------------------------
    > require 'openssl'
    >
    > def encrypt(data, key, iv, cipher_type)
    > aes = OpenSSL::Cipher::Cipher.new(cipher_type)
    > aes.encrypt
    > aes.key = key
    > aes.iv = iv if iv != nil
    > aes.update(data) + aes.final
    > end
    >
    > def decrypt(encrypted_data, key, iv, cipher_type)
    > aes = OpenSSL::Cipher::Cipher.new(cipher_type)
    > aes.decrypt
    > aes.key = key
    > aes.iv = iv if iv != nil
    > aes.update(encrypted_data) + aes.final
    > end
    >
    > @key = "borgeddd"*4
    > @salt = nil
    > @algorithm = "AES-256-ECB"
    >
    > file = File.new("./Ruby_Script.rb")
    >
    > encrypted_file = File.open("borged", 'w')


    You probably need to open the file in binary write mode:

    encrypted_file = File.open("borged", 'wb')

    > encrypted_output = encrypt(file.read, @key, @salt, @algorithm)
    > encrypted_file.write(encrypted_output)
    >
    > encrypted_file.close
    >
    > file = File.open("borged")
    > decrypted_output = decrypt(file.read, @key, @salt, @algorithm) #works
    > if I just use the string encrypted_output
    > puts decrypted_output
    >
    > eval decrypted_output
    > -----------------------------------------------------------------------------------------------------------------------------------
    >
    > The code works if I just encrypt and decrypt strings, but when I write
    > to a file it appears its losing data. After encrypting the data and
    > looking at the length of the string its roughly about 1,000
    > characters, however, after I write to a file and close/reopen it the
    > new content is only about 300 characters long. That could be a bad
    > measurement, but it seems to me like Im losing something in the
    > translation. When I actually run this code as it is I get:
    >
    > "C:/Workspace/Hob-nobbery/init.rb:16:in `final': wrong final block
    > length (OpenSSL::CipherError)"
    > " from C:/Workspace/Hob-nobbery/init.rb:16:in `decrypt'"
    >
    > Any ideas?


    Try opening the file in binary write mode as I've written above. I
    think that will work out the problems.

    --
    Aaron Patterson
    http://tenderlovemaking.com/
     
    Aaron Patterson, Aug 25, 2009
    #2
    1. Advertising

  3. Well that did it. Wasn't the write, but the read. Thanks a bunch!

    On Aug 25, 11:16=A0am, Aaron Patterson <>
    wrote:
    > On Wed, Aug 26, 2009 at 01:33:02AM +0900, Juston Davies wrote:
    > > Let me start out by saying all I do is in the name of science, I only
    > > say this because what Im about to ask seems unreasonable. I'm trying
    > > to:

    >
    > > 1.) Take an existing ruby script
    > > 2.) Encrypt it
    > > 3.) Store the encrypted copy in another file
    > > 4.) Reopen that file
    > > 5.) Decrypt the contents
    > > 6.) eval the contents

    >
    > > I have some code so far, here is what it looks like:
    > > -----------------------------------------------------------------------=

    ------------------------------------------------------------
    > > require 'openssl'

    >
    > > def encrypt(data, key, iv, cipher_type)
    > > =A0 aes =3D OpenSSL::Cipher::Cipher.new(cipher_type)
    > > =A0 aes.encrypt
    > > =A0 aes.key =3D key
    > > =A0 aes.iv =3D iv if iv !=3D nil
    > > =A0 aes.update(data) + aes.final
    > > end

    >
    > > def decrypt(encrypted_data, key, iv, cipher_type)
    > > =A0 aes =3D OpenSSL::Cipher::Cipher.new(cipher_type)
    > > =A0 aes.decrypt
    > > =A0 aes.key =3D key
    > > =A0 aes.iv =3D iv if iv !=3D nil
    > > =A0 aes.update(encrypted_data) + aes.final
    > > end

    >
    > > @key =3D "borgeddd"*4
    > > @salt =3D nil
    > > @algorithm =3D "AES-256-ECB"

    >
    > > file =3D File.new("./Ruby_Script.rb")

    >
    > > encrypted_file =3D File.open("borged", 'w')

    >
    > You probably need to open the file in binary write mode:
    >
    > =A0 encrypted_file =3D File.open("borged", 'wb')
    >
    >
    >
    > > encrypted_output =3D encrypt(file.read, @key, @salt, @algorithm)
    > > encrypted_file.write(encrypted_output)

    >
    > > encrypted_file.close

    >
    > > file =3D File.open("borged")
    > > decrypted_output =3D decrypt(file.read, @key, @salt, @algorithm) #works
    > > if I just use the string encrypted_output
    > > puts decrypted_output

    >
    > > eval decrypted_output
    > > -----------------------------------------------------------------------=

    ------------------------------------------------------------
    >
    > > The code works if I just encrypt and decrypt strings, but when I write
    > > to a file it appears its losing data. After encrypting the data and
    > > looking at the length of the string its roughly about 1,000
    > > characters, however, after I write to a file and close/reopen it the
    > > new content is only about 300 characters long. That could be a bad
    > > measurement, but it seems to me like Im losing something in the
    > > translation. When I actually run this code as it is I get:

    >
    > > "C:/Workspace/Hob-nobbery/init.rb:16:in `final': wrong final block
    > > length (OpenSSL::CipherError)"
    > > " =A0 =A0 from C:/Workspace/Hob-nobbery/init.rb:16:in `decrypt'"

    >
    > > Any ideas?

    >
    > Try opening the file in binary write mode as I've written above. =A0I
    > think that will work out the problems.
    >
    > --
    > Aaron Pattersonhttp://tenderlovemaking.com/
     
    Juston Davies, Aug 25, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. openssl and DES

    , Jun 28, 2005, in forum: C Programming
    Replies:
    2
    Views:
    482
    CBFalconer
    Jun 29, 2005
  2. Replies:
    2
    Views:
    2,838
    Maxim Yegorushkin
    Jun 29, 2005
  3. Bil Kleb
    Replies:
    6
    Views:
    125
    Bil Kleb
    Sep 18, 2006
  4. Damjan Rems
    Replies:
    4
    Views:
    243
    Roland Schmitt
    Oct 16, 2006
  5. Redd Vinylene
    Replies:
    6
    Views:
    306
    Jakub Pawlowicz
    Nov 18, 2008
Loading...

Share This Page