Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with Python Best Practices

    I am new to Python and I am hoping some veterans of the language could give me some pointers on my first program.

    My goal was to make a program like XORSearch; that is, it will open a binary file and find a given string that has been XORed with any 1 byte value 0-255.

    Basically the idea is get a word (in this case, MESSAGE) and if each letter in that word XORed with the first len('MESSAGE') values of the file data results in the same number then we have our XOR key. If any are different, then it's no match and we shift the word over one and try again - repeating till the end of file.

    Here is the code:

    Code:
    file = open('file', 'rb')
    data = file.read()
    # Plaintext string to search for
    string = 'MESSAGE'
    # Loop to iterate through file data
    for x in range(0, len(data)):
        # Stop before end of file
        if x+len(string) > len(data):
            break
        # XOR every letter in our string with 
        # the current data from the file
        xored = []
        for y in range(len(string)):
            # I don't care about XORing the zeros
            # so use -- to skip them in output
            if ord(data[x+y]) == 0:
                xored.append('--')
            else:
                # Output the result into a new list
                xored.append(('%02X' % (ord(data[x+y]) ^ ord(string[y]))))
     
        match = True
        for z in range(len(xored) - 1):
            # If one or more of the values differs 
            # from the other, it was not the same 
            # XOR key and there is no match
            if xored[z] == '--' or (xored[z] != xored[z+1]):
                match = False
     
        if match:
            print 'Match found at offset 0x%02X with key: 0x%s' % (x, xored[0])
    print 'Done'
    I'm trying to code things in Python "the Python way" but while I was coding this I found myself stuck doing things a way that seemed very much like the I would code it in other languages like C/C++, C#, Java or PHP. The parts of the code that concern me most are the for loops; I feel like for x in range(0, len(data)): is just forcing Python to do a C style forloop (for(int x=0; x<len(data); x++))

    I'd really like to use Python for making programs quickly and easily readable and during my reading I came across a few new ideas (like comprehensions and slicing) that I thought might apply in this script but couldn't get them to apply.

    If anyone could glance over my code and give me some tips on how to better do it the Python way, it'd be appreciated. Is there an easier way to iterate over a sequence like a string like I am trying to do?

    Thanks in advance.

  • #2
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    I don't know which is better, but here's another way of looking at it:

    Code:
    # Open and read the data.
    data = open("testfile.txt").read()
    
    # Plaintext string to search for.
    string = "MESSAGE"
    
    x = 0
    # While loop to iterate through file data using while instead of for; gets
    # rid of the break condition for when the iteration gets to the end of data.
    while (x + len(string)) < len(data):
        
        # List comprehension for generating the xored list. I heard it's faster than
        # the typical for loop.
        xored = ["%02X" % (ord(data[x+y]) ^ ord(string[y])) for y in range(len(string))]
        
        # List comprehension to evaluate the contents of xored. Basically, tries to
        # generate a list of dissimilar items from xored. If it fails (all items are the same),
        # acts as the "matched = True" condition of the original script.
        if not [z for z in xored if z != xored[0]]:
            print "Match found at offset 0x%02X with key: 0x%s" % (x, xored[0])
        
        # Increment x.
        x += 1
    
    print "Done"
    Disclaimer: I'm also a n00b to Python.
    Last edited by Samhain13; 05-05-2010 at 08:51 PM.
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **

  • Users who have thanked Samhain13 for this post:

    paulq (05-12-2010)

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks a lot! Functionally the same but in about 1/2 the lines of code. This is EXACTLY what I was looking for.

    Thanks again.

  • #4
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Location
    Pilipinas
    Posts
    169
    Thanks
    4
    Thanked 18 Times in 18 Posts
    No problem.
    I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
    ** Independent Multimedia Artist in Pasig **


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •