Cypher Question

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 2
Reputation: BigPappyJappy is an unknown quantity at this point 
Solved Threads: 0
BigPappyJappy BigPappyJappy is offline Offline
Newbie Poster

Cypher Question

 
0
  #1
Mar 12th, 2009
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:

  1. #Cypher.py
  2. #A program that encrypts/decrypts a message
  3.  
  4. import string
  5.  
  6. def main():
  7. print"This program will either encrypt or decrypt a message."
  8.  
  9. mess = raw_input("Please enter your message: ")
  10. meth = raw_input("Enter 0 for encryption, 1 for decryption: ")
  11. while meth >= "2":
  12. print "Please enter 0 or 1 ONLY."
  13. meth = raw_input("Enter 0 for encryption, 1 for decryption: ")
  14. key = raw_input("What key index do you want to use: ")
  15.  
  16. mesCode = []
  17.  
  18. if meth == "0":
  19. for ch in(mess):
  20. ch = chr(ord(ch)+ eval(key))
  21. mesCode.append(ch)
  22. elif meth == "1":
  23. for ch in(mess):
  24. ch = chr(ord(ch) - eval(key))
  25. mesCode.append(ch)
  26.  
  27.  
  28. print string.join(mesCode,"")
  29.  
  30. main()

( 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.
Last edited by BigPappyJappy; Mar 12th, 2009 at 4:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Cypher Question

 
1
  #2
Mar 13th, 2009
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 ...
  1. # example of simple encoding/decoding using Python's 'rot13'
  2. # alphabet is shifted by 13 positions to nopqrstuvwxyzabcdefghijklm
  3. # so original 'a' becomes 'n' or 'A' becomes 'N' and so on
  4. # (non-alpha characters are not affected)
  5.  
  6. text = "Peanuts88-q"
  7. print "original =", text
  8.  
  9. encoded = text.encode('rot13')
  10. print "encoded =", encoded
  11.  
  12. decoded = encoded.encode('rot13')
  13. print "decoded =", decoded
  14.  
  15. """
  16. output -->
  17. original = Peanuts88-q
  18. encoded = Crnahgf88-d
  19. decoded = Peanuts88-q
  20. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: BigPappyJappy is an unknown quantity at this point 
Solved Threads: 0
BigPappyJappy BigPappyJappy is offline Offline
Newbie Poster

Re: Cypher Question

 
0
  #3
Mar 13th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC