本文介绍了Java MessageDigest不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使MessageDigest工作,程序给我两个错误:
UnsupportedEncodingException,NoSuchAlgorithmException

I can't make MessageDigest work, the program gives me two error:UnsupportedEncodingException, NoSuchAlgorithmException

 byte[] bytesOfchat_key = "lol".getBytes("UTF-8");
 MessageDigest md = MessageDigest.getInstance("MD5");
 byte[] Digest = md.digest(bytesOfchat_key);

如果我抛出错误,它会给我ワ￟ㅄ9ㅔヌnp> 0xd￉z as回复(16个字符)

If I throw the errors, it give me ワ￟ᄡ9ᅦヌnp>0xd￉z as response ( 16 chars )

PS:我曾经打印过摘要

PS: I have used to print the Digest

for (byte b : Digest) {
    System.out.print((char)b);
}


推荐答案

md5返回十六进制数字,所以用于将其解码为可以使用的字符串

md5 returns hexadecimal numbers, so for decoding it to a String you could use

String plaintext = "lol";
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(plaintext.getBytes());
byte[] digest = m.digest();
//Decoding
BigInteger bigInt = new BigInteger(1,digest);
String hashtext = bigInt.toString(16);
while(hashtext.length() < 32 ){
  hashtext = "0"+hashtext;
}

这篇关于Java MessageDigest不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 16:02