Forum: Ruby on Rails Simplest way to encrypt / decrypt a string?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
328f0bb678423fcea01ebe3b0edc74e6?d=identicon&s=25 Ben Johnson (benjohnson)
on 2007-06-15 18:23
I have done multiple google searches and searched the mailing list. All
of the solutions I found seemed kind of heavy / complicated. I just want
a very simple way to encrypt and decrypt a string. Something like:

encrypt("fdsfdsfdsf", "some key");
decrypt("fdsfdsfsdf", "some key");

Is this possible?

Thanks for your help.
B3086d7340aeba221e30ce8679eab3f3?d=identicon&s=25 Emmanuel Oga (emmanueloga)
on 2007-06-15 19:19
Ben Johnson wrote:
> I have done multiple google searches and searched the mailing list. All
> of the solutions I found seemed kind of heavy / complicated. I just want
> a very simple way to encrypt and decrypt a string. Something like:
>
> encrypt("fdsfdsfdsf", "some key");
> decrypt("fdsfdsfsdf", "some key");
>
> Is this possible?
>
> Thanks for your help.

# There's a simple solution if all you want to do is store passwords or
something like that. The thing with this is that you can't decrypt the
string, because the result is a hash of what you want to encode.... But,
as it always generates the same hash for the same string, you can
re-encode the string and check if they are the same.
require 'digest/sha1'

class Encode
  def initialize(key)
    @salt= key
  end

  def encrypt(text)
     Digest::SHA1.hexdigest("--#{@salt}--#{text}--")
  end
end

e= Encode.new("This is a very hard key.")

pas1= e.encrypt("This is my secret password")
pas2= e.encrypt("This is my secret password")
pas3= e.encrypt("This is NOT my secret password")

puts(pas1 == pas2)
puts(pas2 == pas3)
B3086d7340aeba221e30ce8679eab3f3?d=identicon&s=25 Emmanuel Oga (emmanueloga)
on 2007-06-15 19:23
Ben Johnson wrote:
> I have done multiple google searches and searched the mailing list. All
> of the solutions I found seemed kind of heavy / complicated. I just want
> a very simple way to encrypt and decrypt a string. Something like:
>
> encrypt("fdsfdsfdsf", "some key");
> decrypt("fdsfdsfsdf", "some key");
>
> Is this possible?
>
> Thanks for your help.

If you really need to decode your strings, check:

http://crypt.rubyforge.org/installation.html
0b7500f90b400f9e3c4b202f6e57b654?d=identicon&s=25 kellys (Guest)
on 2009-01-27 01:18
(Received via mailing list)
Hi Ben:

You can refer to the following URL for help:

http://www.andreavb.com/tip000013.html

Even though the sample code in this URL is in VB, you can modify it
using Java or any other programming language you use.

On Jun 15 2007, 8:23 am, Ben Johnson <rails-mailing-l...@andreas-
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-01-27 01:38
Ben Johnson wrote:
> I have done multiple google searches and searched the mailing list. All
> of the solutions I found seemed kind of heavy / complicated. I just want
> a very simple way to encrypt and decrypt a string. Something like:
>
> encrypt("fdsfdsfdsf", "some key");
> decrypt("fdsfdsfsdf", "some key");
A quick Google for "Ruby AES encrypt" reveled several examples.

This one looks fine to me. (completely untested):
http://brentrubyrails.blogspot.com/2007/12/aes-enc...
317976b83cf7b2cf3a23c233ef65661b?d=identicon&s=25 sumit asok (Guest)
on 2009-04-02 06:54
(Received via mailing list)
On Jun 15 2007, 10:19 pm, Emmanuel Oga <rails-mailing-l...@andreas-
s.net> wrote:
> > Thanks for your help.
>     @salt= key
> pas2= e.encrypt("This is my secret password")
> pas3= e.encrypt("This is NOT my secret password")
>
> puts(pas1 == pas2)
> puts(pas2 == pas3)
>
> --
> Posted viahttp://www.ruby-forum.com/.




Thank you sir,
       It helped me ( or any newbe like me ) about using encryption
algorithm with rails .Its so easy to use.
sumit asok.
This topic is locked and can not be replied to.