encrypting lines from file with md5 module doesn't work?
Hi, I need some help with my script. I hope someone can show me the right direction or at least explain to me what did I wrong? I write a small script that read lines from plain text file and encrypt each lines using md5 module. I have a small word list that contain 2000+ words, 1 word/line. Using the code below, I can save the output to another file to use it with john the ripper (http://www.openwall.com). Here is the part that annoys me, john recognize the output as des and not as md5. Using the original wordlist, I tried to crack the list but nothing has come up after waiting for almost 9 hours. Can someone please tell me what did I wrong? Why john don't recognize the output as md5? I'm using python 2.5.1 and I'm python noob and also don't have any knowledge about encryption. code import sys, md5 f = open(sys.argv[1]) obj = md5.new() for line in f: if line[-1:] == '\n': text = line[:-1] obj.update(text), print text + ':' + obj.hexdigest() f.close() ---result--- 00:670b14728ad9902aecba32e22fa4f6bd :c47532bbb1e2883c902071591ae1ec9b 11:bf874003f752e86e6d6ba6d6df1f24a2 :65a89de3000110bf37bcafdbd33df55a 121212:38a8eeb4dfb0f86aefea908365817c15 123123:f226a65a908909b83aed92661897d0c9 Thanks in advance -- http://mail.python.org/mailman/listinfo/python-list
Re: encrypting lines from file with md5 module doesn't work?
MRAB schreef: > Canned wrote: >> Hi, >> I need some help with my script. I hope someone can show me the right >> direction or at least explain to me what did I wrong? >> >> I write a small script that read lines from plain text file and encrypt >> each lines using md5 module. I have a small word list that contain 2000+ >> words, 1 word/line. Using the code below, I can save the output to >> another file to use it with john the ripper (http://www.openwall.com). >> > [snip] > MD5 is a type of checksum, not a method of encryption. I found this from google: http://tinyurl.com/4ow2q4 Using that, I changed my script and it seems that it works well. John recognize it as md5. It also raise another question. John can crack my password from /etc/shadow in less than a second, but if I write it down in a file and using the code below to encrypt the password, john would took really long time to crack the password. I've tried several different password using passwd command and my code to generate md5 hash, but it seems that john really having trouble with hashes generated with my code as the hashes generated with passwd being cracked in a matter of second. Is there something wrong with the code? Am I missing something? -code- #!/usr/bin/env python import sys import md5encrypt # from http://tinyurl.com/4ow2q4 f = open(sys.argv[1]) for line in f: if line[-1:] == '\n': text = line[:-1] print text + ':' + md5encrypt.md5crypt(text, md5encrypt.generate_salt(5)) f.close() ---password.txt 00 11 121212 123123 -result.txt- 00:$1$http://mail.python.org/mailman/listinfo/python-list
Re: encrypting lines from file with md5 module doesn't work?
Nick Craig-Wood schreef: > Canned wrote: >> I write a small script that read lines from plain text file and encrypt >> each lines using md5 module. I have a small word list that contain 2000+ >> words, 1 word/line. Using the code below, I can save the output to >> another file to use it with john the ripper (http://www.openwall.com). >> >> Here is the part that annoys me, john recognize the output as des and >> not as md5. Using the original wordlist, I tried to crack the list but >> nothing has come up after waiting for almost 9 hours. Can someone please >> tell me what did I wrong? Why john don't recognize the output as md5? >> I'm using python 2.5.1 and I'm python noob and also don't have any >> knowledge about encryption. >> >> import sys, md5 >> >> f = open(sys.argv[1]) >> obj = md5.new() >> >> for line in f: >> if line[-1:] == '\n': >> text = line[:-1] >> obj.update(text), >> print text + ':' + obj.hexdigest() >> >> f.close() >> >> >> 00:670b14728ad9902aecba32e22fa4f6bd >> :c47532bbb1e2883c902071591ae1ec9b >> 11:bf874003f752e86e6d6ba6d6df1f24a2 >> :65a89de3000110bf37bcafdbd33df55a >> 121212:38a8eeb4dfb0f86aefea908365817c15 >> 123123:f226a65a908909b83aed92661897d0c9 > > john cracks password files if I remember rightly. > > md5 encoded password files don't look like that, they look like this > > guest:$1$3nvOlOaw$vRWaitT8Ne4sMjf9NOrVZ.:13071:0:9:7::: > > (not a real password line!) > > You need to work out how to write that format. > Yes, I'm aware of that format and john only need the first 2 fields to work with, and the rest, AFAIK is ignored. So the format from my example should be: 00:$1$670b14728ad9902aecba32e22fa4f6bd > From memory: the "$1" bit means it is an md5 hash, the next > "$3nvOlOaw$" is the salt and the final "$vRWaitT8Ne4sMjf9NOrVZ." is > the md5 hash in some encoded format or other! Some googling should > reveal the correct algorithm! > Googling around, I found a piece of code that could do that. http://tinyurl.com/4ow2q4. Please read my another post about that. I'm not python expert and I have a little knowledge about encryption. -- http://mail.python.org/mailman/listinfo/python-list
translating ascii to binary
Hi,
I'm trying to write a class that can convert ascii to binary and vice
versa. I write my class based on this function I've found on internet
> def ascii_to_bin(char):
> ascii = ord(char)
> bin = []
>
> while (ascii > 0):
> if (ascii & 1) == 1:
> bin.append("1")
> else:
> bin.append("0")
> ascii = ascii >> 1
>
> bin.reverse()
> binary = "".join(bin)
> zerofix = (8 - len(binary)) * '0'
>
> return zerofix + binary
>
>
>
> some_string = 'Time to go now, Rummy?'
>
> binary = []
> for char in some_string:
> binary.append(ascii_to_bin(char))
>
> print binary
> print " ".join(binary)
That works perfectly, but when I try to implement it in my own class it
gives me alot of headache, also because I'm totally new to the language.
It work only with one character at a time, and if I give a string it
just give some weird result.
> if len(sys.argv) < 2:
> print 'usage:', os.path.basename(sys.argv[0]), 'text'
> sys.exit()
>
> class Converterab:
> '''
> Ascii-binary converter.
> '''
> def __init__(self, string):
> self.string = string
>
> def ascii_to_bin(self):
> bindump = []
> for char in self.string:
> bin = ord(char)
> while bin > 0:
> if (bin & 1) == 1:
> bindump.append("1")
> else:
> bindump.append("0")
> bin = bin >> 1
> bindump.reverse()
> print bindump # Debug tool, delete this
>
> '''
> Zero fix in bindump
> '''
> bindump.insert(0, "0")
> count = 0
> pos = 0
> for dg in bindump:
> count += 1
> pos += 1
> if count == 8:
> bindump.insert(pos, "0")
> count = 0
> bindump.pop()
> print bindump # Debug tool, delete this, the best result so
> far
>
> '''
> Reversing array per byte
> '''
> final = []
> pos -= pos # Set pos to 0 again
> while len(bindump) != 0:
> print count # Debug tool, delete this, this is
> weird, start at 1, I expected 0
> count += 1
> if count > 8:
> pos += 8
> count -= count
> final.insert(pos, bindump.pop())
> print final # Debug tool, delete this
> '''
> for ar in bindump:
> count += 1
> if (count < 8):
> final.insert(pos, bindump.pop())
> elif (count >= 8):
> pos = count
> final.insert(pos, bindump.pop())
> else:
> final.insert(pos, bindump.pop())
> '''
> final.insert(0, final.pop())
>
> binary = "".join(final)
> return binary
>
> result = Converterab(sys.argv[1])
>
> print "Char : ", result.ascii_to_bin()
The problem start at "Reversing array per byte". That block should
reversing the array from 'bindump' and copy it to 'final' per 8 items,
e.g. a = ['0', '1', '0', '1', '0', '1', '0', '1', '2', '1', '2', '1',
'2', '1', '2', '1', '3', '2', '3', '2', '3', '2', '3', '2']
b = ['3', '2', '3', '2', '3', '2', '3', '2', '2', '1', '2', '1', '2',
'1', '2', '1', '0', '1', '0', '1', '0', '1', '0', '1']
Any advice about this matter would be very appreciated.
Thanks in advance.
C
--
http://mail.python.org/mailman/listinfo/python-list
Re: translating ascii to binary
Steven D'Aprano schreef: > Your "ascii_to_bin" method tries to do too much in one method. You should > split the functionality into small, self-contained pieces, then combine > them. And frankly, once you got to the part where you started popping and > inserting, my brain melted. You are making an easy job too hard! *smiles* > It's a bad habit, I can't help it. From now on, I'll follow your advice to split the functionality into small, self-contained pieces. That popping and inserting is just a workaround for another workaround. > Try this instead: > > class Converterab: > ''' > Ascii-binary converter. > ''' > def __init__(self, string): > self.string = string > def bin(self, n): > """Return the binary representation of a positive integer n.""" > bindump = [] > while n > 0: > bindump.append(str(n & 1)) > n = n >> 1 > bindump.reverse() > if bindump: > return ''.join(bindump) > else: > return '0' > def char_to_bin(self, c): > """Return the binary representation of a character c.""" > bits = self.bin(ord(c)) > zeroes = "0" * (8-len(bits)) > return zeroes+bits > def ascii_to_bin(self): > results = [] > for c in self.string: > results.append(self.char_to_bin(c)) > return ''.join(results) > I've spend 3 days to find out what did I do wrong, but you did it in just half an hour/more. You're my hero. -- http://mail.python.org/mailman/listinfo/python-list
