Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a binary encoding of a string in python?

I am trying to build an md5 cracker for practice. Before I go any further here is my code:

def offline_wordlist_attack(list_path):
      with fileinput.input(files=(list_path)) as wordlist:
          for word in wordlist:
              md5_hash_object = hashlib.md5() # constructing an md5 hash object
              md5_hash_object.update(binascii.a2b_uu(word))
              word_digest = md5_hash_object.digest() # performing the md5 digestion of the word   
              print(word_digest) # Debug

My issue is with md5_hash_object.update(binascii.a2b_uu(word)). The hashlib Python 3 documentation states that the string passed to update() should be in binary representation. The documentation uses m.update(b"Nobody inspects") as an example. In my code, I can not simply attach b in front of the variable word. So I tried to use the binascii library, but that library too, has a note in the documentation stating:

Note

Encoding and decoding functions do not accept Unicode strings. Only bytestring and bytearray objects can be processed.

Could somebody help me out with this? It is getting the better of me.

like image 758
NlightNFotis Avatar asked Aug 29 '12 10:08

NlightNFotis


People also ask

How do I encode a binary string in Python?

Method #1 : Using join() + ord() + format() The ord function converts the character to it's ASCII equivalent, format converts this to binary number and join is used to join each converted character to form a string.

How do you convert data to binary in Python?

In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.

How do you encode strings in Python?

Python String encode() MethodThe encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.


2 Answers

You need to pass in a bytes object, rather than a str. The typical way to go from str (a unicode string in Python 3) to bytes is to use the .encode() method on the string and specify the encoding you wish to use.

my_bytes = my_string.encode('utf-8')
like image 64
Amber Avatar answered Oct 22 '22 00:10

Amber


Just call fileinput.input(...,mode='rb') to open files in binary mode. Such files produce binary strings instead of Unicode strings as files opened in text mode do.

It allows you to skip an unnecessary (implicit) decoding of bytes read from disk followed by immediate encoding them back to bytes using .encode() before passing them to md5().

like image 37
jfs Avatar answered Oct 22 '22 00:10

jfs