# Working with openssl and files

**URL:** <https://www.ruby-forum.com/t/working-with-openssl-and-files/174434>\
**Category:** Ruby\
**Created:** [August 26, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/working-with-openssl-and-files/174434 "2009-08-26T18:29:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juston\_Davies](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@Juston\_Davies](https://www.ruby-forum.com/u/Juston_Davies)\
**Post date:** [August 26, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/working-with-openssl-and-files/174434/1 "2009-08-26T18:29:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Juston\_Davies](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@Juston\_Davies](https://www.ruby-forum.com/u/Juston_Davies)\
**Post date:** [August 26, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/working-with-openssl-and-files/174434/2 "2009-08-26T18:43:41Z")

</div>

On Wed, Aug 26, 2009 at 01:33:02AM +0900, Juston Davies wrote:

> end  
> @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’)

> eval decrypted\_output  
> “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.

---

<div class="post-metadata">

**Author:** ![Juston\_Davies](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@Juston\_Davies](https://www.ruby-forum.com/u/Juston_Davies)\
**Post date:** [August 26, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/working-with-openssl-and-files/174434/3 "2009-08-26T18:44:16Z")

</div>

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

On Aug 25, 11:16 am, Aaron P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
