├── AtBash.py ├── Caesar.py ├── ColTransCipher.py ├── KeywordCipher.py ├── LICENSE ├── README.md ├── ROT13.py ├── ROT18.py ├── ROT47.py ├── ROT5.py ├── RailFence.py ├── Vigenere.py ├── XOR Cipher.py └── hill_cipher.py /AtBash.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | def at_encryption(): 9 | alpa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10 | # reversing alphabets of alpa variable 11 | rev_alpa = alpa[::-1] 12 | 13 | message = input("Enter message: ").upper(); 14 | 15 | encry_text = "" 16 | 17 | for i in range(len(message)): 18 | if message[i] == chr(32): 19 | encry_text += " " 20 | else: 21 | for j in range(len(alpa)): 22 | if message[i] == alpa[j]: 23 | encry_text += rev_alpa[j] 24 | break 25 | # if 26 | # inner for 27 | # if-else 28 | # for 29 | 30 | print("Encrypted Text: {}".format(encry_text)) 31 | 32 | 33 | def at_decryption(): 34 | alpa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 35 | # reversing alphabets of alpa variable 36 | rev_alpa = alpa[::-1] 37 | 38 | message = input("Enter message: ").upper(); 39 | 40 | dencry_text = "" 41 | 42 | for i in range(len(message)): 43 | if message[i] == chr(32): 44 | dencry_text += " " 45 | else: 46 | for j in range(len(rev_alpa)): 47 | if message[i] == rev_alpa[j]: 48 | dencry_text += alpa[j] 49 | break 50 | # if 51 | # inner for 52 | # if-else 53 | # for 54 | 55 | print("Decrypted Text: {}".format(dencry_text)) 56 | 57 | 58 | def main(): 59 | choice = int(input("1. Encryption\n2.Decryption\nChoose(1,2): ")) 60 | if choice == 1: 61 | print("---Encryption---") 62 | at_encryption() 63 | elif choice == 2: 64 | print("---Decryption---") 65 | at_decryption() 66 | else: 67 | print("Wrong Choice") 68 | 69 | 70 | if __name__ == "__main__": 71 | main() 72 | -------------------------------------------------------------------------------- /Caesar.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | # we'll be using ASCII in this 8 | 9 | 10 | def encryption(): 11 | print("Encryption") 12 | 13 | print("Message can only be Lower or Uppercase alphabet") 14 | msg = input("Enter message: ") 15 | key = int(input("Enter key(0-25): ")) # based on 26 letters of alphabet 16 | 17 | encrypted_text = "" 18 | 19 | for i in range(len(msg)): 20 | if ord(msg[i]) == 32: # ord() will give us the ASCII of space char, which is 32 21 | encrypted_text += chr(ord(msg[i])) # chr() will convert ASCII back to character 22 | 23 | elif ord(msg[i]) + key > 122: 24 | # after 'z' move back to 'a', 'a' = 97, 'z' = 122 25 | temp = (ord(msg[i]) + key) - 122 # subtracting 122 to get a lower int and adding it in 96 26 | encrypted_text += chr(96+temp) 27 | 28 | elif (ord(msg[i]) + key > 90) and (ord(msg[i]) <= 96): 29 | # moving back to 'A' after 'Z' 30 | temp = (ord(msg[i]) + key) - 90 31 | encrypted_text += chr(64+temp) 32 | 33 | else: 34 | # in case of letters being between a-z and A-Z 35 | encrypted_text += chr(ord(msg[i]) + key) 36 | 37 | print("Encrypted: " + encrypted_text) 38 | 39 | 40 | def decryption(): 41 | print("Decryption") 42 | 43 | print("Message can only be Lower or Uppercase alphabet") 44 | encrp_msg = input("Enter encrypted Text: ") 45 | decrp_key = int(input("Enter key(0-25): ")) 46 | 47 | decrypted_text = "" 48 | 49 | for i in range(len(encrp_msg)): 50 | if ord(encrp_msg[i]) == 32: 51 | decrypted_text += chr(ord(encrp_msg[i])) 52 | 53 | elif ((ord(encrp_msg[i]) - decrp_key) < 97) and ((ord(encrp_msg[i]) - decrp_key) > 90): 54 | # subtract key from letter ASCII and add 26 to current number 55 | temp = (ord(encrp_msg[i]) - decrp_key) + 26 56 | decrypted_text += chr(temp) 57 | 58 | elif (ord(encrp_msg[i]) - decrp_key) < 65: 59 | temp = (ord(encrp_msg[i]) - decrp_key) + 26 60 | decrypted_text += chr(temp) 61 | 62 | else: 63 | decrypted_text += chr(ord(encrp_msg[i]) - decrp_key) 64 | 65 | print("Decrypted Text: " + decrypted_text) 66 | 67 | 68 | def main(): 69 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 70 | if choice == 1: 71 | encryption() 72 | elif choice == 2: 73 | decryption() 74 | else: 75 | print("Wrong Choice") 76 | 77 | 78 | if __name__ == "__main__": 79 | main() -------------------------------------------------------------------------------- /ColTransCipher.py: -------------------------------------------------------------------------------- 1 | def cipher_encryption(): 2 | msg = input("Enter Plain Text: ").replace(" ", "").upper() 3 | # print(msg) 4 | key = input("Enter keyword: ").upper() 5 | 6 | # assigning numbers to keywords 7 | kywrd_num_list = keyword_num_assign(key) 8 | 9 | # printing key 10 | for i in range(len(key)): 11 | print(key[i], end=" ", flush=True) 12 | # for 13 | print() 14 | for i in range(len(key)): 15 | print(str(kywrd_num_list[i]), end=" ", flush=True) 16 | # for 17 | print() 18 | print("-------------------------") 19 | 20 | # in case characters don't fit the entire grid perfectly. 21 | extra_letters = len(msg) % len(key) 22 | # print(extraLetters) 23 | dummy_characters = len(key) - extra_letters 24 | # print(dummyCharacters) 25 | 26 | if extra_letters != 0: 27 | for i in range(dummy_characters): 28 | msg += "." 29 | # if 30 | 31 | # print(msg) 32 | 33 | num_of_rows = int(len(msg) / len(key)) 34 | 35 | # Converting message into a grid 36 | arr = [[0] * len(key) for i in range(num_of_rows)] 37 | z = 0 38 | for i in range(num_of_rows): 39 | for j in range(len(key)): 40 | arr[i][j] = msg[z] 41 | z += 1 42 | # for 43 | # for 44 | 45 | for i in range(num_of_rows): 46 | for j in range(len(key)): 47 | print(arr[i][j], end=" ", flush=True) 48 | print() 49 | # for 50 | 51 | # getting locations of numbers 52 | num_loc = get_number_location(key, kywrd_num_list) 53 | 54 | print(num_loc) 55 | 56 | # cipher 57 | cipher_text = "" 58 | k = 0 59 | for i in range(num_of_rows): 60 | if k == len(key): 61 | break 62 | else: 63 | d = int(num_loc[k]) 64 | # if 65 | for j in range(num_of_rows): 66 | cipher_text += arr[j][d] 67 | # for 68 | k += 1 69 | # for 70 | 71 | print("Cipher Text: {}".format(cipher_text)) 72 | 73 | 74 | def get_number_location(key, kywrd_num_list): 75 | num_loc = "" 76 | for i in range(len(key) + 1): 77 | for j in range(len(key)): 78 | if kywrd_num_list[j] == i: 79 | num_loc += str(j) 80 | # if 81 | # for 82 | # for 83 | return num_loc 84 | 85 | 86 | def keyword_num_assign(key): 87 | alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 88 | kywrd_num_list = list(range(len(key))) 89 | # print(kywrdNumList) 90 | init = 0 91 | for i in range(len(alpha)): 92 | for j in range(len(key)): 93 | if alpha[i] == key[j]: 94 | init += 1 95 | kywrd_num_list[j] = init 96 | # if 97 | # inner for 98 | # for 99 | return kywrd_num_list 100 | 101 | 102 | def cipher_decryption(): 103 | msg = input("Enter Cipher Text: ").replace(" ", "").upper() 104 | # print(msg) 105 | key = input("Enter keyword: ").upper() 106 | 107 | # assigning numbers to keywords 108 | kywrd_num_list = keyword_num_assign(key) 109 | 110 | num_of_rows = int(len(msg) / len(key)) 111 | 112 | # getting locations of numbers 113 | num_loc = get_number_location(key, kywrd_num_list) 114 | 115 | # Converting message into a grid 116 | arr = [[0] * len(key) for i in range(num_of_rows)] 117 | 118 | # decipher 119 | plain_text = "" 120 | k = 0 121 | itr = 0 122 | 123 | # print(arr[6][4]) 124 | # itr = len(msg) 125 | 126 | for i in range(len(msg)): 127 | d = 0 128 | if k == len(key): 129 | k = 0 130 | else: 131 | d: int = int(num_loc[k]) 132 | for j in range(num_of_rows): 133 | arr[j][d] = msg[itr] 134 | # print("j: {} d: {} m: {} l: {} ". format(j, d, msg[l], l)) 135 | itr += 1 136 | if itr == len(msg): 137 | break 138 | k += 1 139 | print() 140 | 141 | for i in range(num_of_rows): 142 | for j in range(len(key)): 143 | plain_text += str(arr[i][j]) 144 | # for 145 | # for 146 | 147 | print("Plain Text: " + plain_text) 148 | 149 | 150 | def main(): 151 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 152 | if choice == 1: 153 | print("Encryption") 154 | cipher_encryption() 155 | elif choice == 2: 156 | print("Decryption") 157 | cipher_decryption() 158 | else: 159 | print("Invalid Choice") 160 | 161 | 162 | if __name__ == "__main__": 163 | main() 164 | -------------------------------------------------------------------------------- /KeywordCipher.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | import sys 9 | 10 | 11 | def keyword_picker(): 12 | print("Keyword must contain each letter of A-Z exactly once, no letter should repeat itself") 13 | keyword = input("Enter keyword: ").upper() 14 | for i in range(len(keyword)): 15 | pos = i 16 | for j in range(len(keyword)): 17 | if pos == j: 18 | # skipping letter comparison with itself 19 | continue 20 | elif keyword[i] == keyword[j]: 21 | print("Letter {} repeating in keyword".format(keyword[i])) 22 | sys.exit() 23 | # elif 24 | # inner for 25 | # for 26 | 27 | temp = "" 28 | for i in range(len(keyword)): 29 | temp += keyword[i] 30 | for i in range(26): 31 | temp += chr(i+65) 32 | 33 | # removing keyword letters from alphabet 34 | alpha_with_key = "" 35 | for i in range(len(temp)): 36 | found = False 37 | for j in range(len(alpha_with_key)): 38 | if temp[i] == alpha_with_key[j]: 39 | found = True 40 | break 41 | # if 42 | # inner for 43 | if not found: 44 | alpha_with_key += temp[i] 45 | # if 46 | # for 47 | 48 | # print(alpha_with_key) 49 | 50 | return alpha_with_key 51 | 52 | 53 | def encryption(alpha_key, alpha): 54 | message = input("Enter message: ").upper() 55 | 56 | encrypted_text = "" 57 | for i in range(len(message)): 58 | if message[i] == chr(32): 59 | encrypted_text += " " 60 | else: 61 | counter = 0 62 | for j in range(len(alpha)): 63 | if message[i] == alpha[j]: 64 | encrypted_text += alpha_key[counter] 65 | break 66 | else: 67 | counter += 1 68 | # inner for 69 | # if-else 70 | # for 71 | 72 | print("Encrypted Message: {}".format(encrypted_text)) 73 | 74 | 75 | def decryption(alpha_key, alpha): 76 | message = input("Enter Encrypted Message: ").upper() 77 | 78 | decrypted_text = "" 79 | for i in range(len(message)): 80 | if message[i] == chr(32): 81 | decrypted_text += " " 82 | else: 83 | counter = 0 84 | for j in range(len(alpha)): 85 | if message[i] == alpha_key[j]: 86 | decrypted_text += alpha[counter] 87 | break 88 | else: 89 | counter += 1 90 | # inner for 91 | # if-else 92 | # for 93 | 94 | print("Decrypted Text: {}".format(decrypted_text)) 95 | 96 | 97 | def main(): 98 | alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 99 | alpha_key = keyword_picker() 100 | 101 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 102 | if choice == 1: 103 | print("---Encryption---") 104 | encryption(alpha_key, alpha) 105 | 106 | elif choice == 2: 107 | print("---Decryption---") 108 | decryption(alpha_key, alpha) 109 | 110 | else: 111 | print("Incorrect Choice") 112 | 113 | if __name__ == "__main__": 114 | main() 115 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 VoxelPixel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ciphers Python Source Code 2 | Source Code of Ciphers programmed in Python for YT channel 3 | 4 | https://www.youtube.com/channel/UCMHIdhpwf6pSLg87xvQ-Mqg/ 5 | -------------------------------------------------------------------------------- /ROT13.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | def cipher_encryption(): 9 | print("Message can only be alphabetic") 10 | message = input("Enter message: ").upper() 11 | 12 | key = 13 13 | encryp_text = "" 14 | 15 | for i in range(len(message)): 16 | temp = ord(message[i]) + key 17 | if ord(message[i]) == 32: 18 | encryp_text += " " 19 | elif temp > 90: 20 | temp -= 26 21 | encryp_text += chr(temp) 22 | else: 23 | encryp_text += chr(temp) 24 | # if-else 25 | # for 26 | 27 | print("Encrypted Text: {}".format(encryp_text)) 28 | 29 | 30 | def cipher_decryption(): 31 | print("Message can only be alphabetic") 32 | message = input("Enter message: ").upper() 33 | 34 | key = 13 35 | decryp_text = "" 36 | 37 | for i in range(len(message)): 38 | temp = ord(message[i]) - key 39 | if ord(message[i]) == 32: 40 | decryp_text += " " 41 | elif temp < 65: 42 | temp += 26 43 | decryp_text += chr(temp) 44 | else: 45 | decryp_text += chr(temp) 46 | # if-else 47 | # for 48 | 49 | print("Decrypted Text: {}".format(decryp_text)) 50 | 51 | 52 | def main(): 53 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 54 | if choice == 1: 55 | print("---Encryption---") 56 | cipher_encryption() 57 | 58 | elif choice == 2: 59 | print("---Decryption---") 60 | cipher_decryption() 61 | 62 | else: 63 | print("Invalid Choice") 64 | 65 | 66 | if __name__ == "__main__": 67 | main() 68 | -------------------------------------------------------------------------------- /ROT18.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | import re 9 | 10 | def cipher_encryption(): 11 | rot5 = "5678901234" 12 | zeroToNine = "0123456789" 13 | rot_13_key = 13 14 | 15 | print("Message can be alphanumeric") 16 | message = input("Enter message: ").upper() 17 | 18 | encryp_text = ""; 19 | for i in range(len(message)): 20 | temp = message[i] + "" 21 | if ord(message[i]) == 32: 22 | encryp_text += " " 23 | elif re.search('[\d\s]+', temp): 24 | # ROT5 25 | for j in range(len(zeroToNine)): 26 | if message[i] == zeroToNine[j]: 27 | encryp_text += rot5[j] 28 | # inner for 29 | elif re.search('[\w\s]+', temp): 30 | # ROT13 31 | ch_temp = ord(message[i]) + rot_13_key 32 | if ord(message[i]) == 32: 33 | encryp_text += " " 34 | elif ch_temp > 90: 35 | ch_temp -= 26 36 | encryp_text += chr(ch_temp) 37 | else: 38 | encryp_text += chr(ch_temp) 39 | # if-else 40 | # for 41 | 42 | print("Encrypted Text: {}".format(encryp_text)) 43 | 44 | 45 | def cipher_decryption(): 46 | rot5 = "5678901234" 47 | zeroToNine = "0123456789" 48 | rot_13_key = 13 49 | 50 | print("Message can be alphanumeric") 51 | message = input("Enter message: ").upper() 52 | 53 | decryp_text = ""; 54 | for i in range(len(message)): 55 | temp = message[i] + "" 56 | if ord(message[i]) == 32: 57 | decryp_text += " " 58 | elif re.search('[\d\s]+', temp): 59 | # ROT5 60 | for j in range(len(zeroToNine)): 61 | if message[i] == rot5[j]: 62 | decryp_text += zeroToNine[j] 63 | # inner for 64 | elif re.search('[\w\s]+', temp): 65 | # ROT13 66 | ch_temp = ord(message[i]) - rot_13_key 67 | if ord(message[i]) == 32: 68 | decryp_text += " " 69 | elif ch_temp < 65: 70 | ch_temp += 26 71 | decryp_text += chr(ch_temp) 72 | else: 73 | decryp_text += chr(ch_temp) 74 | # if-else 75 | # for 76 | 77 | print("Decrypted Text: {}".format(decryp_text)) 78 | 79 | 80 | def main(): 81 | choice = int(input("1. Encryption\n2. Decryption\nChoose (1,2): ")) 82 | if choice == 1: 83 | print("---Encryption---") 84 | cipher_encryption() 85 | elif choice == 2: 86 | print("---Decryption---") 87 | cipher_decryption() 88 | else: 89 | print("Invalid Choice") 90 | 91 | 92 | if __name__ == "__main__": 93 | main() 94 | -------------------------------------------------------------------------------- /ROT47.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | def cipher_encryption(): 8 | message = input("Enter message: ") 9 | key = 47 10 | encryp_text = "" 11 | 12 | for i in range(len(message)): 13 | temp = ord(message[i]) + key 14 | if ord(message[i]) == 32: 15 | encryp_text += " " 16 | elif temp > 126: 17 | temp -= 94 18 | encryp_text += chr(temp) 19 | else: 20 | encryp_text += chr(temp) 21 | # if-else 22 | # for 23 | 24 | print("Encrypted Text: {}".format(encryp_text)) 25 | 26 | 27 | def cipher_decryption(): 28 | message = input("Enter message: ") 29 | key = 47 30 | decryp_text = "" 31 | 32 | for i in range(len(message)): 33 | temp = ord(message[i]) - key 34 | if ord(message[i]) == 32: 35 | decryp_text += " " 36 | elif temp < 32: 37 | temp += 94 38 | decryp_text += chr(temp) 39 | else: 40 | decryp_text += chr(temp) 41 | # if-else 42 | # for 43 | 44 | print("Decrypted Text: {}".format(decryp_text)) 45 | 46 | 47 | def main(): 48 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 49 | if choice == 1: 50 | print("---Encryption---") 51 | cipher_encryption() 52 | elif choice == 2: 53 | print("---Decryption---") 54 | cipher_decryption() 55 | else: 56 | print("Invalid Choice") 57 | 58 | if __name__ == "__main__": 59 | main() 60 | 61 | -------------------------------------------------------------------------------- /ROT5.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | import re 9 | import sys 10 | 11 | 12 | def cipher_encryption(rot5, zero_to_nine): 13 | message = input("Enter message: ") 14 | 15 | # checking if input is int or not 16 | if not re.search('[\d\s]+', message): 17 | print("Entered message is not an integer") 18 | sys.exit() 19 | # \d = int 20 | # \s = white space 21 | # + = one or more times 22 | # [] a set, with logical OR 23 | 24 | encryp_text = "" 25 | for i in range(len(message)): 26 | if message[i] == chr(32): 27 | encryp_text += " " 28 | else: 29 | for j in range(len(zero_to_nine)): 30 | # simple substitution 31 | if message[i] == zero_to_nine[j]: 32 | encryp_text += rot5[j] 33 | # inner if 34 | # inner for 35 | # if-else 36 | # for 37 | 38 | print("Encrypted Text: {}".format(encryp_text)) 39 | 40 | 41 | def cipher_decryption(rot5, zero_to_nine): 42 | message = input("Enter message: ") 43 | 44 | # checking if input is int or not 45 | if not re.search('[\d\s]+', message): 46 | print("Entered message is not an integer") 47 | sys.exit() 48 | # \d = int 49 | # \s = white space 50 | # + = one or more times 51 | # [] a set, with logical OR 52 | 53 | decryp_text = "" 54 | for i in range(len(message)): 55 | if message[i] == chr(32): 56 | decryp_text += " " 57 | else: 58 | for j in range(len(zero_to_nine)): 59 | # simple substitution 60 | if message[i] == rot5[j]: 61 | decryp_text += zero_to_nine[j] 62 | # inner if 63 | # inner for 64 | # if-else 65 | # for 66 | 67 | print("Encrypted Text: {}".format(decryp_text)) 68 | 69 | 70 | def main(): 71 | rot5 = "5678901234" 72 | zero_to_nine = "0123456789" 73 | 74 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 75 | if choice == 1: 76 | print("---Encryption---") 77 | cipher_encryption(rot5, zero_to_nine) 78 | 79 | elif choice == 2: 80 | print("---Decryption---") 81 | cipher_decryption(rot5, zero_to_nine) 82 | 83 | else: 84 | print("Wrong Choice") 85 | 86 | if __name__ == "__main__": 87 | main() 88 | -------------------------------------------------------------------------------- /RailFence.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def cipher_encryption(): 5 | msg = input("Enter message: ") 6 | rails = int(input("Enter number of rails: ")) 7 | 8 | # removing white space from message 9 | msg = msg.replace(" ", "") 10 | 11 | # creating an empty matrix 12 | railMatrix = [] 13 | for i in range(rails): 14 | railMatrix.append([]) 15 | for row in range(rails): 16 | for column in range(len(msg)): 17 | railMatrix[row].append('.') 18 | # inner for 19 | # for 20 | 21 | # testing the matrix 22 | # for row in railMatrix: 23 | # for column in row: 24 | # print(column, end="") 25 | # print("\n") 26 | # # inner for 27 | # # for 28 | 29 | # putting letters of message one by one in the matrix in zig-zag 30 | row = 0 31 | check = 0 32 | for i in range(len(msg)): 33 | if check == 0: 34 | railMatrix[row][i] = msg[i] 35 | row += 1 36 | if row == rails: 37 | check = 1 38 | row -= 1 39 | # inner if 40 | elif check == 1: 41 | row -= 1 42 | railMatrix[row][i] = msg[i] 43 | if row == 0: 44 | check = 0 45 | row = 1 46 | # inner if 47 | # if-else 48 | 49 | # testing the matrix with message in zig-zag 50 | # for row in railMatrix: 51 | # for column in row: 52 | # print(column, end="") 53 | # print("\n") 54 | # # inner for 55 | # # for 56 | 57 | encryp_text = "" 58 | for i in range(rails): 59 | for j in range(len(msg)): 60 | encryp_text += railMatrix[i][j] 61 | # for 62 | 63 | # removing '.' from encrypted text 64 | encryp_text = re.sub(r"\.", "", encryp_text) 65 | print("Encrypted Text: {}".format(encryp_text)) 66 | 67 | 68 | def cipher_decryption(): 69 | msg = input("Enter message: ") 70 | rails = int(input("Enter number of rails: ")) 71 | 72 | # removing white space from message 73 | msg = msg.replace(" ", "") 74 | 75 | # creating an empty matrix 76 | railMatrix = [] 77 | for i in range(rails): 78 | railMatrix.append([]) 79 | for row in range(rails): 80 | for column in range(len(msg)): 81 | railMatrix[row].append('.') 82 | # inner for 83 | # for 84 | 85 | # testing the matrix 86 | # for row in railMatrix: 87 | # for column in row: 88 | # print(column, end="") 89 | # print("\n") 90 | # # inner for 91 | # # for 92 | 93 | # putting letters of message one by one in the matrix in zig-zag 94 | row = 0 95 | check = 0 96 | for i in range(len(msg)): 97 | if check == 0: 98 | railMatrix[row][i] = msg[i] 99 | row += 1 100 | if row == rails: 101 | check = 1 102 | row -= 1 103 | # inner if 104 | elif check == 1: 105 | row -= 1 106 | railMatrix[row][i] = msg[i] 107 | if row == 0: 108 | check = 0 109 | row = 1 110 | # inner if 111 | # if-else 112 | 113 | # testing the matrix with message in zig-zag 114 | # for row in railMatrix: 115 | # for column in row: 116 | # print(column, end="") 117 | # print("\n") 118 | # # inner for 119 | # # for 120 | 121 | # reordering the matrix 122 | ordr = 0 123 | for i in range(rails): 124 | for j in range(len(msg)): 125 | temp = railMatrix[i][j] 126 | if re.search("\\.", temp): 127 | # skipping '.' 128 | continue 129 | else: 130 | railMatrix[i][j] = msg[ordr] 131 | ordr += 1 132 | # if-else 133 | # inner for 134 | # for 135 | 136 | # testing matrix reorder 137 | for i in railMatrix: 138 | for column in i: 139 | print(column, end="") 140 | #inner for 141 | print("\n") 142 | # for 143 | 144 | # putting reordered matrix into decrypted text string to get decrypted text 145 | check = 0 146 | row = 0 147 | decryp_text = "" 148 | for i in range(len(msg)): 149 | if check == 0: 150 | decryp_text += railMatrix[row][i] 151 | row += 1 152 | if row == rails: 153 | check = 1 154 | row -= 1 155 | # inner if 156 | elif check == 1: 157 | row -= 1 158 | decryp_text += railMatrix[row][i] 159 | if row == 0: 160 | check = 0 161 | row = 1 162 | # inner if 163 | # if-else 164 | # for 165 | 166 | decryp_text = re.sub(r"\.", "", decryp_text) 167 | print("Decrypted Text: {}".format(decryp_text)) 168 | 169 | 170 | def main(): 171 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 172 | if choice == 1: 173 | print("---Encryption---") 174 | cipher_encryption() 175 | elif choice == 2: 176 | print("---Decryption---") 177 | cipher_decryption() 178 | else: 179 | print("Invalid Choice") 180 | 181 | if __name__ == "__main__": 182 | main() 183 | -------------------------------------------------------------------------------- /Vigenere.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | def msg_and_key(): 9 | msg = input("Enter message: ").upper() 10 | key = input("Enter key: ").upper() 11 | 12 | # variable to store mapped key 13 | key_map = "" 14 | 15 | j=0 16 | for i in range(len(msg)): 17 | if ord(msg[i]) == 32: 18 | # ignoring space 19 | key_map += " " 20 | else: 21 | if j < len(key): 22 | key_map += key[j] 23 | j += 1 24 | else: 25 | j = 0 26 | key_map += key[j] 27 | j += 1 28 | 29 | # print(key_map) 30 | return msg, key_map 31 | 32 | 33 | def create_vigenere_table(): 34 | table = [] 35 | for i in range(26): 36 | table.append([]) 37 | 38 | for row in range(26): 39 | for column in range(26): 40 | if (row + 65) + column > 90: 41 | # moving back to A after Z 42 | # after first row, each letter will shift left by one position compared to row above it 43 | table[row].append(chr((row+65) + column - 26)) 44 | else: 45 | # after first row, each letter will shift left by one position compared to row above it 46 | table[row].append(chr((row+65)+column)) 47 | 48 | # printing the table 49 | # for row in table: 50 | # for column in row: 51 | # print(column, end=" ") 52 | # print(end="\n") 53 | 54 | return table 55 | 56 | 57 | def cipher_encryption(message, mapped_key): 58 | table = create_vigenere_table() 59 | encrypted_text = "" 60 | 61 | for i in range(len(message)): 62 | if message[i] == chr(32): 63 | # ignoring space 64 | encrypted_text += " " 65 | else: 66 | # getting element at specific index of table 67 | row = ord(message[i])-65 68 | column = ord(mapped_key[i]) - 65 69 | encrypted_text += table[row][column] 70 | 71 | print("Encrypted Message: {}".format(encrypted_text)) 72 | 73 | 74 | def itr_count(mapped_key, message): 75 | counter = 0 76 | result = "" 77 | 78 | # starting alphabets from mapped_key letter and finishing all 26 letters from it, (after z we move to a) 79 | for i in range(26): 80 | if mapped_key + i > 90: 81 | result += chr(mapped_key+(i-26)) 82 | else: 83 | result += chr(mapped_key+i) 84 | 85 | # counting the number of iterations it take from mapped key letter to ciphertext letter 86 | for i in range(len(result)): 87 | if result[i] == chr(message): 88 | break 89 | else: 90 | counter += 1 91 | 92 | return counter 93 | 94 | 95 | def cipher_decryption(message, mapped_key): 96 | table = create_vigenere_table() 97 | decrypted_text = "" 98 | 99 | for i in range(len(message)): 100 | if message[i] == chr(32): 101 | # ignoring space 102 | decrypted_text += " " 103 | else: 104 | # adding number of iterations, it takes to reach from mapped key letter to cipher letter in 65 105 | # by doing so we get column header of ciphertext letter, which happens to be decrypted letter 106 | decrypted_text += chr(65 + itr_count(ord(mapped_key[i]), ord(message[i]))) 107 | 108 | print("Decrypted Message: {}".format(decrypted_text)) 109 | 110 | 111 | def main(): 112 | print("Key and Message can only be alphabetic") 113 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 114 | if choice == 1: 115 | print("---Encryption---") 116 | message, mapped_key = msg_and_key() 117 | cipher_encryption(message, mapped_key) 118 | 119 | elif choice == 2: 120 | print("---Decryption---") 121 | message, mapped_key = msg_and_key() 122 | cipher_decryption(message, mapped_key) 123 | 124 | else: 125 | print("Wrong choice") 126 | 127 | 128 | if __name__ == "__main__": 129 | main() 130 | -------------------------------------------------------------------------------- /XOR Cipher.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | def cipher_encryption(): 9 | msg = input("Enter message: ") 10 | key = input("Enter key: ") 11 | 12 | encrypt_hex = "" 13 | key_itr = 0 14 | for i in range(len(msg)): 15 | temp = ord(msg[i]) ^ ord(key[key_itr]) 16 | # zfill will pad a single letter hex with 0, to make it two letter pair 17 | encrypt_hex += hex(temp)[2:].zfill(2) 18 | key_itr += 1 19 | if key_itr >= len(key): 20 | # once all of the key's letters are used, repeat the key 21 | key_itr = 0 22 | 23 | print("Encrypted Text: {}".format(encrypt_hex)) 24 | 25 | 26 | def cipher_decryption(): 27 | msg = input("Enter message: ") 28 | key = input("Enter key: ") 29 | 30 | hex_to_uni = "" 31 | for i in range(0, len(msg), 2): 32 | hex_to_uni += bytes.fromhex(msg[i:i+2]).decode('utf-8') 33 | 34 | decryp_text = "" 35 | key_itr = 0 36 | for i in range(len(hex_to_uni)): 37 | temp = ord(hex_to_uni[i]) ^ ord(key[key_itr]) 38 | # zfill will pad a single letter hex with 0, to make it two letter pair 39 | decryp_text += chr(temp) 40 | key_itr += 1 41 | if key_itr >= len(key): 42 | # once all of the key's letters are used, repeat the key 43 | key_itr = 0 44 | 45 | print("Decrypted Text: {}".format(decryp_text)) 46 | 47 | def main(): 48 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 49 | if choice == 1: 50 | print("---Encryption---") 51 | cipher_encryption() 52 | elif choice == 2: 53 | print("---Decryption---") 54 | cipher_decryption() 55 | else: 56 | print("Invalid Choice") 57 | 58 | if __name__ == "__main__": 59 | main() 60 | -------------------------------------------------------------------------------- /hill_cipher.py: -------------------------------------------------------------------------------- 1 | # ********* 2 | # -*- Made by VoxelPixel 3 | # -*- For YouTube Tutorial 4 | # -*- https://github.com/VoxelPixel 5 | # -*- Support me on Patreon: https://www.patreon.com/voxelpixel 6 | # ********* 7 | 8 | import sys 9 | import numpy as np 10 | 11 | 12 | def cipher_encryption(): 13 | msg = input("Enter message: ").upper() 14 | msg = msg.replace(" ", "") 15 | 16 | # if message length is odd number, append 0 at the end 17 | len_chk = 0 18 | if len(msg) % 2 != 0: 19 | msg += "0" 20 | len_chk = 1 21 | 22 | # msg to matrices 23 | row = 2 24 | col = int(len(msg)/2) 25 | msg2d = np.zeros((row, col), dtype=int) 26 | 27 | itr1 = 0 28 | itr2 = 0 29 | for i in range(len(msg)): 30 | if i % 2 == 0: 31 | msg2d[0][itr1] = int(ord(msg[i])-65) 32 | itr1 += 1 33 | else: 34 | msg2d[1][itr2] = int(ord(msg[i])-65) 35 | itr2 += 1 36 | # for 37 | 38 | key = input("Enter 4 letter Key String: ").upper() 39 | key = key.replace(" ", "") 40 | 41 | # key to 2x2 42 | key2d = np.zeros((2, 2), dtype=int) 43 | itr3 = 0 44 | for i in range(2): 45 | for j in range(2): 46 | key2d[i][j] = ord(key[itr3])-65 47 | itr3 += 1 48 | 49 | # checking validity of the key 50 | # finding determinant 51 | deter = key2d[0][0] * key2d[1][1] - key2d[0][1] * key2d[1][0] 52 | deter = deter % 26 53 | 54 | # finding multiplicative inverse 55 | mul_inv = -1 56 | for i in range(26): 57 | temp_inv = deter * i 58 | if temp_inv % 26 == 1: 59 | mul_inv = i 60 | break 61 | else: 62 | continue 63 | # for 64 | 65 | if mul_inv == -1: 66 | print("Invalid key") 67 | sys.exit() 68 | # if 69 | 70 | encryp_text = "" 71 | itr_count = int(len(msg)/2) 72 | if len_chk == 0: 73 | for i in range(itr_count): 74 | temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1] 75 | encryp_text += chr((temp1 % 26) + 65) 76 | temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1] 77 | encryp_text += chr((temp2 % 26) + 65) 78 | # for 79 | else: 80 | for i in range(itr_count-1): 81 | temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1] 82 | encryp_text += chr((temp1 % 26) + 65) 83 | temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1] 84 | encryp_text += chr((temp2 % 26) + 65) 85 | # for 86 | # if else 87 | 88 | print("Encrypted Text: {}".format(encryp_text)) 89 | 90 | 91 | def cipher_decryption(): 92 | msg = input("Enter message: ").upper() 93 | msg = msg.replace(" ", "") 94 | 95 | # if message length is odd number, append 0 at the end 96 | len_chk = 0 97 | if len(msg) % 2 != 0: 98 | msg += "0" 99 | len_chk = 1 100 | 101 | # msg to matrices 102 | row = 2 103 | col = int(len(msg) / 2) 104 | msg2d = np.zeros((row, col), dtype=int) 105 | 106 | itr1 = 0 107 | itr2 = 0 108 | for i in range(len(msg)): 109 | if i % 2 == 0: 110 | msg2d[0][itr1] = int(ord(msg[i]) - 65) 111 | itr1 += 1 112 | else: 113 | msg2d[1][itr2] = int(ord(msg[i]) - 65) 114 | itr2 += 1 115 | # for 116 | 117 | key = input("Enter 4 letter Key String: ").upper() 118 | key = key.replace(" ", "") 119 | 120 | # key to 2x2 121 | key2d = np.zeros((2, 2), dtype=int) 122 | itr3 = 0 123 | for i in range(2): 124 | for j in range(2): 125 | key2d[i][j] = ord(key[itr3]) - 65 126 | itr3 += 1 127 | # for 128 | 129 | # finding determinant 130 | deter = key2d[0][0] * key2d[1][1] - key2d[0][1] * key2d[1][0] 131 | deter = deter % 26 132 | 133 | # finding multiplicative inverse 134 | mul_inv = -1 135 | for i in range(26): 136 | temp_inv = deter * i 137 | if temp_inv % 26 == 1: 138 | mul_inv = i 139 | break 140 | else: 141 | continue 142 | # for 143 | 144 | # adjugate matrix 145 | # swapping 146 | key2d[0][0], key2d[1][1] = key2d[1][1], key2d[0][0] 147 | 148 | # changing signs 149 | key2d[0][1] *= -1 150 | key2d[1][0] *= -1 151 | 152 | key2d[0][1] = key2d[0][1] % 26 153 | key2d[1][0] = key2d[1][0] % 26 154 | 155 | # multiplying multiplicative inverse with adjugate matrix 156 | for i in range(2): 157 | for j in range(2): 158 | key2d[i][j] *= mul_inv 159 | 160 | # modulo 161 | for i in range(2): 162 | for j in range(2): 163 | key2d[i][j] = key2d[i][j] % 26 164 | 165 | # cipher to plain 166 | decryp_text = "" 167 | itr_count = int(len(msg) / 2) 168 | if len_chk == 0: 169 | for i in range(itr_count): 170 | temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1] 171 | decryp_text += chr((temp1 % 26) + 65) 172 | temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1] 173 | decryp_text += chr((temp2 % 26) + 65) 174 | # for 175 | else: 176 | for i in range(itr_count - 1): 177 | temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1] 178 | decryp_text += chr((temp1 % 26) + 65) 179 | temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1] 180 | decryp_text += chr((temp2 % 26) + 65) 181 | # for 182 | # if else 183 | 184 | print("Decrypted Text: {}".format(decryp_text)) 185 | 186 | 187 | def main(): 188 | choice = int(input("1. Encryption\n2. Decryption\nChoose(1,2): ")) 189 | if choice == 1: 190 | print("---Encryption---") 191 | cipher_encryption() 192 | elif choice == 2: 193 | print("---Decryption---") 194 | cipher_decryption() 195 | else: 196 | print("Invalid Choice") 197 | 198 | if __name__ == "__main__": 199 | main() 200 | --------------------------------------------------------------------------------