2017-11-06 8 views
1

레인보우 테이블을 사용하여 길이 4의 해시 및 해킹 프로그램을 작성하려고합니다. 해시 값에 대한 알고리즘은 다음과 같습니다. ℎℎ = (163 * ℎ 0) + (162 * ℎ1 + (161 * ℎ2) + (160 * ℎ3))Java Rainbow Tables- 계산 방법

내가 잘못한 것을 알아내는 데 도움이 필요합니다. . 컴퓨팅 방식의 코드 앞에 주석이 그 문제를 해결하는 데 도움이 될 것입니다 경우에 무엇이 필요한지있다

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; 
} 

}

+2

"내가 뭘 잘못했는지 알아 내면"어떤 행동을 기대했는지, 실제로 어떤 행동을했는지 말하면 더 적절합니다. – slim

답변

1

여기에서 문제는 while 루프입니다.

while(f())은 "f()이 true를 반환하는 한 계속 수행"을 의미합니다.

당신은 AAAA ZZZZ ""와 password로 끝 ","오래과 password 시작과 같이이 일을 계속 "라고했습니다.

우리는 당신이 password을 초기화하지만이 네 개의 문자 문자열 인 경우 설명이 표시되지 않습니다 "aaaa"로 시작하고 "zzzz"로 끝나는 것은 불가능합니다. 따라서 while 루프의 블록은 실행되지 않습니다.

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"와.

+0

원래 질문을 편집하고 답변을 변경하는 경우 나머지 클래스 코드를 추가했습니다. –