Convert MD5 to string in java

Does anyone know how to convert MD5 to String . In my case, I saved the password in MD5 in the database. I am trying to recover a password and display it in a string for editing purposes.

This is what I did to convert the string to encryption format:

public static String encrypt(String source) {
   String md5 = null;
   try {
         MessageDigest mdEnc = MessageDigest.getInstance("MD5"); //Encryption algorithm
         mdEnc.update(source.getBytes(), 0, source.length());
         md5 = new BigInteger(1, mdEnc.digest()).toString(16); // Encrypted string
        } 
    catch (Exception ex) {
         return null;
    }
    return md5;
}

I do not know how to convert the encryption format to a string for editing a password.

+5
source share
4 answers
    String password = "123456";

    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(password.getBytes());

    byte byteData[] = md.digest();

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++)
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));

    System.out.println("Digest(in hex format):: " + sb.toString());
+11
source

MD5 is a one-way hash function. Therefore, you cannot decrypt it. This is why on many websites you have the option to "get a password" by creating a new one.

More on MD5

+6
source

String MD5

private static String convertToMd5(final String md5) throws UnsupportedEncodingException {
        StringBuffer sb=null;
        try {
            final java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            final byte[] array = md.digest(md5.getBytes("UTF-8"));
            sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (final java.security.NoSuchAlgorithmException e) {
        }
        return sb.toString();
    }
0

, :

public String getMD5(String data) {
        String result = null;
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(data.getBytes(Charset.forName("UTF-8")));
            result = String.format(Locale.ROOT, "%032x", new BigInteger(1, md.digest()));
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }
        return result;
}

`

-3

All Articles