【问题标题】:Java compare md5 with another md5 from another generator : not same value [duplicate]Java将md5与来自另一个生成器的另一个md5进行比较:值不同[重复]
【发布时间】:2020-11-18 01:15:03
【问题描述】:

我可以用这个函数生成md5:

private void generateMd5() throws NoSuchAlgorithmException, IOException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hash = md.digest(pathFile.getBytes());
        nomGestionnaire.setText(String.valueOf(String.format("%032X", new BigInteger(1, hash))));

    }

我的问题是当我将我的 md5 生成器与另一个 md5 生成器进行比较时,我没有相同的值。 正常吗?好像我的生成器没有生成 real md5

使用此文件进行测试:aaa.txt(内容:aaa)

我的发电机:A4FA953DB4BC7772E5AF67BD706B9110

其他生成器:47bce5c74f589f4867dbd57e9ca9f808

编辑:

FileChooser fileChooser = new FileChooser();
File selectedFile = new 
File(String.valueOf(fileChooser.showOpenDialog(primaryStage)));
nameFile = selectedFile.getName();
pathFile = selectedFile.getPath();

【问题讨论】:

  • 您正在计算字符串的 md5(文件路径)。我的第一个怀疑是编码问题(即字符串可以用不同的字节序列表示)。您能否将您的输入和生成的两个 md5 和添加到您的问题中?
  • @Software149735:pathFile的类型和值是什么?
  • @Software149735 您是要检查/运行文件路径的 md5 校验和还是要检查/运行文件内容的 md5 校验和?
  • “pathFile”是文件的路径,还是文件的内容?
  • @Software149735 所以有你的问题:你正在比较 md5("aaa") 和 md5("C:\Users\Software149735\aaa.txt") 或类似的东西。当然这些不一样!

标签: java


【解决方案1】:

我猜输入有一些错误。不幸的是,您提供的 and 文件不完整。所以我做了我首先写了一个Java方法来做一个基本的md5。然后我做了一些取证来猜测和修复代码。两者都提供正确的 MD5:47BCE5C74F589F4867DBD57E9CA9F808

    public static String getMD5(String filename)
        throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(Files.readAllBytes(Paths.get(filename)));
    byte[] digest = md.digest();
    String myChecksum = DatatypeConverter.printHexBinary(digest).toUpperCase();
    return myChecksum;
}



public static String generateMd5(String pathToFile) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] hash = md.digest(Files.readAllBytes(Paths.get(pathToFile)));
    return String.valueOf(String.format("%032X", new BigInteger(1, hash)));
}

【讨论】:

  • 完美!有用 !谢谢!
【解决方案2】:

我之前遇到过这个问题,可能是编码问题(来自那个文件路径)。 我把它放在我的项目中,从那时起它工作得很好。 我会为您提供一个示例,因此您可能会从中得到一个想法:

public String hash(String stringToHash){
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    // here you init the message digit singleton with the wanted algorithm
    messageDigest.update(stringToHash.getBytes());
    //now you updated it, with the bytes of your string
    byte[] digest = messageDigest.digest();
    String result = DatatypeConverter.printHexBinary(digest);
    // finally you converted the result (hex) to String (you hashed string)
    // you may want to use toLowerCase() so you make it case insensitive
    return result;
}

注意:你有很多使用 md5 散列的方法,我真的更喜欢使用 Apache Commons 作为一种简单的方法,你可能想看看MD5 Hashing using Apache Commons。 您也可以使用 Google Guava:MD5 Hashing using Google Guava

【讨论】:

    猜你喜欢
    • 2015-03-24
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多