2016-08-07 2 views
1

저는 파이썬을 배우기 시작했고 폭력적인 파이썬으로 시작했습니다. 첫 번째 장에서는 crypt 클래스의 도움으로 * nix 기반 암호 해시를 해독 할 스크립트에 대해 설명합니다. 나는 passwords.txt 사용자 이름을 포함 파일이똑같은 문자열이 참으로 돌아 오지 않습니다.

import crypt 
def testPass(cryptPass): 
    salt = cryptPass[0:2] 
    dictFile = open('dictionary.txt','r') 
    for word in dictFile.readlines(): 
     word = word.strip('\n') 
     cryptWord = crypt.crypt(word,salt) 
     print cryptPass+":"cryptWord 
     if(cryptPass == cryptWord): 
      print "[+] Password found : "+word 
      return 

print "[-] Password Not Found.\n" 
return 
def main(): 
    passFile = open('passwords.txt') 
    for line in passFile.readlines(): 
     if ":" in line: 
      user = line.split(':')[0] 
      cryptPass = line.split(':')[1].strip(' ') 
      print "[*] Cracking Password For: "+user 
      testPass(cryptPass) 
if __name__ == "__main__": 
main() 

: 여기에 코드 사전 words.These를 포함하는하는 dictionary.txt 암호의 내용을 암호 (암호 해시) 문자열 및 명명 된 다른 파일입니다 .txt 파일 :

apple:HXJintBqUVCEY 
mango:HXInAjNhZF7YA 
banana:HXKfazJbFHORc 
grapes:HXtZSWbomS0xQ 

및하는 dictionary.txt :

apple 
abcd 
efgh 
ijkl 
mnop 

testpass에서 계산 암호 해시() 메소드에 대한 usern passwords.txt에서 암호 해시 ame apple은 둘 다 인쇄 할 때 동등합니다. 그러나 4 개의 모든 사용자 이름에 대한 출력은 "[-] 비밀번호를 찾을 수 없습니다"입니다. 왜 == 여기 테스트가 실패합니까?

답변

1

아마도 줄의 끝에 \n이라는 꼬리가 있습니다.

 cryptPass = line.split(':')[1].strip(' ') 

에 : (의견 제안)

 cryptPass = line.split(':')[1].strip('\n').strip(' ') 

또는 간단한 : 변화를 시도 .strip에게 '('\ n ')를 사용

 cryptPass = line.split(':')[1].strip() 
+1

을'보다 간단 할 것 두 개의'strip' 메소드 호출을 연결합니다. –

+1

'.strip()'은 개행과 공백 모두에 적용됩니다 –

+0

고마워요. 그 트릭을 했어, 자바 배경에서 오는 마지막에 후행 \ n 줄 알았는데. –

관련 문제