File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes How to Decrypt password Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "How to Decrypt password" Watch "How to Decrypt password" New topic
Author

How to Decrypt password

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
Hi,i wrote a java code to save phone numberss in md5 format in mysql.
by using,


i can do encryption,but i can not do decryption

please help me how to decrypt it,please
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
Right. MD5 is a hash (or digest), not a cipher. That means once something has been run through it, the original text can no longer be recovered. That's how you want to store passwords.


Ping & DNS - updated with new look and Ping home screen widget
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
lot of people are using md5 for encryption,so how they are validating the password then?
suppose user entering password then how can i validate it ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

shyam sunder prasad wrote:lot of people are using md5 for encryption,so how they are validating the password then?
suppose user entering password then how can i validate it ?


After challenging the user for the password, run the same algorithm on the password to get the md5 hash. Compare the newly hashed value with the previously saved one. If the two hashes matches, then the passwords are, for most intents and purposes, can be considered as a match.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
THANKS FOR YOUR REPLY,

i want for phone numbers. i want to store phone numbers in mysql in md5 format ,
so, whenever i want to show them in jsp page to the user i should show as original phone numbers.

if it not possible with md5 please tell me another algorithm ......
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
Check out AES.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

shyam sunder prasad wrote:lot of people are using md5 for encryption

No, no one can encrypt anything with MD5. It simply can't do that. What it can do is hash a thing.

By definition, when you encrypt (better term is encipher) you expect to be able to reverse the action and decrypt/decipher the resulting ciphertext. You can not do that with any cryptographic hash (MD5, SHA1, SHA256, etc.).

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
Can Help me out how to write encrypt-er and decrypt er with any cryptographic hash (MD5, SHA1, SHA256, etc.). ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
Did you read Pat's and my posts about how that is impossible?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Ulf Dittmer wrote:Did you read Pat's and my posts about how that is impossible?


I delayed responding to this post to give the OP time understand why in the narrow context of the his initial requirement to 'decrypt' a digest then this is indeed true. BUT under his latest wider context of -

shyam sunder prasad wrote:Can Help me out how to write encrypt-er and decrypt er with any cryptographic hash (MD5, SHA1, SHA256, etc.). ?


then this is most definitely possible.

One approach (there are many others) is to use the digest to create a stream of pseudo random bytes and to use these to create a stream cipher using a kind of CFB mode. One starts with a set of key bytes and an IV and digests the concatenation of these to create the first block of pseudo random bytes. These are then xored in sequence with the bytes of the cleartext to create the ciphertext bytes. When the block of pseudo random bytes has been used one then creates a new block by digesting the the last N ciphertext bytes concatenated with the key bytes. This process is repeated until all the cleartext has been encrypted.

Obviously the key has to be kept secret and although an IV does not need to be kept secret and can be shipped in the clear, in common with pretty much all stream ciphers, a particular IV should never ever be used more than once.

Two point to bear in mind :-

1) the above encryption scheme has had very little analysis and cannot be recommended. Since AES is the 'standard' symmetric encryption algorithm of much of the world one should use AES or whichever algorithm your government prefers. One will never get sacked if ones government's preferred encryption algorithm is shown to be flawed but if any home grown algorithm is flawed then one needs to have a very very very good reason to have used it in preference to the preferred encryption algorithm .

2) it is generally considered insecure to encrypt passwords and it is usually better to use a randomly seeded digest.



Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

James Sabre wrote:then this is most definitely possible.


Only if you mean "encipher and decipher using an untrusted algorithm that happens to have MD5, SHA1, etc. as one part"

You are proposing a hack that uses the crypto-hash as a key generator to a stream cipher.

Later on, you properly say "just use AES" or TwoFish, BlowFish, etc. which is the real advice.

Home-grown ciphers nearly always have fatal flaws. Using the known, proven, and already implemented ciphers is not only easier than hacking something together, its far more likely to actually achieve what your goal is. At least if the goal is to secure the passwords. Even with a proven algorithm, it is easy to screw up and lose your security.

At a higher level, the standard is to use a one-way hash for a reason. There is no practical advantage to being able to decrypt a password, and there are many practical disadvantages. In short, not only is doing what the OP asked impossible as phrased, but even if you changed it to a more generic "How do I crypt and decrypt passwords in my system" the only proper answer is "don't do that"

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Pat Farrell wrote:
James Sabre wrote:then this is most definitely possible.


Only if you mean "encipher and decipher using an untrusted algorithm that happens to have MD5, SHA1, etc. as one part"


As I indicated in my response.


You are proposing a hack that uses the crypto-hash as a key generator to a stream cipher.


No. Far from it. I am just trying to correct the impression gained from the rest of the thread that a Digest cannot be used as the basis for a encryption. It can but I did not and do not propose it should be used.


Later on, you properly say "just use AES" or TwoFish, BlowFish, etc. which is the real advice.

Home-grown ciphers nearly always have fatal flaws. Using the known, proven, and already implemented ciphers is not only easier than hacking something together, its far more likely to actually achieve what your goal is. At least if the goal is to secure the passwords. Even with a proven algorithm, it is easy to screw up and lose your security.

At a higher level, the standard is to use a one-way hash for a reason. There is no practical advantage to being able to decrypt a password, and there are many practical disadvantages. In short, not only is doing what the OP asked impossible as phrased, but even if you changed it to a more generic "How do I crypt and decrypt passwords in my system" the only proper answer is "don't do that"


Nothing in my response is meant to endorse the use of a home grown encryption algorithm or the use of encryption for when storing passwords; I thought I had made that very very clear. My motive was to dispel the impression that a message digest could not be used as the basis for encryption. The method I described is not mine; if I can find a reference that describes it I will post the link.

Edit : Section 14.11 in "Applied Cryptography" second edition by Bruce Schneier describes several schemes in the same vein as the one I outlined. If not the the actual bible, this book is at least considered one of the gospels of cryptography.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

James Sabre wrote:Edit : Section 14.11 in "Applied Cryptography" second edition by Bruce Schneier describes several schemes in the same vein as the one I outlined. If not the the actual bible, this book is at least considered one of the gospels of cryptography.


And for that tiny percentage of programmers who have read and understand Applied Cryptography, its only a minor sin to do as you propose. For the vast majority, the instructions should be clear and unambiguous: use AES or some real cipher when you want to encipher. And never, never encipher a password, hash it.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Pat Farrell wrote:
James Sabre wrote:Edit : Section 14.11 in "Applied Cryptography" second edition by Bruce Schneier describes several schemes in the same vein as the one I outlined. If not the the actual bible, this book is at least considered one of the gospels of cryptography.


And for that tiny percentage of programmers who have read and understand Applied Cryptography, its only a minor sin to do as you propose.


I have re-read my first response several times just to make sure I said what I wanted to say and 'propose' is far far too strong a word. To dispel a myth I described an approach to cryptography using message digest that could be used and then went on to say it should not be used.

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4637
    
    5

James Sabre wrote:I described an approach to cryptography using message digest that could be used and then went on to say it should not be used.


I fail to see any reason that you have posted the approach, disclaimers tend to be ignored. But I'm not interested in getting into a he-said discussion.

To the OP, don't do what you are asking. Its both impossible without ugly hacks and bad practice.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Pat Farrell wrote:
James Sabre wrote:I described an approach to cryptography using message digest that could be used and then went on to say it should not be used.


I fail to see any reason that you have posted the approach


And I fail to see why you choose to deliberately misrepresent and belittle what I have written. I find it sad that someone with a "Rancher" ranking should do this.

The 'he-said' discussion will only be necessary if you keep on with this seemingly deliberate misrepresentation.

I'm finished with this.

Bye.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Decrypt password
 
Similar Threads
cryptography vs security
md5 decryption
how do i encrypt/decrypt query string.
encrypted username breaks sql
How to convert the ascii code to character