Java Cryptography Extension giving me different result every time
sa sam
Ranch Hand
Joined: Mar 01, 2009
Posts: 46
posted
0
i am using Java Cryptography Extension to encrypt/decrypt the password but it is giving me different encrypted string every time even though i am passing same string my sample code is as follows -
Jesper Young wrote:You should generate the key only once, and then store it and re-use it for encrypting and decrypting.
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
posted
0
Actually you should never need to decrypt the password. All you need is a one way hash, you can then store the hashed password in the database. When the user goes to login, you just hash the password that the user enters by the same method and check whether the two hashes are the same. You should also salt the password with a random salt to help prevent multiple accounts from being compromised using a rainbow table should your database become compromised. The salt can be stored in the database along with the hashed password. All this can easily be done using java.security.MessageDigest, and java.security.SecureRandom.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
ajay chavan wrote:i am not asking how to store value. i am asking about, how to get constant encryption value.
What your code above does, is like this: Every time you go out of your house and lock the door, you put a completely new lock with a new key on the door. And your questions is like: "Why does the key look different every time after I locked my door?". Answer: Because you're putting a new lock on the door every time...
ajay chavan wrote:i am not asking how to store value. i am asking about, how to get constant encryption value.
What your code above does, is like this: Every time you go out of your house and lock the door, you put a completely new lock with a new key on the door. And your questions is like: "Why does the key look different every time after I locked my door?". Answer: Because you're putting a new lock on the door every time...
Also, the key is not saved by the encrypt() method... so it more like "every time you go out of your house and lock the door, you put a completely new lock with a new key on the door. And throw away the key once you drive off".