วิธีการแก้ไข TypeError: Unicode-objects ต้องได้รับการเข้ารหัสก่อนการแฮช


295

ฉันมีข้อผิดพลาดนี้:

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

เมื่อฉันพยายามรันโค้ดนี้ใน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()

ฉันพบว่าการเปิดไฟล์ด้วย 'rb' ช่วยกรณีของฉัน
— dlamblin

คำตอบ:


299

wordlistfileมันอาจจะมองหาการเข้ารหัสอักขระจาก

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

หรือถ้าคุณกำลังทำงานแบบทีละบรรทัด:

line.encode('utf-8')

3
open(wordlist,"r",encoding='utf-8')เหตุใดจึงใช้เปิดด้วยการเข้ารหัสเฉพาะการเข้ารหัสจะระบุตัวแปลงสัญญาณถอดรหัสโดยไม่มีตัวเลือกนี้มันใช้การเข้ารหัสขึ้นอยู่กับแพลตฟอร์ม
— Tanky Woo

129

คุณต้องกำหนดencoding formatเช่นutf-8ลองวิธีนี้ง่ายๆ

ตัวอย่างนี้สร้างตัวเลขสุ่มโดยใช้อัลกอริทึม SHA256:

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

18

ในการจัดเก็บรหัสผ่าน (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()

1
บรรทัดนี้ทำให้ไม่สามารถใช้รหัสผ่านได้ password_salt = os.urandom (32) .hex () ควรเป็นค่าคงที่ที่ทราบ แต่สามารถเป็นความลับสำหรับเซิร์ฟเวอร์เท่านั้น โปรดแก้ไขฉันหรือปรับให้เข้ากับรหัสของคุณ
— Yash

1
ฉันเห็นด้วยกับ @Yash คุณมีเกลือเดียวที่คุณใช้สำหรับทุกแฮช (ไม่ดีที่สุด) หรือถ้าคุณสร้างเกลือสุ่มสำหรับแต่ละแฮชคุณต้องเก็บมันไว้กับแฮชที่จะใช้อีกครั้งในภายหลังเพื่อการเปรียบเทียบ
— Carson Evans

15

ข้อผิดพลาดได้แจ้งสิ่งที่คุณต้องทำแล้ว MD5 ทำงานบนไบต์ดังนั้นคุณต้องเข้ารหัสสตริง Unicode เข้าไปเช่นกับbytesline.encode('utf-8')


11

กรุณาดูเป็นครั้งแรกที่ว่าคำตอบ

ตอนนี้เกิดข้อผิดพลาดมีความชัดเจน: คุณสามารถใช้ไบต์ไม่สตริงหลาม (สิ่งที่เคยเป็นunicodeในหลาม <3) เพื่อให้คุณมีการเข้ารหัสสตริงกับการเข้ารหัสที่คุณต้องการ: utf-32, utf-16, utf-8หรือแม้กระทั่งหนึ่งใน 8- จำกัด การเข้ารหัสบิต (สิ่งที่บางคนอาจเรียกเพจรหัส)

ไบต์ในไฟล์ wordlist ของคุณจะถูกถอดรหัสเป็น Unicode โดย Python 3 โดยอัตโนมัติเมื่อคุณอ่านจากไฟล์ ฉันขอแนะนำให้คุณทำ:

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

ดังนั้นข้อมูลที่เข้ารหัสจะถูกส่งไปยังอัลกอริทึม md5 จะถูกเข้ารหัสเหมือนกับไฟล์ที่อยู่ข้างใต้



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


0

ถ้าเป็นสตริงเส้นเดียว ล้อมด้วย b หรือ B เช่น:

variable = b"This is a variable"

หรือ

variable2 = B"This is also a variable"

-3

โปรแกรมนี้เป็นโปรแกรมแก้ไขข้อผิดพลาดของ 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()
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.