您的位置:首页 > 其它

MD5算法加密~16位、32位、64位

2016-11-30 18:20 405 查看
import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

//Java Build Path->Libraries(选中)->Edit->Workspace default JRE

public class MD5 {

    private static final String ALGORITHM_MD5 = "MD5";

    public static final String MD5_16bit(String readyEncryptStr)

            throws NoSuchAlgorithmException {

        if (readyEncryptStr != null) {

            return MD5.MD5_32bit(readyEncryptStr).substring(8, 24);

        } else {

            return null;

        }

    }

    public static final String MD5_32bit(String readyEncryptStr)

            throws NoSuchAlgorithmException {

        if (readyEncryptStr != null) {

            MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);

            md.update(readyEncryptStr.getBytes());

            byte[] b = md.digest();

            StringBuilder su = new StringBuilder();

            for (int offset = 0, bLen = b.length; offset < bLen; offset++) {

                String haxHex = Integer.toHexString(b[offset] & 0xFF);

                if (haxHex.length() < 2) {

                    su.append("0");

                }

                su.append(haxHex);

            }

            return su.toString();

        } else {

            return null;

        }

    }

    public static final String MD5_32bit1(String readyEncryptStr)

            throws NoSuchAlgorithmException {

        if (readyEncryptStr != null) {

            StringBuilder su = new StringBuilder();

            MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);

            byte[] b = md.digest(readyEncryptStr.getBytes());

            int temp = 0;

            for (int offset = 0, bLen = b.length; offset < bLen; offset++) {

                temp = b[offset];

                if (temp < 0) {

                    temp += 256;

                }

                int d1 = temp / 16;

                int d2 = temp % 16;

                su.append(Integer.toHexString(d1) + Integer.toHexString(d2));

            }

            return su.toString();

        } else {

            return null;

        }

    }

    public static final String MD5_32bit2(String readyEncryptStr)

            throws NoSuchAlgorithmException {

        if (readyEncryptStr != null) {

            StringBuilder su = new StringBuilder();

            MessageDigest md = MessageDigest.getInstance(ALGORITHM_MD5);

            md.update(readyEncryptStr.getBytes());

            byte[] b = md.digest();

            int temp = 0;

            for (int offset = 0, bLen = b.length; offset < bLen; offset++) {

                temp = b[offset];

                if (temp < 0) {

                    temp += 256;

                }

                if (temp < 16) {

                    su.append("0");

                }

                su.append(Integer.toHexString(temp));

                System.out.println("result: " + su.toString());// 32位的加密

                System.out.println("result: " + su.toString().substring(8, 24));// 16位的加密

            }

            return su.toString();

        } else {

            return null;

        }

    }

    public static final String MD5_64bit(String readyEncryptStr)

            throws NoSuchAlgorithmException {

        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(readyEncryptStr.getBytes());

        byte[] b = md.digest();

        BASE64Encoder base64Encoder = new BASE64Encoder();

        return base64Encoder.encode(b);

    }

    public static void main(String[] args) {

        try {

            String md516 = MD5.MD5_16bit("90504");

            System.out.println("16bit-md5:\n" + md516);

            String md532 = MD5.MD5_32bit("90504");

            String md5321 = MD5.MD5_32bit1("90504");

            String md5322 = MD5.MD5_32bit2("90504");

            System.out.println("32bit-md5:");

            System.out.println("1:  " + md532);

            System.out.println("2:  " + md5321);

            System.out.println("3:  " + md5322);

            String md564 = MD5.MD5_64bit("90504");

            System.out.println("64bit-md5:\n" + md564);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: