Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > file encryption/decryption needed

Reply
Thread Tools

file encryption/decryption needed

 
 
Venkat Bagam
Guest
Posts: n/a
 
      09-18-2007
Hi Folks,

Yesterday, in my ruby application, I was needed to
encrypt/decrypt a file. I searched for a 2-way encryption technique and
ended up with ezcrypto gem. I thought to give it a test. I was able to
encrypt the content and store it in a file but getting errors while
decrypting the file content and writing to console. I couldn't figure
out the problem. here is my code

require 'rubygems'
require 'ezcrypto'

@key = EzCrypto::Key.with_password "private documents","salted hash"
file1 = File.new("crypto.txt", "w")
@encrypted = @key.encrypt "These are private documents"
file1.puts @encrypted
file1.close
puts "Here is the content"
file2 = File.read("crypto.txt")
puts @key.decrypt file2

Can anyone figure out the problem? or suggest me a different crypting
technique other than ezcrypto or suggest me a ruby app implementing such
a requirement..
or a code snippet

any help appreciated

thanks&regards,
Venkat

Attachments:
http://www.ruby-forum.com/attachment/304/test.rb

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Morton Goldberg
Guest
Posts: n/a
 
      09-18-2007
On Sep 18, 2007, at 2:15 AM, Venkat Bagam wrote:

> Hi Folks,
>
> Yesterday, in my ruby application, I was needed to
> encrypt/decrypt a file. I searched for a 2-way encryption technique
> and
> ended up with ezcrypto gem. I thought to give it a test. I was able to
> encrypt the content and store it in a file but getting errors while
> decrypting the file content and writing to console. I couldn't figure
> out the problem. here is my code
>
> require 'rubygems'
> require 'ezcrypto'
>
> @key = EzCrypto::Key.with_password "private documents","salted hash"
> file1 = File.new("crypto.txt", "w")
> @encrypted = @key.encrypt "These are private documents"
> file1.puts @encrypted
> file1.close
> puts "Here is the content"
> file2 = File.read("crypto.txt")
> puts @key.decrypt file2


I think puts is adding a "\n" at the end of the file you write out as
crypto.txt. So from the point of view of @key.decrypt the file has
been corrupted. Try the following:

<untested>
@key = EzCrypto::Key.with_password "private documents","salted hash"
File.open("crypto.txt", "w") do |file1|
@encrypted = @key.encrypt "These are private documents"
file1.write @encrypted
end
puts "Here is the content"
file2 = File.read("crypto.txt")
puts @key.decrypt file2
</untested>

I couldn't test the above code because I don't have the ezcrypt gem.

Regards, Morton

 
Reply With Quote
 
 
 
 
Axel Etzold
Guest
Posts: n/a
 
      09-18-2007

-------- Original-Nachricht --------
> Datum: Tue, 18 Sep 2007 15:15:28 +0900
> Von: Venkat Bagam <(E-Mail Removed)>
> An: http://www.velocityreviews.com/forums/(E-Mail Removed)
> Betreff: file encryption/decryption needed


> Hi Folks,
>
> Yesterday, in my ruby application, I was needed to
> encrypt/decrypt a file. I searched for a 2-way encryption technique and
> ended up with ezcrypto gem. I thought to give it a test. I was able to
> encrypt the content and store it in a file but getting errors while
> decrypting the file content and writing to console. I couldn't figure
> out the problem. here is my code
>
> require 'rubygems'
> require 'ezcrypto'
>
> @key = EzCrypto::Key.with_password "private documents","salted hash"
> file1 = File.new("crypto.txt", "w")
> @encrypted = @key.encrypt "These are private documents"
> file1.puts @encrypted
> file1.close
> puts "Here is the content"
> file2 = File.read("crypto.txt")
> puts @key.decrypt file2
>
> Can anyone figure out the problem? or suggest me a different crypting
> technique other than ezcrypto or suggest me a ruby app implementing such
> a requirement..
> or a code snippet
>
> any help appreciated
>
> thanks&regards,
> Venkat
>
> Attachments:
> http://www.ruby-forum.com/attachment/304/test.rb
>
> --
> Posted via http://www.ruby-forum.com/.


Dear Venkat,

I tried your code and I got a problem about "wrong final block length (OpenSSL::Cipher)", which comes from the fact that you are writing
an additional newline at the end of the encrypted text when
you store it in a file.
So read in the encrypted text from that file, chop off the last newline, and decrypt:

require 'rubygems'
require 'ezcrypto'
@key = EzCrypto::Key.with_password "mighty mouse","salted hash"
file1 = File.new("crypto.txt", "w")
@encrypted = @key.encrypt "This is my super-secret message"
file1.puts @encrypted
file1.close
file2=File.read("crypto.txt").chop
puts @key.decrypt(file2) # => "This is my super-secret message"


Best regards,

Axel


--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

 
Reply With Quote
 
Venkat Bagam
Guest
Posts: n/a
 
      09-19-2007
Dear Axel,

Thanks. It works great. I have been trying for two days but
unable to
figure out that. I need to be little bit careful with files.

once again, thanks to you and the forum....

regards,
venkat
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed on this 857W config. Repost to be clearer what the problemsare and the help needed sparticle Cisco 3 08-30-2007 07:47 PM
Ideas needed & help needed! Ryan Macy Ruby 2 07-19-2006 08:04 PM
Needed Instructor's Manual for Data Structures and Algorithms in C++ needed!!! Thomas Nick C++ 0 06-13-2005 01:58 AM
Advise needed re Olympus Camedia C-3030 Zoom Camera (driver needed) Arawak Computer Support 2 11-18-2004 03:03 PM
Microsoft small business server 2003 - help needed to understand what is needed to use it Dima Computer Support 5 10-20-2004 08:27 PM



Advertisments