-1

Please let me know equivalent code to decrypt. I have encrypted my password using this encode method and now i want to decrypt now.

MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(password.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
byte hashedBytes[] = (new String(digest.digest(), "UTF-8")).getBytes();
System.out.println(encoder.encode(hashedBytes))
  • 1
    This is a duplicate of [How to decrypt SHA-256 encrypted String?](http://stackoverflow.com/questions/9316437/how-to-decrypt-sha-256-encrypted-string), the difference in the hash function is not critical. Also, please read [Difference between Hashing a Password and Encrypting it](http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it) – Oleg Estekhin May 23 '14 at 03:48
  • You should read [How to securely hash passwords?](http://security.stackexchange.com/questions/211/how-to-securely-hash-passwords). It explains why you don't need to decrypt to verify a password and why MD5 or even SHA-2 are bad choices for password hashing. – CodesInChaos May 23 '14 at 11:07

1 Answers1

1

Short answer is you can't. MD5 is a hash, which means that data "encrypted" with it theoretically cannot be turned back into the original data. It's a one-way function, that (theoretically) cannot be reversed. Read up on cryptographic hash functions to find out more.

It's like if you had a machine that processed books by returning the number of pages in the book. You can feed it a book, and you'll get a value back, but given only the output from the machine it's impossible to tell what was fed in.


More details:

From the Wikipedia page for a cryptographic hash function:

A cryptographic hash function is a hash function that takes an arbitrary block of data and returns a fixed-size bit string, the cryptographic hash value, such that any (accidental or intentional) change to the data will (with very high probability) change the hash value.

The ideal cryptographic hash function has four main properties:

  • it is easy to compute the hash value for any given message
  • it is infeasible to generate a message that has a given hash
  • it is infeasible to modify a message without changing the hash
  • it is infeasible to find two different messages with the same hash.

Note bullet point 2. This means that it'll be effectively impossible to produce your password from the hash.

Granted, MD5 is considered cryptographically unsafe, but it still means that there isn't a general way to go from hash to input.

Community
  • 1
  • 1
awksp
  • 11,764
  • 4
  • 37
  • 44