-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaffineCipher_2.py
41 lines (40 loc) · 1.11 KB
/
affineCipher_2.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
# Affine Cipher (MOD 26 Version) :AAG
MOD = 26
while True:
b = int(input("Enter Affine Cipher Additive Key : "))
a = int(input("Enter Affine Cipher Multiplicative Key : "))
data = input("Enter Data: ").upper()
ans = []
print('''Enter a choice:
1: For encryption
2: For decryption''')
choice = int(input())
if choice==1:
for i in data:
ans.append(chr(((ord(i)-65)*a+b)%MOD +65))
elif choice==2:
for i in data:
for j in range(MOD):
if (a*j)%MOD==1: a_inverse=j
ans.append(chr((a_inverse*((ord(i)-65)+MOD-b))%MOD +65))
else:
print("Enter a valid choice")
print(''.join(ans))
''' Output
Enter Affine Cipher Additive Key : 20
Enter Affine Cipher Multiplicative Key : 17
Enter Data: AFFINECIPHER
Enter a choice:
1: For encryption
2: For decryption
1
UBBAHKCAPJKX
Enter Affine Cipher Additive Key : 20
Enter Affine Cipher Multiplicative Key : 17
Enter Data: UBBAHKCAPJKX
Enter a choice:
1: For encryption
2: For decryption
2
AFFINECIPHER
'''