encrypt String with MD5 - Java Security

Java examples for Security:MD5

Description

encrypt String with MD5

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        long uin = 2;
        String plain = "java2s.com";
        String verify = "java2s.com";
        System.out.println(encrypt(uin, plain, verify));
    }//from w  ww.  j a  v  a  2 s.  c  o  m

    public static String encrypt(long uin, String plain, String verify) {
        byte[] data = concat(md5(plain.getBytes()), long2bytes(uin));
        String code = byte2HexString(md5(data));
        data = md5((code + verify.toUpperCase()).getBytes());
        return byte2HexString(data);
    }

    private static byte[] concat(byte[] bytes1, byte[] bytes2) {
        byte[] big = new byte[bytes1.length + bytes2.length];
        System.arraycopy(bytes1, 0, big, 0, bytes1.length);
        System.arraycopy(bytes2, 0, big, bytes1.length, bytes2.length);
        return big;
    }

    private static byte[] md5(byte[] bytes) {
        MessageDigest dist = null;
        byte[] result = null;
        try {
            dist = MessageDigest.getInstance("MD5");
            result = dist.digest(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
        return result;
    }

    private static byte[] long2bytes(long i) {
        byte[] b = new byte[8];
        for (int m = 0; m < 8; m++, i >>= 8) {
            b[7 - m] = (byte) (i & 0x000000FF);
        }
        return b;
    }

    private static String byte2HexString(byte[] b) {
        StringBuffer sb = new StringBuffer();
        char[] hex = new char[] { '0', '1', '2', '3', '4', '5', '6', '7',
                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        if (b == null)
            return "null";
        int offset = 0;
        int len = b.length;
        // ??????
        int end = offset + len;
        if (end > b.length)
            end = b.length;
        sb.delete(0, sb.length());
        for (int i = offset; i < end; i++) {
            sb.append(hex[(b[i] & 0xF0) >>> 4]).append(hex[b[i] & 0xF]);
        }
        return sb.toString();
    }
}

Related Tutorials