简体   繁体   English

用Java生成MD5哈希-输出16个字符

[英]Generate MD5 hash in Java -Output 16 character

I have created MD5 hash.Its working fine now.I want output in 16 character.Current Code is returning 32 characters. 我已经创建了MD5哈希,现在可以正常工作了,我希望输出16个字符,当前代码返回32个字符。

This is my code: 这是我的代码:

    try {
            String text = "Hello World";
            MessageDigest msg = MessageDigest.getInstance("MD5");
            msg.update(text.getBytes(), 0, text.length());
            String digest1 = new BigInteger(1, msg.digest()).toString(16);
            System.out.println("MD5: " + digest1.length());
            System.out.println("MD5: " + digest1);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(AndroidActivationView.class.getName()).log(Level.SEVERE, null, ex);
        }

Where I want to change it. 我要更改的地方。

How can i get the 16 character output.? 我如何获得16个字符的输出?

Thanks in advance; 提前致谢;

Cut the string to length: digest1 = digest1.substring(0, 16); 将字符串digest1 = digest1.substring(0, 16);长度: digest1 = digest1.substring(0, 16);

MD5 outputs 16 bytes. MD5输出16个字节。 If you encode it in hexadecimal, its 32 characters. 如果以十六进制编码,则为32个字符。 If you encode it in base-64 it's 24 characters. 如果以base-64编码,则为24个字符。 Base-85 will squeeze it into 20 characters. Base-85会将其压缩为20个字符。 There's no well-known encoding that is one character per byte. 没有众所周知的编码是每个字节一个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM