-6
public static final String encodeMd5(final String s){
        final String MD5 = "MD5";
        try {
            // Create MD5 Hash
            MessageDigest digest = MessageDigest
                    .getInstance(MD5);
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuilder hexString = new StringBuilder();
            for (byte aMessageDigest : messageDigest) {
                String h = Integer.toHexString(0xFF & aMessageDigest);
                while (h.length() < 2)
                    h = "0" + h;
                hexString.append(h);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

Now I need the decryption phase so that the client can login to his account.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ha Jer
  • 23
  • 1
  • 6
  • 2
    MD5 is not a good choice for hashing passwords. Consider [PBKDF2](http://stackoverflow.com/questions/11412882/). – Dour High Arch May 28 '16 at 00:49
  • 2
    MD5 is not encryption. – zaph May 28 '16 at 03:35
  • What is the "decryption phase" you are after? Do you mean a piece of code that will do decryption for you? As EJP says, you don't need to (and should not try to) decrypt the hashed password - it is not possible. Instead you need to re-hash an entered password, and compare the hashes. – halfer May 28 '16 at 11:06

1 Answers1

3

You have a basic misunderstanding which could fail you in the course. Revise. MD5 is not encryption. It is hashing. You cannot decrypt it. You don't need to decrypt it. You need to MD5 the entered password and compare it with the stored MD5 of the real password.

You are also almost certainly expected to use database functions for the MD5 and hex-encoding, not Java code. If indeed hex-encoding is required at all.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thnx for the information. But, How can I solve this ? I'm a beginner, please help me ? – Ha Jer May 28 '16 at 00:54
  • 1
    What part of the sentence starting 'you need to' don't you understand? You are expected to solve this yourself, and if you can't you have no right to graduate. It's astonishing you've got this far. – user207421 May 28 '16 at 00:57
  • 1
    @HaJer You say "I'm in my graduation project" and also "I'm a beginner"? That seems to be a contradiction. – zaph May 28 '16 at 03:36