Java使用MD5

        MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致。是计算机广泛使用的杂凑算法之一(又译摘要算法、哈希算法)。

jvav具体实现:

public static String string2MD5(String inSrc) {
        String result = null;
        MessageDigest messagedigest = null;

        try {
            messagedigest = MessageDigest.getInstance("MD5");
            byte[] bytes = inSrc.getBytes();
            byte[] targ = messagedigest.digest(bytes);
            result = bintoascii(targ);
        } catch (NoSuchAlgorithmException nsaex) {
            throw new RuntimeException(nsaex);
        }

        return result;
    }

 将md5加密后的128位(bit)的结果转换成16进制字符表示的字符串

 private static String bintoascii(byte[] bySourceByte) {
        int len, i;
        byte tb;
        char high, tmp, low;
        String result = new String();
        len = bySourceByte.length;
        for (i = 0; i < len; i++) {
            tb = bySourceByte[i];

            tmp = (char) ((tb >>> 4) & 0x000f);
            if (tmp >= 10)
                high = (char) ('a' + tmp - 10);
            else
                high = (char) ('0' + tmp);
            result += high;
            tmp = (char) (tb & 0x000f);
            if (tmp >= 10)
                low = (char) ('a' + tmp - 10);
            else
                low = (char) ('0' + tmp);

            result += low;
        }
        return result;
    }

你可能感兴趣的:(java,开发语言)