Ceasar Cipher

Reply

Join Date: Nov 2009
Posts: 4
Reputation: SMIFMD is an unknown quantity at this point 
Solved Threads: 0
SMIFMD SMIFMD is offline Offline
Newbie Poster

Ceasar Cipher

 
0
  #1
27 Days Ago
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.




  1. ######CEASAR CIPHER##########
  2.  
  3. from string import *
  4.  
  5.  
  6.  
  7. def message():
  8. print "Type in a message to encrypt:"
  9. return raw_input()
  10.  
  11. def key():
  12. print "What numerical value should the text be encrypted?"
  13. return int(raw_input())
  14.  
  15. def password():
  16. print "Please enter a password to protect this message:"
  17. return raw_input()
  18. print "Your message is now password protected."
  19.  
  20. def encrypted_message():
  21. char = message
  22. encrypted_message = ""
  23. for char in encrypted_message:
  24. x = ord(char)
  25. if char.isalpha():
  26. x = x + key
  27. offset = 65
  28. if char.islower():
  29. offset = 97
  30. while x < offset:
  31. x += 26
  32. while x > offset+25:
  33. x -= 26
  34. encrypted_message += chr(x+key)
  35. print encrypted_message
  36.  
  37. def user_key():
  38. user_key = raw_input("Please enter the password: \t")
  39. if user_key==password:
  40. print message
  41. while user_key != password:
  42. print "The password is incorrect."
  43. user_key = raw_input("Please enter the password: \t")
  44. if user_key == password:
  45. print message
  46.  
  47. def decoder():
  48. user_key()
  49. if user_key == password:
  50. print message
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. def main():
  61. print 5*"\n"
  62. repeat = "y"
  63. while repeat == "y" or repeat == "Y":
  64. print 5*"\n"
  65. message()
  66. key()
  67. password ()
  68. encrypted_message()
  69.  
  70. print encrypted_message
  71.  
  72.  
  73.  
  74. repeat = raw_input("Would you like to repeat the program? Press <y or n>")
  75.  
  76. ans = raw_input("Press <Enter> to QUIT. ")
  77.  
  78. main()
Last edited by vegaseat; 27 Days Ago at 11:06 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster
 
0
  #2
27 Days Ago
Some corrections to get you started, provided the indentation is correct.
  1. def encrypted_message():
  2. ## "message" has not been defined in this function
  3. char = message
  4. encrypted_message = ""
  5.  
  6. ## encrypted_message = "" from the previous statement
  7. for char in encrypted_message:
  8. x = ord(char)
  9. if char.isalpha():
  10.  
  11. ## "key" has not been declared yet
  12. x = x + key
  13. offset = 65
  14. if char.islower():
  15. offset = 97
  16.  
  17. ## if char is not alpha, then offset has not been defined
  18. while x < offset:
  19. x += 26
  20. while x > offset+25:
  21. x -= 26
  22.  
  23. ## key is being added to x twice, once in statement #11 and once here
  24. encrypted_message += chr(x+key)
  25. print encrypted_message
Last edited by woooee; 27 Days Ago at 12:16 am.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 202
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 47
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #3
26 Days Ago
Here's what I came up with

  1. def encode( text ):
  2. shiftParameter = 3
  3. encoded = ""
  4. lettersL = "abcdefghijklmnopqrstuvwxyz"
  5. lettersU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  6. for char in text:
  7. if char.isalpha() == False:
  8. encoded += char
  9. for i in range( len( lettersL ) ):
  10. if char == lettersL[ i ]:
  11. try:
  12. encoded += lettersL[ i + shiftParameter ]
  13. except:
  14. encoded += lettersL[ ( i + shiftParameter ) - 26 ]
  15. break
  16. elif char == lettersU[ i ]:
  17. try:
  18. encoded += lettersU[ i + shiftParameter ]
  19. except:
  20. encoded += lettersU[ ( i + shiftParameter ) - 26 ]
  21. break
  22.  
  23. return encoded
  24.  
  25. print encode( "this, Is, a Test and xzy" )
  26.  
  27. >>>
  28. wklv, Lv, d Whvw dqg acb
  29. >>>

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
Last edited by masterofpuppets; 26 Days Ago at 2:40 pm.
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC