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

Mike Faire res0p1ia at verizon.net
Thu Nov 6 23:48:39 EST 2003


I hit the wrong "reply" button and sent my initial response to Danny,
here it is for the list to see.

On Thursday 06 November 2003 19:04, Danny Yoo wrote:
> On Thu, 6 Nov 2003, Danny Yoo wrote:
> >
> >     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?
>

Wow! of course! I imagined that since writelines() read in a list of strings,
I could process that list of strings, write it to disk and it would come back
as another list of strings. Since the encrypted output of rotor is "binary"
data, all structure is lost. I was completely missing the fact that
 r.encrypt(a) + r.encrypt(b) was happening at all.

>
> But the simpler approach is to use rotor's rotor.encryptmore() and
> rotor.decryptmore()  methods instead:
>
> ###
>
> >>> import rotor
> >>> rt = rotor.newrotor("foo")
> >>> rt.encryptmore("hello") + rt.encryptmore("world")
>
> 'F\x14\xf5\x12\xdb\xec%\xf8\x92\xb6'
>
> >>> rt = rotor.newrotor("foo")
> >>> rt.encryptmore("helloworld")
>
> 'F\x14\xf5\x12\xdb\xec%\xf8\x92\xb6'
> ###
>
>
> encryptmore() and decryptmore() preserve the property that
>
>     rt.encryptmore(a + b)
>
> and
>
>     rt.encryptmore(a) + rt.encryptmore(b)
>
> are equivalent, so if you just use these methods instead, you should get
> good results.  But when decrypting, be sure to reset the rotor to the
> initial state, so that the gears are in the right positions... *grin*
>
>
>
> Hope this helps!
>
>

Very slick! This list has the *stellar* tutors!
Thanks Danny and Gregor (who touched on the similiar points).

The code now decrypts successfully. Although it leaves the file in the
form of one large string instead of a list of strings. The split() method
returns it to a list of strings but *removes* the \n's. I could loop through
the list and append newlines to each string. Can split() be instructed
to perform the split without removing the newlines?

import rotor

contents = ["This is a line of text\n", " including newlines\n", " for rotor 
testing."]
print contents   # shows a list of three strings
encon = []
rt = rotor.newrotor('606')
for item in contents:
     encon.append(rt.encryptmore(item))
print encon

# at this point, print encon shows a list of three strings, encrypted

fh = open('text.txt', 'w')
fh.writelines(encon)             # write to a file
fh.close()
fh = open('text.txt', 'r')
incoming = fh.readlines()     # read from a file
fh.close()
print incoming                    # now the string is identical

rt = rotor.newrotor('606')   # reset the wheels to their starting positions
after = []
for item in incoming:
     after.append(rt.decryptmore(item))
after = after[0].split('\n')  # restore the "list of strings" form of the file
fh = open('text.txt', 'w')
fh.writelines(after)
fh.close()

Mike




More information about the Tutor mailing list