복붙노트

[PYTHON] TypeError 수정 방법 : 해시하기 전에 유니 코드 개체를 인코딩해야합니까?

PYTHON

TypeError 수정 방법 : 해시하기 전에 유니 코드 개체를 인코딩해야합니까?

나는이 오류가있다 :

Traceback (most recent call last):
  File "python_md5_cracker.py", line 27, in <module>
  m.update(line)
TypeError: Unicode-objects must be encoded before hashing

파이썬 3.2.2에서이 코드를 실행하려고하면 :

import hashlib, sys
m = hashlib.md5()
hash = ""
hash_file = input("What is the file name in which the hash resides?  ")
wordlist = input("What is your wordlist?  (Enter the file name)  ")
try:
        hashdocument = open(hash_file,"r")
except IOError:
        print("Invalid file.")
        raw_input()
        sys.exit()
else:
        hash = hashdocument.readline()
        hash = hash.replace("\n","")

try:
        wordlistfile = open(wordlist,"r")
except IOError:
        print("Invalid file.")
        raw_input()
        sys.exit()
else:
        pass
for line in wordlistfile:
        m = hashlib.md5()  #flush the buffer (this caused a massive problem when placed at the beginning of the script, because the buffer kept getting overwritten, thus comparing incorrect hashes)
        line = line.replace("\n","")
        m.update(line)
        word_hash = m.hexdigest()
        if word_hash==hash:
                print("Collision!  The word corresponding to the given hash is", line)
                input()
                sys.exit()

print("The hash given does not correspond to any supplied word in the wordlist.")
input()
sys.exit()

해결법

  1. ==============================

    1.아마도 wordlistfile에서 문자 인코딩을 찾고있을 것입니다.

    아마도 wordlistfile에서 문자 인코딩을 찾고있을 것입니다.

    wordlistfile = open(wordlist,"r",encoding='utf-8')
    

    또는 라인 단위로 작업하는 경우 :

    line.encode('utf-8')
    
  2. ==============================

    2.utf-8과 같은 인코딩 형식을 정의해야합니다. 이 쉬운 방법을 시도해보십시오.

    utf-8과 같은 인코딩 형식을 정의해야합니다. 이 쉬운 방법을 시도해보십시오.

    이 예제는 SHA256 알고리즘을 사용하여 난수를 생성합니다.

    >>> import hashlib
    >>> hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
    'cd183a211ed2434eac4f31b317c573c50e6c24e3a28b82ddcb0bf8bedf387a9f'
    
  3. ==============================

    3.암호 (PY3)를 저장하려면 다음과 같이하십시오.

    암호 (PY3)를 저장하려면 다음과 같이하십시오.

    import hashlib, os
    password_salt = os.urandom(32).hex()
    password = '12345'
    
    hash = hashlib.sha512()
    hash.update(('%s%s' % (password_salt, password)).encode('utf-8'))
    password_hash = hash.hexdigest()
    
  4. ==============================

    4.오류는 이미 당신이해야 할 일을 말합니다. MD5는 바이트에서 작동하므로 유니 코드 문자열을 바이트로 인코딩해야합니다. line.encode ( 'utf-8')을 사용하십시오.

    오류는 이미 당신이해야 할 일을 말합니다. MD5는 바이트에서 작동하므로 유니 코드 문자열을 바이트로 인코딩해야합니다. line.encode ( 'utf-8')을 사용하십시오.

  5. ==============================

    5.그 대답을 먼저 들여다보십시오.

    그 대답을 먼저 들여다보십시오.

    이제 오류 메시지는 분명합니다. 파이썬 문자열 (파이썬 <3)에서 유니 코드로 사용했던 바이트가 아닌 바이트 만 사용할 수 있으므로 원하는 인코딩으로 문자열을 인코딩해야합니다. utf-32, utf-16, utf -8 또는 제한된 8 비트 인코딩 (일부는 코드 페이지라고하는 것) 중 하나입니다.

    단어 목록 파일의 바이트는 파일에서 읽으면 Python 3에 의해 자동으로 유니 코드로 디코딩됩니다. 나는 다음과 같이 제안한다.

    m.update(line.encode(wordlistfile.encoding))
    

    그래서 md5 알고리즘으로 푸시 된 인코딩 된 데이터는 원본 파일과 똑같이 인코딩됩니다.

  6. ==============================

    6.이진 모드로 파일을 열 수 있습니다.

    이진 모드로 파일을 열 수 있습니다.

    import hashlib
    
    with open(hash_file) as file:
        control_hash = file.readline().rstrip("\n")
    
    wordlistfile = open(wordlist, "rb")
    # ...
    for line in wordlistfile:
        if hashlib.md5(line.rstrip(b'\n\r')).hexdigest() == control_hash:
           # collision
    
  7. ==============================

    7.이 프로그램은 해시 된 암호 목록을 포함하는 파일을 읽고 영어 사전 단어 목록의 해쉬 된 단어와 대조하여 위 MD5 크래커의 버그가없고 향상된 버전입니다. 희망이 도움이됩니다.

    이 프로그램은 해시 된 암호 목록을 포함하는 파일을 읽고 영어 사전 단어 목록의 해쉬 된 단어와 대조하여 위 MD5 크래커의 버그가없고 향상된 버전입니다. 희망이 도움이됩니다.

    다음 링크에서 영어 사전을 다운로드했습니다. https://github.com/dwyl/english-words

    # md5cracker.py
    # English Dictionary https://github.com/dwyl/english-words 
    
    import hashlib, sys
    
    hash_file = 'exercise\hashed.txt'
    wordlist = 'data_sets\english_dictionary\words.txt'
    
    try:
        hashdocument = open(hash_file,'r')
    except IOError:
        print('Invalid file.')
        sys.exit()
    else:
        count = 0
        for hash in hashdocument:
            hash = hash.rstrip('\n')
            print(hash)
            i = 0
            with open(wordlist,'r') as wordlistfile:
                for word in wordlistfile:
                    m = hashlib.md5()
                    word = word.rstrip('\n')            
                    m.update(word.encode('utf-8'))
                    word_hash = m.hexdigest()
                    if word_hash==hash:
                        print('The word, hash combination is ' + word + ',' + hash)
                        count += 1
                        break
                    i += 1
            print('Itiration is ' + str(i))
        if count == 0:
            print('The hash given does not correspond to any supplied word in the wordlist.')
        else:
            print('Total passwords identified is: ' + str(count))
    sys.exit()
    
  8. from https://stackoverflow.com/questions/7585307/how-to-correct-typeerror-unicode-objects-must-be-encoded-before-hashing by cc-by-sa and MIT license