Bottleneck: easy obscurity "encryption" via xor
Tino Lange
tl_news at nexgo.de
Wed Jul 30 06:36:18 EDT 2003
Hi Irmen, Bengt and Paul!
Thanks a lot for your answer.
Indeed it seems that I chose the slowest possible implementation :-)
I tested all your recepies and I would like to give a short summary of what
I found:
> def xorcrypt_old(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), "")
>
> def xorcrypt_irmen1(str, salt = 255):
> if salt > 255:
> raise "Invalid salt! Must be < 255!"
> return ''.join(map(lambda char, _salt = salt:
chr(operator.xor(ord(char), _salt)), str))
>
> def xorcrypt_irmen2(str, salt = 255):
> if salt <0 or salt> 255:
> raise "Invalid salt! Must be 0<=salt<=255!"
> return ''.join( [ chr(ord(c) ^ salt) for c in str ] )
>
> def xorcrypt_bengt(str, salt = 255):
> if salt <0 or salt> 255:
> raise "Invalid salt! Must be 0<=salt<=255!"
> translation_table = ''.join( [ chr(c ^ salt) for c in range(255) ])
> return str.translate(translation_table)
>
> def xorcrypt_paul(str, salt = 255):
> if salt <0 or salt> 255:
> raise "Invalid salt! Must be 0<=salt<=255!"
> n = len(str)
> stream = array.array('B', str)
> for i in xrange(n):
> stream[i] = stream[i] ^ salt
> return stream.tostring()
And it seems that Bengt's reciepe is the fastest. For very small strings
(<255 chars) the method irmen2 should be the best choice - it doesn' have
to pre-create the translation-table and does everything on-the-fly.
Have a look at some results:
File with 380303 Bytes
> Old Version ... 511 seconds -> 744 Bytes/second
> Irmen's 1st Version ... 4 seconds -> 95075 Bytes/second
> Irmen's 2nd Version ... 3 seconds -> 126767 Bytes/second
> Bengt's Version ... 1 seconds -> 380303 Bytes/second
> Paul's Version ... 1 seconds -> 380303 Bytes/second
File with 6587435 Bytes
> Old Version ... not tested
> Irmen's 1st Version ... 71 seconds -> 92780 Bytes/second
> Irmen's 2nd Version ... 39 seconds -> 168908 Bytes/second
> Bengt's Version ... 1 seconds -> 6587435 Bytes/second
> Paul's Version ... 16 seconds -> 411714 Bytes/second
Once again - thanks for your help!
Cheers,
Tino
More information about the Python-list
mailing list