|
| 1 | +''' +-+#^#+--+#^#+--+#^#+-+ |
| 2 | + Project: ROT Cipher ░░╚══╗░╔═╔════╝ |
| 3 | + Version: 1.00 ╚═╦═╗╠═╩═╩╗╔═╦═╗ |
| 4 | + Author: Colton Tatum ░░║▒╠╣▒▒▒▒╠╣▒║▒║ |
| 5 | + Email: [email protected] ╔═╩═╝╠═╦═╦╝╚═╩═╝ |
| 6 | + Date: 2/14/22 ░░╔══╝░╚═╚════╗ |
| 7 | + +-+#^#+--+#^#+--+#^#+-+ |
| 8 | +''' |
| 9 | + |
| 10 | +# Program that prompts the user for a string, and encodes it with ROT13. |
| 11 | +# For each character, find the corresponding character, add it to an output string. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +from string import ascii_letters, ascii_lowercase |
| 20 | + |
| 21 | + |
| 22 | +def rot13 (user_input, num): |
| 23 | + """Encrypts letter by adding changing it to a defined letter at another index""" |
| 24 | + from string import ascii_lowercase |
| 25 | + |
| 26 | + # create list with letters sorted starting with designated letter instead of a |
| 27 | + # I have no idea why it wouldn't get the last letter of the list without ascii_lowercase[-1] |
| 28 | + rot = list(ascii_lowercase[num:-1] +ascii_lowercase[-1] +ascii_lowercase[0:num]) |
| 29 | + print(rot) |
| 30 | + index_nums = [] |
| 31 | + rot_list = [] |
| 32 | + |
| 33 | + # get index value of user_input chars within ascii_lowercase |
| 34 | + for char in user_input: |
| 35 | + x = ascii_lowercase.index(char) |
| 36 | + index_nums.append(x) |
| 37 | + |
| 38 | + print(index_nums) |
| 39 | + |
| 40 | + # take index values and apply them to rot_list[index values] to get corresponding char |
| 41 | + for char in index_nums: |
| 42 | + x = rot[char] |
| 43 | + rot_list.append(x) |
| 44 | + |
| 45 | + print(rot_list) |
| 46 | + print("".join(rot_list)) |
| 47 | + |
| 48 | + |
| 49 | +def rot_decrypt (user_input, num): |
| 50 | + """Encrypts letter by adding changing it to a defined letter at another index""" |
| 51 | + from string import ascii_lowercase |
| 52 | + |
| 53 | + # create list with letters sorted starting with designated letter index value |
| 54 | + rot = list(ascii_lowercase[num:-1] +ascii_lowercase[-1] +ascii_lowercase[0:num]) |
| 55 | + decrypted = [] |
| 56 | + |
| 57 | + # for char in user_input take the value of the corresponding rot, and apply that index value to the ascii_lowercase |
| 58 | + # append that char to a decrypted list |
| 59 | + for char in user_input: |
| 60 | + x = rot.index(char) |
| 61 | + b = ascii_lowercase[x] |
| 62 | + decrypted.append(b) |
| 63 | + |
| 64 | + print(decrypted) |
| 65 | + print("".join(decrypted)) |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +# Encrypt user_input to rot_13 |
| 75 | +while True: |
| 76 | + |
| 77 | + user_phrase = input("Enter word to convert\n").lower().strip().replace(" ", "") # ensures input is a valid number |
| 78 | + if user_phrase.isalpha() == True: |
| 79 | + break |
| 80 | + else: |
| 81 | + print("Enter a valid input") |
| 82 | + continue |
| 83 | + |
| 84 | + |
| 85 | +# Designate amount of rotations for encryption |
| 86 | +while True: |
| 87 | + try: |
| 88 | + user_num = int(input("Enter number to determine starting letter for encryption\n")) |
| 89 | + break |
| 90 | + except ValueError: |
| 91 | + print("Enter valid input, 1-26") |
| 92 | + continue |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +rot13(user_phrase, user_num) |
| 97 | + |
| 98 | + |
| 99 | +# Decrypt back to ascii_lowercase |
| 100 | +while True: |
| 101 | + |
| 102 | + user_phrase = input("Enter word to convert\n").lower().strip().replace(" ", "") # ensures input is a valid number |
| 103 | + if user_phrase.isalpha() == True: |
| 104 | + break |
| 105 | + else: |
| 106 | + print("Enter a valid input") |
| 107 | + continue |
| 108 | + |
| 109 | + |
| 110 | +# Enter amount of rotations to decrypt |
| 111 | +while True: |
| 112 | + try: |
| 113 | + user_num = int(input("Enter number to determine starting letter for encryption\n")) |
| 114 | + break |
| 115 | + except ValueError: |
| 116 | + print("Enter valid input, 1-26") |
| 117 | + continue |
| 118 | + |
| 119 | + |
| 120 | +rot_decrypt(user_phrase, user_num) |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
0 commit comments