# Symetrical encryption algo's?

**URL:** <https://www.ruby-forum.com/t/symetrical-encryption-algos/74050>\
**Category:** Ruby\
**Created:** [October 29, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/symetrical-encryption-algos/74050 "2006-10-29T14:56:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/symetrical-encryption-algos/74050/1 "2006-10-29T14:56:50Z")

</div>

hi -

> I have a feeling I’m missing something, but if all you want to do is  
> generate obfuscated URLs, why not just the the SHA1-hash of a string  
> consisting of the “real” url and the user’s name? You can store the  
> generated urls in the server-side user-session so you don’t have to do any  
> decrypting.

is there a way to de-sha1 these hashed results to get the original data?  
and to do the shash with a specific key?  
all i could find was:

```
       res = Digest::SHA1.hexdigest(str)

```

whats the reverse of this,  
and where is the key from?

we need to pass URLs back and forth from a ruby site to a PHP site. so  
i was looking for a reasonably easy algorithm that would work for  
both. I’ve had problems with blowfish on PHP giving different results  
on different OSs. So i dont really want to risk something more obscure  
given such different environments.

this is also for a mobile phone service where we reliably can get a  
uniqueID per user.

we need to prevent people forwarding URLs which is why we need to key  
off the uniqueID

but I need to be able to go both ways, given the key. eg:

[ruby.site.com/uri=1b2347ghjhsdgf](http://ruby.site.com/uri=1b2347ghjhsdgf)

I can then decode this + with the users ID/key + send them on their way,  
and also pack up URLs to send ppl back to the php page.

other suggestions are to put all the URLs in a DB + then check against  
the userID/urlID key etc but i want to avoid DB access.

tx!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/symetrical-encryption-algos/74050/2 "2006-10-29T15:22:49Z")

</div>

On 10/29/06, dc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> res = Digest::SHA1.hexdigest(str)
> 
> ```
> 
> whats the reverse of this,  
> and where is the key from?

SHA1 is, to my knowledge, not reversable.

J`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/symetrical-encryption-algos/74050/3 "2006-10-29T16:01:06Z")

</div>

On Sun, 29 Oct 2006, dc wrote:

> is there a way to de-sha1 these hashed results to get the original data?  
> and to do the shash with a specific key?  
> all i could find was:
> 
> ```
> res = Digest::SHA1.hexdigest(str)
> 
> ```
> 
> whats the reverse of this,  
> and where is the key from?

No.

A hashing algorithm is a one way algorithm. You can’t get your original  
data back from it.

> we need to pass URLs back and forth from a ruby site to a PHP site. so  
> i was looking for a reasonably easy algorithm that would work for  
> both. I’ve had problems with blowfish on PHP giving different results  
> on different OSs. So i dont really want to risk something more obscure  
> given such different environments.

As I’ve mentioned, I’m doing this using a pure ruby crypto library found  
on Rubyforge: [http://rubyforge.org/projects/crypt/](http://rubyforge.org/projects/crypt/)

> but I need to be able to go both ways, given the key. eg:
> 
> [ruby.site.com/uri=1b2347ghjhsdgf](http://ruby.site.com/uri=1b2347ghjhsdgf)
> 
> I can then decode this + with the users ID/key + send them on their way,  
> and also pack up URLs to send ppl back to the php page.

If Rijndael(AES), Gost, or IDEA can work reliably via PHP, then you do  
something like this:

irb(main):001:0\> require ‘rubygems’  
=\> true

irb(main):002:0\> require ‘crypt/rijndael’  
=\> true

irb(main):003:0\> require ‘base64’  
=\> true

irb(main):004:0\> require ‘cgi’  
=\> true

irb(main):005:0\> uri = ‘uri=1b2347ghjhsdgf’  
=\> "uri=1b2347ghjhsdgf  
"  
irb(main):006:0\> crypto = Crypt::Rijndael.new(‘I am a key’)  
=\> #\<Crypt::Rijndael:0xb726dd9c @shiftIndex=0, @keyWords=8,  
@blockWords=4,  
…[snip]…

irb(main):007:0\> encrypted\_uri =  
CGI.escape(Base64::encode64(crypto.encrypt\_string(uri)))  
=\>  
“Ai32bnCHRKW1xVEfjtNt63dSQRzRq8SPCSiS6gs9b9fgteBEBSFB%2BLRYdUD2%0AijDQ%0A”

irb(main):008:0\> url = “[ruby.site.com/#{encrypted\_uri}](http://ruby.site.com/#%7Bencrypted_uri%7D)”  
=\>  
"[ruby.site.com/Ai32bnCHRKW1xVEfjtNt63dSQRzRq8SPCSiS6gs9b9fgteBEBSFB%2BLRYdUD2 ijDQ](http://ruby.site.com/Ai32bnCHRKW1xVEfjtNt63dSQRzRq8SPCSiS6gs9b9fgteBEBSFB%2BLRYdUD2%0AijDQ%0A)"

Just reverse the steps to get the original data back:

irb(main):009:0\> original =  
crypto.decrypt\_string(Base64::decode64(CGI.unescape(encrypted\_uri)))  
=\> “uri=1b2347ghjhsdgf”

Kirk H.
