Bottleneck: easy obscurity "encryption" via xor

Tino Lange tl_news at nexgo.de
Wed Jul 30 00:03:06 CEST 2003


Hi!

I identified a bottleneck in my programs.

I just want to "encrypt" data by easy xoring. Ok - that's no
encryption at all -  I know. But it's hardly readable - and that's
enough :-) Just some quick obscurity.

It turns out not to be quick at all. I really didn't expect this to be
a bottleneck, but it takes quite some time.

Here's the code:

>$ cat python/EasyCrypt.py
>#! /usr/bin/env python
>import operator
>def xorcrypt(str, salt = 255):
>    if salt > 255:
>        raise "Invalid salt! Must be < 255!"
>    return reduce(lambda x,y: operator.add(x, chr(y)), map(lambda char, _salt = salt: operator.xor(ord(char), _salt), str), "")

xor'ing medium sized-files takes long time. For example a 360
kByte-File takes:

>$ time ./just_crypt.py Userdatan/ScanImage01.jpg > bert
>real    1m52.138s
>user    0m40.320s
>sys     1m6.030s

on my 2.66 GHz P4 machine!

Hmmm, do you have some better implementation ideas? Some optimizing
tricks? (Besides coding in C to avoid immutable string problems)
I already took the operator module to speed up a bit - but it seems
that's not enough...

Thanks

Tino
 




More information about the Python-list mailing list