Can anybody help me out on creating the ceasar cipher. I am attempting to create the program with several functions, including a function for a password. I think that it adds a few more "bells and whistles" by adding this feature. When I run the script below, what is being printed is the location of memory that it is being kept in.

Although I have yet to finish writing this script, i.e. for the decoding function, I can't seem correctly encrypt the string and have yet to get that far.

Any assistance would be greatly appreciated.

######CEASAR CIPHER##########

from string import *



def message():
    print "Type in a message to encrypt:"
    return raw_input()

def key():
    print "What numerical value should the text be encrypted?"
    return int(raw_input())

def password():
    print "Please enter a password to protect this message:"
    return raw_input()
    print "Your message is now password protected."

def encrypted_message():
    char = message
    encrypted_message = ""
    for char in encrypted_message:
        x = ord(char)
        if char.isalpha():
            x = x + key
            offset = 65
        if char.islower():
            offset = 97
        while x < offset:
            x += 26
        while x > offset+25:
            x -= 26
            encrypted_message += chr(x+key)
        print encrypted_message
    
def user_key():
    user_key = raw_input("Please enter the password: \t")
    if user_key==password:
            print message
    while user_key != password:
            print "The password is incorrect."
            user_key = raw_input("Please enter the password: \t")
            if user_key == password:
                print message

def decoder():
    user_key()
    if user_key == password:
        print message    








    
def main():
    print 5*"\n"
    repeat = "y"
    while repeat == "y" or repeat == "Y":
        print 5*"\n"
        message()
        key()
        password ()
        encrypted_message()
        
        print encrypted_message

        
        
        repeat = raw_input("Would you like to repeat the program?  Press <y or n>")

    ans = raw_input("Press <Enter> to QUIT. ")
    
main()

Some corrections to get you started, provided the indentation is correct.

def encrypted_message():
    ##  "message" has not been defined in this function
    char = message
    encrypted_message = ""

    ## encrypted_message = "" from the previous statement
    for char in encrypted_message:
        x = ord(char)
        if char.isalpha():

            ## "key" has not been declared yet
            x = x + key
            offset = 65
            if char.islower():
                offset = 97

        ##   if char is not alpha, then offset has not been defined
        while x < offset:
            x += 26
        while x > offset+25:
            x -= 26

        ##  key is being added to x twice, once in statement #11 and once here
        encrypted_message += chr(x+key)
    print encrypted_message
Member Avatar for masterofpuppets

Here's what I came up with :)

def encode( text ):
    shiftParameter = 3
    encoded = ""
    lettersL = "abcdefghijklmnopqrstuvwxyz"
    lettersU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for char in text:
        if char.isalpha() == False:
            encoded += char
        for i in range( len( lettersL ) ):
            if char == lettersL[ i ]:
                try:
                    encoded += lettersL[ i + shiftParameter ]
                except:
                    encoded += lettersL[ ( i + shiftParameter ) - 26 ]
                break
            elif char == lettersU[ i ]:
                try:
                    encoded += lettersU[ i + shiftParameter ]
                except:
                    encoded += lettersU[ ( i + shiftParameter ) - 26 ]
                break
            
    return encoded

print encode( "this, Is, a Test and xzy" )

>>> 
wklv, Lv, d Whvw dqg acb
>>>

It might not be the best way of doing it but it works :) hope this helps

P.S I just read about the cipher in wikipedia so if I am missing something pls post :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.