[Tutor] How do I save the output of the rotor module?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Nov 6 21:48:30 EST 2003

On Thu, 6 Nov 2003, Mike Faire wrote:

> I wrote a simple script to get acquainted with the rotor module.
> Encrypting and decrypting was simple enough. However, once I save the
> encrypted list to a file, it will not decrypt correctly beyond the first
> newline.

Hi Mike,

'rotor' provides no guarantee that

    r.encrypt(a + b)

is the same as

    r.encrypt(a) + r.encrypt(b)

Let me check something...

>>> r = rotor.newrotor("606")
>>> r.encrypt("hello") + r.encrypt("world")
>>> r = rotor.newrotor("606")
>>> r.encrypt("helloworld")

So that's one issue we need to be aware of.

Another is that newlines themselves will be encoded!

>>> r.encrypt("\n\n\n")

These two facts interact in a funny way: since you're doing the encoding,
line by line:

> rt = rotor.newrotor('606')
> for item in contents:
>      encon.append(rt.encrypt(item))
> print encon

is very likely that the encoded file won't have the same number of
newlines as the original input.

This, combined with the knowledge that:

    r.encrypt(a) + r.encrypt(b) != r.encrypt(a + b)

should be enough to see that we're in trouble as soon as we try decoding
the string.  *grin*

Does this make sense so far?

More information about the Tutor mailing list