Forum: Ruby Efficient way to encode a string with a secret string and recover it

0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-12-20 10:08
(Received via mailing list)
Hi, basically I receive a string generated by a user, then "encode" it
using some secret string I own and pass the result to other user, and
when I receive such a resulting string I must be able to decode it
using my secret string (but no other should be able to decode the
resulting string without knowing my secret key. This is:


original_string = "test"

secret_key = "ABCD"

modified_string = HASH_FUNCTION(original_string, secret_key)

original_string = UNHASH_FUNCTION(modified_string, secret_key)


Any suggestion for the above HASH_FUNCTION and UNHASH_FUNCTION? I
would like it to be something really efficient.

Thanks a lot.
C993c6aaffc6cf7b477a5075099e1d36?d=identicon&s=25 Peter Hickman (Guest)
on 2012-12-20 10:38
(Received via mailing list)
The nature of a hash function is that there is no unhash function. It
would
be pretty useless if there was.

What you want is encryption like RSA, Blowfish or rot13.

PS the rot13 thing was a joke.
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-12-20 10:52
(Received via mailing list)
2012/12/20 Iñaki Baz Castillo <ibc@aliax.net>:
>
> modified_string = HASH_FUNCTION(original_string, secret_key)
>
> original_string = UNHASH_FUNCTION(modified_string, secret_key)
>
>
> Any suggestion for the above HASH_FUNCTION and UNHASH_FUNCTION? I
> would like it to be something really efficient.


I've found the Crypt [*] library but it seems not adapted for Ruby
1.9. And I would prefer a C based Ruby extension.


[*] http://crypt.rubyforge.org/
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-12-20 10:56
(Received via mailing list)
2012/12/20 Peter Hickman <peterhickman386@googlemail.com>:
> The nature of a hash function is that there is no unhash function. It would
> be pretty useless if there was.

Yes, sorry, bad word. I just meant "encode/decode" rather than "hash".


> What you want is encryption like RSA, Blowfish or rot13.
>
> PS the rot13 thing was a joke.

Ok, thanks a lot.
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2012-12-20 11:14
(Received via mailing list)
Hi,

2012/12/20 Iñaki Baz Castillo <ibc@aliax.net>:
>
> modified_string = HASH_FUNCTION(original_string, secret_key)
>
> original_string = UNHASH_FUNCTION(modified_string, secret_key)
>
>
> Any suggestion for the above HASH_FUNCTION and UNHASH_FUNCTION? I
> would like it to be something really efficient.
>
> Thanks a lot.
>
You can use openssl module something like this:

def encrypt(input,key)
  require 'openssl'
  des = OpenSSL::Cipher::Cipher.new("des-ede-cbc")
  des.encrypt
  des.key= "%16s" % key
  des.update(input)+des.final
end

def decrypt(input,key)
  require 'openssl'
  des = OpenSSL::Cipher::Cipher.new("des-ede-cbc")
  des.decrypt
  des.key = "%16s" % key
  des.update(input)+des.final
end

But, I am not sure it is really efficient.

Regards,

Park Heesob
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2012-12-20 12:21
Crypt19 works fine with Ruby 1.9.3

http://rubygems.org/gems/crypt19
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 "Iñaki Baz Castillo" <ibc@aliax.net> (Guest)
on 2012-12-20 12:40
(Received via mailing list)
Thanks, I'm trying it and will compare with crypt19 as suggested by
Joel.

Thanks to both.


2012/12/20 Heesob Park <phasis@gmail.com>:
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.