gpt4 book ai didi

java md5编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:47:40 26 4
gpt4 key购买 nike

我只是尝试在java中使用MD5库,但是我遇到了一些错误,

当我尝试编译它时,出现此错误:

digest(byte[],int,int) in java.security.MessageDigest cannot be applied to (byte[])

I try to compile it by wtk (j2me)What is the problem ?. thanks

here is the code

import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
public static String getMD5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String hashtext = number.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println(getMD5("Javarmi.com"));
}
}

最佳答案

既然你使用WTK,也许你有this version of MessageDigest ,它没有digest(byte[])。所以,你需要写这样的东西:

int MD_SIZE = 16;
byte[] messageDigest = new byte[MD_SIZE];
byte[] message = ...;
md.update(message, 0, message.length);
md.digest(messageDigest, 0, MD_SIZE);

另请注意,您使用了String.getBytes(),因此您的摘要取决于系统默认编码。您需要使用String.getBytes(String encoding)才能获得可移植的结果。

关于java md5编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4004615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com