我正在编写一个程序,该程序创建随机字符串,然后对字符串进行哈希处理以获取其 MAC。然后我想查看哈希的第一个字节,看看是否等于特定的十六进制值。(简化了图像前攻击。)我的代码成功地从每个哈希中提取了第一个字节,但没有正确比较它。因此,即使两个字节相等,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;