I was wondering if someone could help me with modifying a program. I first must write a program that not only encrypts, but decrypts a message as well. This part I think I have figured out (although it is probabaly a little bloated) here it is:

#A program that encrypts/decrypts a message

import string

def main():
    print"This program will either encrypt or decrypt a message."

    mess = raw_input("Please enter your message: ")
    meth = raw_input("Enter 0 for encryption, 1 for decryption: ")
    while meth >= "2":
        print "Please enter 0 or 1 ONLY."
        meth = raw_input("Enter 0 for encryption, 1 for decryption: ")
    key = raw_input("What key index do you want to use: ")

    mesCode = []

    if meth == "0":
        for ch in(mess):
            ch = chr(ord(ch)+ eval(key))
    elif meth == "1":
        for ch in(mess):
            ch = chr(ord(ch) - eval(key))

    print string.join(mesCode,"")


( I hope I enclosed that right)
Anyway, now I have to modify it so that when we "drop off the end" of the alaphabet, the cypher shift goes in a circular fashion, ie: after z comes a again. I have found other solutions, but think I have gone a little over my head with my if/elif statements, and I can't get them to work properly.

Could someone point me in the right direction as to where to take this next, and also, (I feel dumb asking this), WHY would you want the shift to go in a circular fashion? Thanks for any and all advice

Ps. I am using Python 2.6.1, which came with the book, and have no prior programming experience.

8 Years
Discussion Span
Last Post by BigPappyJappy

You want to go into a circle so you get printable characters. Here is an example of Python's builtin rotation encoder, it starts in the middle of the 26 alpha character sets ...

# example of simple encoding/decoding using Python's 'rot13'
# alphabet is shifted by 13 positions to nopqrstuvwxyzabcdefghijklm
# so original 'a' becomes 'n' or 'A' becomes 'N' and so on 
# (non-alpha characters are not affected)

text = "Peanuts88-q"
print "original =", text

encoded = text.encode('rot13')
print "encoded  =", encoded

decoded = encoded.encode('rot13')
print "decoded  =", decoded

output -->
original = Peanuts88-q
encoded  = Crnahgf88-d
decoded  = Peanuts88-q
Votes + Comments
I didn't know the "rotation encoder" !

Wow, that is ALOT less complicated than other ones that I was trying. Thanks for all your help in getting me in the right direction.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.