Encrypts the password by MD5 encrypt twice. - Java javax.persistence

Java examples for javax.persistence:EntityManager

Description

Encrypts the password by MD5 encrypt twice.

Demo Code


//package com.java2s;
import java.security.MessageDigest;

public class Main {
    public static void main(String[] argv) throws Exception {
        String password = "java2s.com";
        System.out.println(encrypt(password));
    }//from   www.  jav a2  s  .c o  m

    /**
     * Encrypts the password by MD5 encrypt twice.
     * @param password
     * @return
     */
    public static String encrypt(String password) {
        return getMD5(getMD5(password));
    }

    /**
     * Gets the MD5 encryption of given String.
     * @param password
     * @return
     */
    private static String getMD5(String password) {
        byte[] result = null;
        try {
            result = MessageDigest.getInstance("MD5").digest(
                    password.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byte2str(result);
    }

    /**
     * Converts the byte array to String.
     * @param byteStr the byte array
     * @return
     */
    private static String byte2str(byte[] byteStr) {
        StringBuffer result = new StringBuffer();
        for (byte b : byteStr) {
            result.append(String.format("%02x", b));
        }
        return result.toString();
    }
}

Related Tutorials