| | |
Ceasar Cipher
![]() |
•
•
Join Date: Nov 2009
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
Python Syntax (Toggle Plain Text)
######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()
Last edited by vegaseat; 27 Days Ago at 11:06 am. Reason: added code tags
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
0
#2 27 Days Ago
Some corrections to get you started, provided the indentation is correct.
Python Syntax (Toggle Plain Text)
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
Last edited by woooee; 27 Days Ago at 12:16 am.
Linux counter #99383
0
#3 26 Days Ago
Here's what I came up with 
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

Python Syntax (Toggle Plain Text)
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 helpsP.S I just read about the cipher in wikipedia so if I am missing something pls post
Last edited by masterofpuppets; 26 Days Ago at 2:40 pm.
![]() |
Other Threads in the Python Forum
- Previous Thread: File size is increased after pickle
- Next Thread: Py_INCREF / Py_DECREF
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change command convert count csv cturtle cursor def dictionary digital dynamic dynamically enter event examples external file float format frange function google gui hints homework i/o iframe import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session signal simple skinning sprite string strings syntax terminal text threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape whileloop wxpython






