DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Code Snippet: ceaser cypher (http://www.daniweb.com/forums/thread216970.html)

avgprogramerjoe Aug 16th, 2007 2:25 am
ceaser cypher
 
This code takes a string a cyphers it by shifting it's position in the alphabet. I've gotten it so it can pretty much take paragraphs. It's quite neat to play with.
For example the above when shifted to 5, would be:
Ymnx htij yfpjx f xywnsl f hDumjwx ny gD xmnkynsl ny,x utxnynts ns ymj fqumfgjy= N,Aj ltyyjs ny xt ny hfs uwjyyD rzhm yfpj ufwflwfumx= Ny,x vznyj sjfy yt uqfD Bnym=

  1. import string
  2. numbers = ""
  3. x = 0
  4. while x < 11:
  5. numbers = numbers + str(x)
  6. x += 1
  7. shift = int(raw_input("Pick shift key from 1-9"))
  8. choice = raw_input("would you like to encode or decode?")
  9. letters = string.ascii_letters + string.punctuation + numbers
  10. word = (raw_input("Please enter text"))
  11. print letters
  12. encoded = ''
  13. if choice == "encode":
  14. for letter in word:
  15. if letter == ' ':
  16. encoded = encoded + ' '
  17. else:
  18. x = letters.index(letter) + shift
  19. encoded = encoded + letters[x]
  20. if choice == "decode":
  21. for letter in word:
  22. if letter == ' ':
  23. encoded = encoded + ' '
  24. else:
  25. x = letters.index(letter) - shift
  26. encoded = encoded + letters[x]
  27. print encoded
vegaseat Aug 16th, 2007 2:13 pm
Removed [ code][ inlinecode] ... [ /inlinecode][ /code] tags.

Note, you could use
letters = string.ascii_letters + string.punctuation + string.digits

ShawnCplus Aug 19th, 2007 11:38 pm
A caeser cypher with a shift of 13 is known as ROT13 just incase you didn't know that and it's the easiest cypher I know of to code besides an XOR

docsharp01 Jul 1st, 2008 3:14 pm
This coding looks very similar to shell scripting for Unix. Interesting.

http://www.1-satellite-tv-facts.com
http://www.1-satellite-tv-facts.com/Direct-TV.html
http://www.1-satellite-tv-facts.com/Dish-Network.html
http://www.1-satellite-tv-facts.com/...ite-Radio.html
http://www.1-satellite-tv-facts.com/...t-Service.html
http://www.1-satellite-tv-facts.com/Satellite-DSL.html
http://www.1-satellite-tv-facts.com/...-Internet.html
http://www.1-satellite-tv-facts.com/VoIP.html
http://www.1-satellite-tv-facts.com/Phone-Systems.html
http://www.1-satellite-tv-facts.com/...-Programs.html


All times are GMT -4. The time now is 4:15 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC