#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2

    New To Python, Help with encryption/bitwise operators


    Okay, so I'm not new to the coding game but the previous language I used to use was DarkBASIC, designed to be easy but not necessarily optimal.

    So, in the interest of testing my encryption algorithm with a more reliable system, I was wondering if someone could outline the basics of this process:

    -Read a file, byte for byte, into a keycode 'c' (Byte) array, and store the name of that array as an accompanying string. Calling the array "wfile".
    -Generate a 'c' array of the given length of the previous file (for sake of example, we can have all the bytes be 255 or whatever.) Calling this "cstream"
    -XOR the wfile by cstream, calling this "outfile"
    creating the file of the array "outfile", byte for byte, and giving it a name based on the original with an altered extension.

    Currently, I've seen lots of methods to do one or the other, but I'm confused about the conversion between the list that is read in from the file and the list that is the byte array, and I have yet to see an outline of bitwise operators.

    I'm looking through Pycrypto to see if I can Frankenstein pieces of it onto this engine, but until that works, is there anyone out there who might have done something similar or who would be willing to write it out?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Encryption and decryption uses the same function
    Code:
    import itertools
    
    def change(text,codekey):
        return ''.join(['%c'%(ord(a)^ord(b)) for (a,b,) in zip(text,itertools.cycle(codekey))])
    
    text='Rock, Paper, and Scissors in Python'
    codekey='blort'
    encryption = change(text,codekey)
    print(encryption)
    decryption = change(encryption,codekey)
    print(decryption)
    assert(text == decryption)
    Last edited by b49P23TIvg; December 16th, 2012 at 11:38 PM. Reason: must be late!
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2
    Yeah, that's fine and all I guess, forgive me if I didn't make my request clear.

    I'm trying to:

    -Learn how to perform bitwise operations
    -Learn how to read bytes into an array (from the array module) from a file
    -Learn how to write byte into a file from an array

    It seems you did the first thing (and yes, I know that the two processes I described were exactly the same) but it still leaves me in the dark as to how to implement it. The system you gave leaves out what is probably the most crucial element; actually applying an encryption algorithm onto a file.

    If I'm using a key of the length specified, do I even need itertools? What does itertools even do? I'm not familiar with the module.

    See, my algorithm process, once I can implement it, generates a key of a length equal to (or greater than) the length of the file. So there shouldn't be a need to cycle, like in the example you've provided, right?

    Does "change" actually do the bitwise calculations with "ord" and whatnot? I'm not familiar with most of Python's syntax.

    I'm sorry if that's too many questions. Thank you for your help, I'll start looking into tutorials now that your code has given me a general direction to work with. Thank you again.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Performing the bitwise operators is easy?
    For example:

    int(a) ^ int(b) # exclusive or

    There are 16 bitwise operations for 2 binary inputs. Implement all of them. An educational exercise. Also implement shift and rotate left, right, with and without sign bit.




    learn to read bytes into an array. Learn to open a file, read the file, and study the array module. Use contextual with statement.

    with open('somefile','somemode') as varname:
    statements




    No, you don't need itertools if you made the key length match the text length. itertools will probably use less memory.




    &|^ >> << work on integers. ord converts a character to an integer. There are other ways.
    Last edited by b49P23TIvg; December 17th, 2012 at 11:47 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2
    Well, my current algorithm is mostly dependent on XOR, but I'm working on implementing some other methods.

    Yes, I know they're easy, but I didn't know the syntax or where to find it. In the language I'm accustomed to, XOR is:
    operandA ~~ operandB
    So you could see now that knowing how to implement them is not the same as knowing what they are.

    I was going to do it byte-for-byte using the array module because it's supposed to be quicker than a list and would be ideal for byte-for-byte calculations, right?

    Okay. I'll go look up a method for the byte reading and writing. Thanks so much for all your help.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2
    So, here's what I got for a test array-

    Code:
    from array import array
    
    def create_sawtooth(a,b,c,d):
        result = array('B')
        for i in range(0,a):
            result.append(85)
        for i in range(0,b):
            result.append(170)
        for i in range(0,c):
            result.append(85)
        for i in range(0,d):
            result.append(170)
        return result
    I was wondering how to optimize it for indefinite arguments. I know there is some way to just have it repeat, but I don't know what it is. Keep in my, switching from 85 every first iteration to 170 every second iteration.
    EDIT: So you know, that isn't my actual algorithm, it's a secondary algorithm I use on top of my general encryption to obfuscate it more.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo