Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  2.  
  3. def main():
  4. myMessage = """When you try your best, but you don't succeed
  5. When you get what you want, but not what you need
  6. When you feel so tired, but you can't sleep
  7. Stuck in reverse
  8. And the tears come streaming down your face
  9. When you lose something you can't replace
  10. When you love someone, but it goes to waste
  11. Could it be worse?
  12. Lights will guide you home
  13. And ignite your bones
  14. I will try to fix you
  15. High up above or down below
  16. When you're too in love to let it go
  17. But if you never try you'll never know
  18. Just what you're worth
  19. Lights will guide you home
  20. And ignite your bones
  21. I will try to fix you
  22. Tears stream down your face
  23. When you lose something you cannot replace
  24. Tears stream down your face and I
  25. Tears stream down your face
  26. I promise you I will learn from my mistakes
  27. Tears stream down your face and I
  28. Lights will guide you home
  29. And ignite your bones
  30. And I will try to fix you"""
  31.  
  32.  
  33.  
  34. myKey = 'ASIMOV'
  35. myMode = 'encrypt'
  36.  
  37. def encryptMessage(key, message):
  38. return translateMessage(key, message, 'encrypt')
  39.  
  40.  
  41. def decryptMessage(key, message):
  42. return translateMessage(key, message, 'decrypt')
  43.  
  44.  
  45. def translateMessage(key, message, mode):
  46. translated = []
  47.  
  48. if myMode == 'encrypt':
  49. translated = encryptMessage(myKey, myMessage)
  50. elif myMode == 'decrypt':
  51. translated = decryptMessage(myKey, myMessage)
  52.  
  53. print('%sed message:' % (myMode.title()))
  54. print(translated)
  55.  
  56. keyIndex = 0
  57. key = key.upper()
  58.  
  59. for symbol in message:
  60. num = LETTERS.find(symbol.upper())
  61. if num != -1:
  62. if mode == 'encrypt':
  63. num += LETTERS.find(key[keyIndex])
  64. elif mode == 'decrypt':
  65. num -= LETTERS.find(key[keyIndex])
  66.  
  67. num %= len(LETTERS)
  68.  
  69. if symbol.isupper():
  70. translated.append(LETTERS[num])
  71. elif symbol.islower():
  72. translated.append(LETTERS[num].lower())
  73.  
  74. keyIndex += 1
  75. if keyIndex == len(key):
  76. keyIndex = 0
  77. else:
  78. translated.append(symbol)
  79.  
  80. return ''.join(translated)
  81.  
  82.  
  83. if __name__ == '__main__':
  84. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement