-1

I have gotten a code from php.net. http://php.net/manual/en/book.mcrypt.php

Problem is when you encrypt something, the next time the ecrypted one isn't the same as the first one. I need to get the exact same hash using sha512 or sha256. I also need to decrypt it because the function will be used for encrypting customer's name and other data.

Thanks in advance!

jww
  • 97,681
  • 90
  • 411
  • 885
  • 7
    SHA-512 is a one-way hashing function, and is theoretically impossible to decrypt. Also, there's nothing bad, with the same input producing a different encrypted output (in fact, that's usually more secure). As long as you get back the original input, that's not a problem – knittl Apr 06 '12 at 08:22
  • It's unclear what exactly you are trying to do. Do you want to compare a new input with an existing encrypted value? → decrypt and compare. Or you might store a hash value in addition to the encrypted value, then you can hash the new input and compare it with the stored hash. – knittl Apr 06 '12 at 08:26
  • As @knittl says you _don't_ decrypt something that has been encrypted using a one-way hashing function. – David-SkyMesh Apr 06 '12 at 08:26
  • @David-SkyMesh: Because you don't _encrypt_ anything with a hashing function! You _hash_ something. – knittl Apr 06 '12 at 08:26
  • Obviously, but the OP hasn't specified what he's doing. – David-SkyMesh Apr 06 '12 at 08:27
  • There are a ton of encrypt/decrypt examples within the comments @ php.net http://www.php.net/manual/en/function.mcrypt-encrypt.php – Lawrence Cherone Apr 06 '12 at 08:32
  • Theoretically, I think there should be a way to get the same hash value because how can you perform a search of the names or customer or a product if you can't get the hash function. If you will unhash everything, then compare you'd exeed the 30s execution time.. – Earvin Bryan S. Co Apr 06 '12 at 08:33
  • possible duplicate of [Best way to use PHP to encrypt and decrypt?](http://stackoverflow.com/questions/1289061/best-way-to-use-php-to-encrypt-and-decrypt) – Alix Axel Apr 06 '12 at 08:34
  • The user input is hashed with the same algorithm before it is compared with database values – Lawrence Cherone Apr 06 '12 at 08:35
  • @EarvinBryanS.Co: A hashing function is deterministic, for a hash (not for encryption), you will always get the same output for the same input. But you cannot "reverse" the hash, it's a one-way function. If you need to get the original data, you have to use encryption. That might produce a totally different output every time – **but** you will be able decrypt it. – knittl Apr 06 '12 at 08:40
  • Possible duplicate of [Encrypting / Decrypting file with Mcrypt](http://stackoverflow.com/questions/2448256/encrypting-decrypting-file-with-mcrypt). See John Conde's answer. It provides a generic routine to encrypt and decrypt (not tied to a file). – jww Oct 11 '14 at 23:17

2 Answers2

0

Instead of using mcrypt for hashing, consider using the hash() function instead.

Remember, hashes is one-way methods and cannot be 'decrypted'.

Looking for encryption/decryption I would recommend you look at AES encryption - either through MySQL if you have your data stored in the database, otherwise mcrypt() can also manage AES.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Repox
  • 15,015
  • 8
  • 54
  • 79
  • It is not true, it can be decrypted if not used with salt. https://crackstation.net/ – John Max Nov 07 '16 at 12:02
  • 1
    @JohnMax, it's not "decryption". It's just matching identical hashes by looking them up in rainbow tables. – Repox Nov 07 '16 at 13:45
0

I shared my crypt wrapper at https://stackoverflow.com/a/173764/17404. Try using that.

Community
  • 1
  • 1
Shoan
  • 4,003
  • 1
  • 26
  • 29