I could use some help making this Python code run faster using only Python code.

Matt McCredie mccredie at gmail.com
Fri Sep 21 00:57:16 CEST 2007

On 9/20/07, Python Maniac <raychorn at hotmail.com> wrote:
> I am new to Python however I would like some feedback from those who
> know more about Python than I do at this time.

Well, you could save some time by not applying the scramble one line
at a time (that is if you don't mind losing the line endings in the
scrambled version). For that to be effective though, you probably want
to open in binary mode. Also, your scramble can be written using list

def scramble(s, key=0x80):
   return ''.join([chr(ord(c) ^ key) for c in s])

output = scramble(f.read())

If you use xor (^) as above, you can use the same method for scramble
as descramble (running it again with the same key will descramble) and
you can use an arbitrary key. Though, with 255 combinations, it isn't
very strong encryption.

If you want stronger encryption you can use the following AESish algorithm:

import random
def scramble(s, key):
    return ''.join([chr(ord(c) ^ random.randint(0,255)) for c in s])

This allows you to use much larger keys, but with a similar effect.
Still not strong enough to be unbreakable, but much better than the
origional. It is strong enough that someone knowing how you scrambled
it will have trouble unscrambling it even if they don't know the key.


More information about the Python-list mailing list