-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAffineAlgorithm.py
More file actions
64 lines (56 loc) · 1.72 KB
/
AffineAlgorithm.py
File metadata and controls
64 lines (56 loc) · 1.72 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
letters = "abcçdefgğhıijklmnoöprsştuüvyz"
a = 0
b = 0
enteredText = ""
def TakeKeys():
global a,b
a = input("Please enter the a variable : ")
while not a.isdigit():
a = input("Incorrect entry. Please enter the a variable : ")
b = input("Please enter the b variable : ")
while not a.isdigit():
a = input("Incorrect entry. Please enter the b variable : ")
def TakeText():
global enteredText
enteredText = input("Enter a text : ")
for l in enteredText:
if((l in letters) == False):
print("Incorrect entry. Please enter a text : ")
TakeText()
break
def FindZValue(a):
for i in range(len(letters)):
if( (a * i) % len(letters) == 1):
return i
def CryptText(a,b,text):
global letters
encrypted = ""
for l in text :
cryptedLetterIndex = (a * letters.index(l) + b)
if(cryptedLetterIndex >= 29):
cryptedLetterIndex = cryptedLetterIndex % len(letters)
encrypted += letters[cryptedLetterIndex]
print("\n\nCRYPTED TEXT : ", encrypted, "\n\n")
def DecryptText(a,b,text):
global letters
decrypted = ""
for l in text :
z = FindZValue(a)
decrypted += letters[(z * (letters.index(l) - b)) % len(letters)]
print("\n\nDECRYPTED TEXT : ", decrypted, "\n\n")
while(True):
print("1) Crypt a text")
print("2) Decrypt a text")
print("3) Exit")
print("Please enter the action number : ")
action = input()
if(action == '1'):
TakeKeys()
TakeText()
CryptText(int(a),int(b),enteredText)
if(action == '2'):
TakeKeys()
TakeText()
DecryptText(int(a),int(b),enteredText)
if(action == '3'):
break