gpt4 book ai didi

Java Rainbow Tables-计算方法

转载 作者:行者123 更新时间:2023-11-30 06:26:18 26 4
gpt4 key购买 nike

我正在尝试使用彩虹表编写一个程序来散列和破解长度为四的密码。哈希值的算法为: ℎ𝑎𝑠ℎ𝑉𝑎𝑙𝑢𝑒 = (163 ∗ 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑎𝑡 𝑝𝑜𝑠𝑡𝑖𝑜 𝑛 0) + (162 * 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑎𝑡 𝑝𝑜𝑠𝑡𝑖𝑜𝑛 1 + (161 * 𝑐ℎ𝑎𝑟𝑎𝑐𝑡 𝑒𝑟𝑎𝑡𝑝𝑜𝑠𝑡𝑖𝑜𝑛2)+(160*𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟𝑎𝑡𝑝𝑜𝑠𝑡𝑖 𝑜𝑛3)。

我需要帮助找出我在计算方法上做错了什么。代码前面有注释,说明需要做什么才能有助于解决问题。

public void compute() {
//TODO: Add code to compute all possible 4-letter passwords - store in rainbow
// Begin your possible passwords with aaaa and end with zzzz
// Use hashCode(pwd) % 29 to determine the key (index) of where this element should be added to rainbow's ArrayList of ArrayLists
// You will need to cast as int, such as key = (int) hashCode(pwd)%29 - key is the index of where to add this element in the rainbow ArrayList of ArrayLists
if(password.length() != passwordLength){
throw new InvalidPasswordException();
}
while(password.startsWith("aaaa") && password.endsWith("zzzz")){
key = (int) (hashCode(password)%29);
rainbow.add(key, password);
}
}

如果需要更多信息,请告诉我。

编辑:这是完整的代码,因此可以看到所有其他方法(尚未全部完成)

import java.util.*;
public class HashMap implements HashMapADT{
private String password;
private Long hash;
private int key;
private final int passwordLength = 4;
ArrayList<ArrayList<PasswordMap>> rainbow;

public HashMap() {
password = "";
hash = -1L;
key = -1;
initializeHashMap();
}
public HashMap(String str) {
password = str;
hash = hashCode(str);
key = (int)(hash % 29);
initializeHashMap();
}

private void initializeHashMap() {
//Initialize an ArrayList of 29 elements - when dividing by 29 only remainders 0 - 28 are possible
rainbow = new ArrayList<>();
for(int i = 0; i < 29; i++) {
rainbow.add(new ArrayList<PasswordMap>());
}
compute();
}

public Long hashCode(String str) throws InvalidPasswordException{
this.hash = 0L;

//TODO: Calculate hashCode using hashing function based on powers of 29

return 0L; // temp - delete once hashCode method is implemented.
}

public void compute() {
//TODO: Add code to compute all possible 4-letter passwords - store in rainbow
// Begin your possible passwords with aaaa and end with zzzz
// Use hashCode(pwd) % 29 to determine the key (index) of where this element should be added to rainbow's ArrayList of ArrayLists
// You will need to cast as int, such as key = (int) hashCode(pwd)%29 - key is the index of where to add this element in the rainbow ArrayList of ArrayLists
if(password.length() != passwordLength){
throw new InvalidPasswordException();
}
while(password.startsWith("aaaa") && password.endsWith("zzzz")){
key = (int) (hashCode(password)%29);
rainbow.add(key, password);
}
}
public Long hash(String pwd) {
//TODO: Return the hashcode for a given password
// First, hash the password: int key = (int)(hashCode(pwd) % 29);
// Use this key to determine which element in the rainbow table you should be traversing to find the hash code
// Recall rainbow is an ArrayList of ArrayLists!!
key = (int)(hashCode(pwd)%29);

return 0L; // temp - delete once hash method is implemented.
}

public String hack(Long pwdHash) {
String pwd="";
//TODO: Given a hashed password, pwdHash, determine the password
// When identifying a correct hashed password, you will need to look at a difference RATHER THAN ==
// That is,
//if (Math.abs(pwdHash - rainbow.get(key).get(i).getHash())<.001) - you've found your password!!
// Note: key is the location of the rainbow list you should be traversing: key = (int)((pwdHash) % 29);


return pwd;
}

@Override
public String toString() {
return password + ": " + hash;
}

}

最佳答案

这里的问题是你的 while 循环。

while(f()) 表示“只要 f() 返回 true,就继续执行此操作”。

您说过“只要 password 以“aaaa”开头且 password 以“zzzz”结尾,就继续执行此操作。

我们没有看到您初始化password,但如果它是如上所述的四个字符的字符串,则它不可能既以“aaaa”开头又以“zzzz”结尾,因此 while 循环的 block 永远不会执行。

如果 password"aaaazzzz" 且条件为 true,那么,由于您从未修改过 password 的值,因此 while循环将永远重复。

你可能想要这样的东西:

for(int i=0;; i++) {
String password = createPassword(i);
rainbow.add(password, hash(password));
}

...并编写一个方法createPassword(),使得createPassword(0)返回“aaaa”,createPassword(1)返回“aaab”,createPassword(26) 返回“aaba”等​​。

关于Java Rainbow Tables-计算方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47142228/

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