简体   繁体   English

比较两个十六进制值作为字符串

[英]Comparing two hex values as strings

I'm writing a program that creates random strings and then hashes the string to get its MAC. 我正在编写一个程序,该程序创建随机字符串,然后对字符串进行哈希处理以获取其MAC。 I want to then look at the first byte for the hash and see if is equal to a specific hex value. 然后,我想查看哈希的第一个字节,看看是否等于特定的十六进制值。 (Pre-image attack simplified.) My code successfully pulls the first byte off of each hash but doesn't compare it correctly. (简化了图像前攻击。)我的代码成功地从每个哈希中提取了第一个字节,但未正确比较它。 So even if the the two bytes are equal, the while loop doesn't recognize it and keeps going indefinitely. 因此,即使两个字节相等,while循环也无法识别它,并且会无限期地进行下去。

    Random generator = new Random();
    Boolean found = false;
    int i;
    String test="";
    int whatIWant = 169;


    while(found == false)
    {

        String x = "";
        i = 0;

    while(i<15000)
    {   //String x = "";


        int y = generator.nextInt(220)+20;
        x = x + Integer.toHexString(y);
        i++;
    }
    byte[] hexMessage = DatatypeConverter.parseHexBinary(x);
    MessageDigest cript = MessageDigest.getInstance("SHA-512");
    cript.reset();
    cript.update(hexMessage);
    byte[] hash = cript.digest();

    test = String.format("%02X ", hash[0]);

    if(test.equalsIgnoreCase(Integer.toHexString(whatIWant).toString()))
        found = true;

I didn't run your code. 我没有运行您的代码。 I would like to see the result of Integer.toHexString() and I am not sure why you are calling the string returned by Integer.toHexString() to be a string again by .toString() although it is not a big issue since the value should be the same. 我想查看Integer.toHexString()的结果,但我不确定为什么您要通过.toString()再次将Integer.toHexString()返回的字符串称为字符串,尽管这并不是一个大问题,因为值应相同。

All in all I think the outstanding issue may be you never closed your while loop... at least it isn't shown here. 总而言之,我认为悬而未决的问题可能是您从未关闭过while循环……至少这里没有显示。

You are searching through bytes ( hash[0] ) for a value (169) greater than the maximum value of a byte (127). 您正在字节( hash[0] )中搜索值(169),该值大于字节的最大值(127)。 That is one reason why your search never finishes. 这就是您的搜索从未完成的原因之一。 Values > 127 will never be there. 值> 127永远不会存在。

The next problem is that your String conversion pattern "%02X " introduces a space after the hex string. 下一个问题是您的字符串转换模式"%02X "在十六进制字符串之后引入一个空格。 Let's say you search for 127... "7F " will never equal "7F" , so again, your search will never finish, even for byte values within range. 假设您搜索127 ... "7F "将永远不会等于"7F" ,因此,即使对于范围内的字节值,搜索也将永远不会结束。

For interest, try adding this to your code: 出于兴趣,请尝试将其添加到您的代码中:

Outside the loop: 循环外:

Set<Integer> foundBytes = new TreeSet<Integer>();

At the end of the loop: 在循环结束时:

if (hash[0] != whatIWant) {
    if (foundBytes.add((int)hash[0])) {
        System.out.printf("[%3d] %s\n", foundBytes.size(), foundBytes);
    }
}

If you set your search value to be greater than 127, you will notice that the foundBytes set quickly fills up with all the possible values of byte after which no more new bytes are found and the print statement is not invoked. 如果将搜索值设置为大于127,则会注意到foundBytes设置快速填充了所有可能的byte值,此后不再找到新的字节,并且不调用print语句。

(Your code could be optimized in several ways BTW, but that is not the point of the question.) (顺便提一下,您的代码可以通过几种方式进行优化,但这不是问题的重点。)

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

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