[Tutor] How do I save the output of the rotor module?
glingl at aon.at
Thu Nov 6 20:38:20 EST 2003
Mike Faire schrieb:
> Hi everyone!
> I wrote a simple script to get acquainted with the rotor module.
> However...it will not decrypt correctly beyond the first newline.
> Here is the code:
> #!/usr/bin/env python
> import rotor
> contents = ["This is a line of text\n", "including newlines\n", "for
> rotor testing."]
> fh = open('text.txt', 'r')
> incoming = fh.readlines() # read from a file
> print incoming
> # after writing the strings to disk and then reading them back in
> # again, print incoming shows a list comprised of one large string
Hi, Mike! I think readlines() cannot decompose the encrypted file into
because those newline-characters also get encrypted.
Maybe the encryption-decryption process doesn't work because
at every call of encrypt, the rotor object is reset to its initial state
In your example this happens three times during encryption but only once
for decryption ... ?
You can avoid this by using the encryptmore method for encrypting (which
doesn't reset the rotor object.
Another way to accomplish correct decryption is using strings:
contents = "This is a line of text\nincluding newlines\nfor rotor testing."
print contents # shows a list of three strings
rt = rotor.newrotor('606')
# at this point, print encon shows a list of three strings, encrypted
fh = open('text.txt', 'w')
fh.write(encon) # write to a file
fh = open('text.txt', 'r')
incoming = fh.read() # read from a file
# after writing the strings to disk and then reading them back in
# again, print incoming shows a list comprised of one large string
after = rt.decrypt(incoming)
# the one large string will decrypt correctly
More information about the Tutor