Click here to Skip to main content
16,019,619 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have used credentials in webconfig file and password format is SHA1.
The password is stored in database.

Now when I retrieve the password from the database, how do I decrypt it so that it gives the original value?
Posted
Updated 9-May-11 10:09am
v3
Comments
Dalek Dave 9-May-11 16:09pm    
Edited for Grammar, Spelling and Readability.

You can't decrypt SHA1 because it is a hashed algorithm rather than an encryption algorithm. When you want to compare passwords, you hash the password the user has just entered and compare that hash to the one you stored in the database.

You can find details of what hashing is as opposed to encryption here[^].
 
Share this answer
 
Comments
Dalek Dave 9-May-11 16:10pm    
Good Answer.
Sergey Alexandrovich Kryukov 9-May-11 22:54pm    
My 5.
Absolutely correct, my 5. It's wonderful how all those "developers" get to programming having no idea on what there are trying to do.

So, I added a good reference in my answer, please see.
--SA
I terms of "decrypting" the original value, you can't. With hash algorithms the encryption is one way and the original "lost", but each time you pass a value through your algorithm you will get same "encrypted" hash value.

In terms of using this for login, you hash the password to be checked with the algorithm you already have and make sure the hashes match.

Note that because the hashing algorithm always results in the same hash this introduces a potential security risk, let's say hypothetically you hash the password "password"

password ---SHA1---> DEADBEEF

this would be the same for user1, user 2, user 99 as long as they entered password. You can query such tables to find the common hashes: these are all likely to be weak. The most common password will be "password" if you haven't enforced a policy.

To increase the security you can salt the hash:

  1. Create a set of random bytes to add to the password to be hashed this is the salt
    • Append the salt to the password

    • Create the hash which is now a salted hash

    • Store the salt and the salted-hash in the database (in different columns!!)


As the hash is irreversible J Random Cracker cannot see what an unsalted hash looks like and cannot find the potentially weak passwords. When logging in, you must salt the password to be checked with the value from the database, hash and check it matches the stored salted hash.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 22:54pm    
I like the explanation! My 5.
It's wonderful how all those "developers" get to programming having no idea on what there are trying to do.

So, I added a good reference in my answer, please see.
--SA
Keith Barrow 10-May-11 5:25am    
I took the view that this is a student project, if the OP is an actual dev that is more of a worry, given he is implementing security. That said there is a lack of basic research skills & nouse generally in some parts of the world.
Sergey Alexandrovich Kryukov 10-May-11 13:32pm    
And we know some of those parts...
Thank you, Keith.
--SA
In addition to correct answers by Keith and Pete:

This reading is a must and will be very useful:
http://en.wikipedia.org/wiki/Hash_function_(cryptography)[^].

—SA
 
Share this answer
 
Comments
Pete O'Hanlon 10-May-11 1:04am    
Excellent link. 5 from me.
Sergey Alexandrovich Kryukov 10-May-11 1:21am    
Thank you, Pete.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900