463 Stimmen

Wie man TypeError korrigiert: Unicode-Objekte müssen vor dem Hashing kodiert werden?

Ich habe diesen Fehler:

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

wenn ich versuche, diesen Code auszuführen in Python 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:
  # 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)
  m = hashlib.md5()
  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()

16voto

Mike Cash Punkte 297

Die Kodierung dieser Zeile hat das Problem für mich gelöst.

m.update(line.encode('utf-8'))

15voto

tzot Punkte 86792

Werfen Sie bitte zunächst einen Blick auf que Antwort.

Jetzt ist die Fehlermeldung eindeutig: Sie können nur Bytes verwenden, keine Python-Strings (was früher als unicode in Python < 3), daher müssen Sie die Zeichenketten mit Ihrer bevorzugten Kodierung kodieren: utf-32 , utf-16 , utf-8 oder sogar eine der eingeschränkten 8-Bit-Kodierungen (die man als Codepages bezeichnen könnte).

Die Bytes in Ihrer Wortlistendatei werden von Python 3 automatisch in Unicode dekodiert, während Sie aus der Datei lesen. Ich schlage vor, Sie tun:

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

so dass die verschlüsselten Daten, die an den md5-Algorithmus übergeben werden, genau wie die zugrunde liegende Datei verschlüsselt sind.

13voto

jfs Punkte 370717

Sie können die Datei im Binärmodus öffnen:

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

7voto

SBimochan Punkte 345

Wenn es sich um eine einzeilige Zeichenkette handelt, wird sie mit b oder B umbrochen, z. B:

variable = b"This is a variable"

o

variable2 = B"This is also a variable"

-4voto

udz Punkte 19

Dieses Programm ist die fehlerfreie und erweiterte Version des oben genannten MD5-Crackers, der die Datei mit der Liste der gehashten Passwörter liest und sie mit dem gehashten Wort aus der Wortliste des englischen Wörterbuchs vergleicht. Hoffentlich ist es hilfreich.

Ich habe das englische Wörterbuch über den folgenden Link heruntergeladen 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()

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X