File tree
Expand file treeCollapse file tree1 file changed
+38
-0
lines changed Expand file treeCollapse file tree1 file changed
+38
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +# The Caesar Cipher Algorithm |
| 2 | + |
| 3 | +message = input("Enter message: ") |
| 4 | +key = int(input("Key [1-26]: ")) |
| 5 | +mode = input("Encrypt or Decrypt [e/d]: ") |
| 6 | + |
| 7 | +if mode.lower().startswith('e'): |
| 8 | +mode = "encrypt" |
| 9 | +elif mode.lower().startswith('d'): |
| 10 | +mode = "decrypt" |
| 11 | + |
| 12 | +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 13 | + |
| 14 | +translated = "" |
| 15 | + |
| 16 | +message = message.upper() |
| 17 | + |
| 18 | +for symbol in message: |
| 19 | +if symbol in LETTERS: |
| 20 | +num = LETTERS.find(symbol) |
| 21 | +if mode == "encrypt": |
| 22 | +num = num + key |
| 23 | +elif mode == "decrypt": |
| 24 | +num = num - key |
| 25 | + |
| 26 | +if num >= len(LETTERS): |
| 27 | +num = num - len(LETTERS) |
| 28 | +elif num < 0: |
| 29 | +num = num + len(LETTERS) |
| 30 | + |
| 31 | +translated = translated + LETTERS[num] |
| 32 | +else: |
| 33 | +translated = translated + symbol |
| 34 | + |
| 35 | +if mode == "encrypt": |
| 36 | +print("Encryption:", translated) |
| 37 | +elif mode == "decrypt": |
| 38 | +print("Decryption:", translated) |
You can’t perform that action at this time.
0 commit comments