# Can we decrypt the cipher encrypted using Digest::SHA1.hexdigest

**URL:** <https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061>\
**Category:** Rails\
**Created:** [April 21, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061 "2009-04-21T14:35:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/1 "2009-04-21T14:35:29Z")

</div>

what i have done is as follows  
password = Digest::SHA1.hexdigest("#{salt}:#{password}")

pass1 = Digest::SHA1.hexdigest("#{salt}:asdfgh")  
pass2 = Digest::SHA1.hexdigest("#{salt}:asdfgh")  
pass3 = Digest::SHA1.hexdigest("#{salt}:qwerty")

puts pass1==pass2  
puts pass1==pass3

This works fine  
but i need to get the decrypted password

how can i get it  
any help is highly appreciated

Thanks in advance

Krishnaprasad V.  
India

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 3:10pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/2 "2009-04-21T15:10:07Z")

</div>

On 21 Apr 2009, at 13:34, — Z@m — wrote:

> This works fine  
> but i need to get the decrypted password
> 
> how can i get it

You can’t[1]. Digests by design are one way only.

Fred

[1] Obviously you can try a brute force attack and cryptography  
researchers have found various attacks, but that’s probably not what  
you meant

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/3 "2009-04-21T15:23:36Z")

</div>

And using Digest::MD5 ?? can i encrypt

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/4 "2009-04-21T16:02:20Z")

</div>

On Apr 21, 2:22 pm, “Z@M” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And using Digest::MD5 ?? can i encrypt

Digests are all one way. pretty much the definition of a digest is  
that it is not reversible.

Fred

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 3:42pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/5 "2009-04-21T15:42:08Z")

</div>

Why do you need the plaintext?

Generally speaking, even an admin should not be able to get back your  
plaintext. That role should be able to reset your account so that you  
can once again set your password to something only you know.

To try and engineer something in that appears like security to a user  
but isn’t is deceptive at best.

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/6 "2009-04-21T16:55:12Z")

</div>

Ok i think its time to reveal my need

i was doin a login system in rails  
for the time being i didnt provide the password encryption and i  
thought it was simple as  
many reliable encryption algorithms are available every where

every thing worked perfectly untill i started encrypting the password  
and save it in db

i am able to check weather the password enter is correct or not

almost all the browser provide the facility to remember username and  
passwords  
so i thought i would do it myself and stored the password and  
username in the cookie if  
" remember me " option is ticked

it now fetches the password from the database and stores it in the  
coockie  
so when the user comes next time he is shown the password which is  
encrypted and the login fails

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/7 "2009-04-21T20:31:44Z")

</div>

On Tue, Apr 21, 2009 at 7:54 AM, Z@M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> so when the user comes next time he is shown the password which is  
> encrypted and the login fails

Apologies if this seems a bit harsh, but you’re not the first person  
to ask this list a question like this (and prolly not the last).

Security- especially cryptography is HARD. You clearly have no idea  
what you’re doing when it comes to security/crypto. Even if you  
properly understood crypto fundamentals, you’d still probably use the  
algorithms incorrectly which would open up huge holes- it happens all  
the time. Even the so-called “experts” can and do get it wrong  
occasionally.

The solution is simple: stop trying to roll your own solution. Use  
SSL with something like restful\_authentication and be done with it.

–  
Aaron T.

> **[Syn Fin dot Net](https://sockstream.synfin.net)**
>
> Rambliings about networking, hardware, software, and plastic-ware.

[http://tcpreplay.synfin.net/](http://tcpreplay.synfin.net/) - Pcap editing and replay tools for Unix &  
Windows  
Those who would give up essential Liberty, to purchase a little  
temporary  
Safety, deserve neither Liberty nor Safety.  
– Benjamin Franklin

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/8 "2009-04-21T17:36:47Z")

</div>

On Apr 21, 3:54 pm, “Z@M” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> almost all the browser provide the facility to remember username and  
> passwords  
> so i thought i would do it myself and stored the password and  
> username in the cookie if  
> " remember me " option is ticked

Typically this is done by storing a token in a cookie - if your login  
page receives a cookie with an appropriate token then you skip  
straight to the user being logged in. This avoids needing to store the  
password in an insecure place.

Fred

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 21, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/9 "2009-04-21T22:17:01Z")

</div>

Wow, that is about the worst possible solution to the “keeping the  
user logged in” problem!

I assume you want them to be “just logged in” when they come back to  
the site? That is what the user wants. You can use a cookie to  
associate the user with being logged in, but why in the name of all  
good things would you want to store the username/password in the  
cookie? You know that is plain text? Your site, if it decides to let  
a user in for some reason other than user name and password really  
does not need to know anything about the password or user name. The  
session cookie is enough.

As others have said: you cannot “unhash” a cryptographic hash. Nor  
should you want to. The idea is to verify that the user knows the  
SECRET (password) without anybody else having access to the SECRET.  
Storing it in the cookie is bad.

You are trying to solve a very simple problem in a very complicated  
way!

Brendon.

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 22, 2009, 3:26am UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/10 "2009-04-22T03:26:45Z")

</div>

He’s still doing it better than the dev shop I once cleaned up after -  
they appeared to think that base64 encoding was a form of  
encryption…

Although, it did save me a lot of trouble when migrating users over to  
the new system.

–Matt J.

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 23, 2009, 1:50am UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/11 "2009-04-23T01:50:27Z")

</div>

The risk of to the users of access to plain text passwords is far  
greater, since many (most) users use the same password for similar  
types of sites. (I say that hoping people use a different password  
for online banking while they use another password for gmail, yahoo,  
, NYT, etc.)

Next up, support having to ask customers for the password to access  
the account!

---

<div class="post-metadata">

**Author:** ![SSZSmSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@SSZSmSS](https://www.ruby-forum.com/u/SSZSmSS)\
**Post date:** [April 22, 2009, 4:04am UTC](https://www.ruby-forum.com/t/can-we-decrypt-the-cipher-encrypted-using-digest-sha1-hexdigest/166061/12 "2009-04-22T04:04:21Z")

</div>

We’ve got the same thing where I am at the moment. The old .net app has  
the passwords stored in plain text in the Db. I guess if you get as far  
as being able to log onto the db, then you have already gotten full  
access to the system, but still seems wrong.

Simon
