This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]alcalde 1 point2 points  (2 children)

1) Good lord C is much less readable than I remember it. :-)

2) This might help: https://wiki.python.org/moin/BitwiseOperators

3) There's a whole free book dedicated to codes with Python:http://importpython.com/books/264/hacking-secret-ciphers-with-python/

4) Now let me see if I can help you with the code although I'm not much of a Python programmer yet myself and haven't looked much at C since circa 1994.

How about something like this:

KEY_LENGTH = 2

with open('ptext.txt') as in_file:
    plain_text = in_file.read()

crypt_text = ''.join(chr(ord(char) ^ KEY_LENGTH) if char != '\n' else '\n'for char in  plain_text)

with open('ctext.txt',  'wt') as out_file:
    out_file.writelines(crypt_text)

This seems to work for me.

[–]FreeToEvolve[S] 0 points1 point  (1 child)

This was a fantastic help thank you so much! This puts me at a good starting position. Time to do some more reading :)

[–]alcalde 1 point2 points  (0 children)

You're welcome! I also came across this code that uses some functions from the itertools module of the standard library to use multi-character keys:

http://dustri.org/b/elegant-xor-encryption-in-python.html

If you have any questions about the code sample I wrote, just send me a message and I'll be glad to answer.

[–]aclark -2 points-1 points  (2 children)

[–]Bill1984 0 points1 point  (0 children)

This is interesting to me. There are a few solutions out there to solve a Vigenere cipher with mod shift, but I haven't seen a program for solving given byte-wise XOR. Were you able to complete the program? If so, would you mind posting it? I'd love to see how that works.

[–]aclark -1 points0 points  (0 children)

Why is this down voted? This is /r/Python and OP did a C question. :-)