-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffineCipher.py
52 lines (43 loc) · 1.23 KB
/
AffineCipher.py
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
# find extended euclidean algorithm (Created by GitHub Coplit)
def gcd(a, b):
if a == 0:
return b, 0, 1
else:
g, y, x = gcd(b % a, a)
return g, x - (b // a) * y, y
# find modular inverse
def modinv(a, m):
g, x, y = gcd(a, m)
if g != 1:
return -1
else:
return x % m
# Create a-z list.
alphabet = []
for i in range(97, 123):
alphabet.append(chr(i))
def encrypt(plaintext, a = 3, b = 5, encrypt = True):
cipher = ""
for char in plaintext:
if char not in alphabet:
cipher += char
continue
if encrypt == True:
cipher += alphabet[(a * alphabet.index(char) + b) % 26]
else:
cipher += alphabet[(modinv(a, 26) * (alphabet.index(char) - b)) % 26]
return cipher
def decrypt(ciphertext, a = 3, b = 5):
return encrypt(ciphertext, a, b, False)
def attack(cipher):
for a in range(26):
for b in range(26):
print(a, b, decrypt(cipher, a, b))
# if decrypt(cipher, a, b) == "Hello World!, this is Umer":
# print(a, b, decrypt(cipher, a, b))
text = "Hello World!, this is Umer"
cipher = encrypt(text)
print(cipher)
print()
print(decrypt(cipher))
#attack(cipher)