944,117 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 609
  • Python RSS
Nov 1st, 2009
0

Ceasar Cipher

Expand Post »
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.




Python Syntax (Toggle Plain Text)
  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; Nov 2nd, 2009 at 11:06 am. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SMIFMD is offline Offline
8 posts
since Nov 2009
Nov 2nd, 2009
0
Re: Ceasar Cipher
Some corrections to get you started, provided the indentation is correct.
Python Syntax (Toggle Plain Text)
  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; Nov 2nd, 2009 at 12:16 am.
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Nov 3rd, 2009
0
Re: Ceasar Cipher
Here's what I came up with

Python Syntax (Toggle Plain Text)
  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; Nov 3rd, 2009 at 2:40 pm.
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: File size is increased after pickle
Next Thread in Python Forum Timeline: Py_INCREF / Py_DECREF





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC