1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| import string
def caesar(ciphertext, shift): shift *= -1 cleartext = "" for char in ciphertext: if char.isalpha(): shifted = ord(char) - shift if char.islower(): cleartext += chr((shifted - ord('a')) % 26 + ord('a')) if char.isupper(): cleartext += chr((shifted - ord('A')) % 26 + ord('A')) else: cleartext += char return cleartext
def egcd(a, b): if a == 0: return (b, 0, 1) else: g, y, x = egcd(b % a, a) return (g, x - (b // a) * y, y)
def modinv(a, m): g, x, y = egcd(a, m) if g != 1: return None else: return x % m
def affine(ciphertext, a, b): cleartext = '' a_inv = modinv(a, 26) if a_inv == None: return cleartext for char in ciphertext: if char in string.ascii_letters: if char.islower(): cleartext += chr(((a_inv * (ord(char) - ord('a') - b)) % 26) + ord('a')) else: cleartext += chr(((a_inv * (ord(char) - ord('A') - b)) % 26) + ord('A')) else: cleartext += char return cleartext
def atbash(ciphertext): table = str.maketrans(string.ascii_letters, string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1]) cleartext = ciphertext.translate(table) return cleartext
def railfence(ciphertext, key): rail = [['\n' for i in range(len(ciphertext))] for j in range(key)] down = None row, col = 0, 0 for i in range(len(ciphertext)): if row == 0: down = True if row == key - 1: down = False rail[row][col] = '*' col += 1 if down: row += 1 else: row -= 1 index = 0 for i in range(key): for j in range(len(ciphertext)): if ((rail[i][j] == '*') and (index < len(ciphertext))): rail[i][j] = ciphertext[index] index += 1 cleartext = [] row, col = 0, 0 for i in range(len(ciphertext)): if row == 0: down = True if row == key - 1: down = False if (rail[row][col] != '*'): cleartext.append(rail[row][col]) col += 1 if down: row += 1 else: row -= 1 return("".join(cleartext))
def vigenere(ciphertext, key): cleartext = [] index = 0 letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' key = key.upper() for char in ciphertext: num = letters.find(char.upper()) if num != -1: num -= letters.find(key[index]) num %= len(letters) if char.isupper(): cleartext.append(letters[num]) elif char.islower(): cleartext.append(letters[num].lower()) index += 1 if index == len(key): index = 0 else: cleartext.append(char) return("".join(cleartext))
def score(input, wordlist): if input == "": return 0 words = input.split() hit = 0 size = len(words) for word in words: for w in wordlist: if w == word: hit += 1 return(round((hit / size) * 100))
def printTop(arr, amt): arr = sorted(arr, key=lambda x: x[3], reverse=True) for i in range(amt): print("{}% {} with key of {}: {}".format(arr[i][3], arr[i][0], arr[i][2], arr[i][1]))
ctext = input("Enter text to attempt decryption:\n")
with open("words.txt", 'r') as file: wordlist = file.read().splitlines()
solutions = [] for i in range(26): plaintext = caesar(ctext, i) solutions += [["Caesar Cipher", plaintext, i, score(plaintext, wordlist)]]
for i in range(1, 20, 2): for j in range(10): plaintext = affine(ctext, i, j) solutions += [["Affine Cipher", plaintext, "{}, {}".format(i, j), score(plaintext, wordlist)]]
plaintext = atbash(ctext) solutions += [["Atbash Cipher", plaintext, "N/A", score(plaintext, wordlist)]]
for i in range(2, 20): plaintext = railfence(ctext, i) solutions += [["Railfence Cipher", plaintext, i, score(plaintext, wordlist)]]
for w in wordlist: plaintext = vigenere(ctext, w) solutions += [["Vigenere Cipher", plaintext, w, score(plaintext, wordlist)]]
printTop(solutions, 5)
|