-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaffineHacker.py
More file actions
44 lines (39 loc) · 1.61 KB
/
affineHacker.py
File metadata and controls
44 lines (39 loc) · 1.61 KB
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
# ========================
# The Affine Cipher Hacker
# ========================
import affineCipher, detectEnglish, cryptomath
from affineCipher import SYMBOLS
SILENT_MODE = False
# return None if not able to crack the code, otherwise returns potential plaintext
def hackAffine(ciphertext):
print('Hacking...')
print('(Print CTRL-C to quit at any time.)')
# total number of possible keys is len of SYMBOLS ** 2
for key in range(len(SYMBOLS) ** 2):
keyA = affineCipher.getTwoKeys(key)[0]
if cryptomath.gcd(keyA, len(affineCipher.SYMBOLS)) != 1:
continue
plaintext = affineCipher.decrypt(key, ciphertext)
if not SILENT_MODE:
print('Tried Key %s: %s' % (key, plaintext[:50]))
if detectEnglish.isEnglish(plaintext, 50, 20):
# Check with the user if this decryption is acceptable
print()
print('Possible decrypted plaintext found.')
print('Key: %s' % (key))
print('Decrypted message: ' + plaintext[:50])
print()
print('Enter A for accept, or Enter for continue hacking...')
response = input('> ')
if response.strip().upper().startswith('A'):
return plaintext
return None
def main():
ciphertext = """"FvLE0JidVTvsEi5QvQVYVTnVvdEvGVvLB55VQvp-dV55pfV-dvpavpdvLEi5QvQVLVpnVvBvki0B-vp-dEvGV5pVnp-fvdkBdvpdvsBYvki0B- "vAvF5B-vhiTp-f"""
hackedMessage = hackAffine(ciphertext)
if hackedMessage == None:
print('Failed to hack the ciphertext.')
else:
print(hackedMessage)
if __name__ == '__main__':
main()