[Tutor] Masking operation

Wayne srilyk at gmail.com
Wed Oct 14 23:09:36 CEST 2009

I've searched "Python masking" and various other terms in Google and the
python docs and came up with nothing useful.
This is just a bit of a hobby project that was inspired by some assembly
homework that one of my friends is now doing and I did last semester. The
assignment was to create a program that took two strings and XOR'ed the one
by the other.

For example:

mask: secret
data: This is a secret message!

and you would have:
e XOR h
<skip a few>
e XOR i
t XOR s
<starts at the beginning of the mask>
s XOR ' '

So I basically want a mask the same length as my data (in this case, 25), so
I would have:

mask = 'secretsecretsecretsecrets'

The way I'm currently doing it is:
mask = mask * (len(message)/len(mask))

for l, m in zip(message, mask):
    word += chr(ord(l) ^ ord(m))

but I'm wondering if there are any easier/more pythonic ways.


To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091014/1fe0658d/attachment.htm>

More information about the Tutor mailing list