我正在尝试使用Rainbow Tables编写一个程序来散列和破解长度为4的密码。哈希值的算法为:ℎℎ=(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;
}
}
答案 0 :(得分:1)
这里的问题是你的while
循环。
while(f())
表示&#34;只要f()
返回true&#34;就继续这样做。
你已经说过,只要password
以&#34; aaaa&#34;开头,就会继续这样做。并且password
以&#34; zzzz&#34;。
我们没有看到你初始化password
,但如果它是一个如上所述的四个字符串字符串,它就不可能以&#34; aaaa&#34;并以&#34; zzzz&#34;结束,因此while循环的块将永远不会执行。
如果password
为"aaaazzzz"
以便条件为真,那么,因为您永远不会修改password
的值,所以while循环将永远重复。
您可能需要以下内容:
for(int i=0;; i++) {
String password = createPassword(i);
rainbow.add(password, hash(password));
}
...并编写方法createPassword()
,以便createPassword(0)
返回&#34; aaaa&#34;,createPassword(1)
返回&#34; aaab&#34;,{{1返回&#34; aaba&#34;等等。