-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAffineCryptoSystem
76 lines (59 loc) · 2.29 KB
/
AffineCryptoSystem
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
# Sage Math Codes
# EmreOvunc
def AffineCryptoSystem():
A=AffineCryptosystem(AlphabeticStrings())
PlainText=A.encoding(Read_TextFile())
key1,key2=GenerateKeys(A)
Cipher=Encryption(A,PlainText,key1,key2)
Write_EncryptedText(Cipher)
EncryptText=A.encoding(Read_TextFile())
key1Inv,key2Inv=GenerateInverseKeys(A,key1,key2)
Write_DecryptedText(A,Decryption(A,key1Inv,key2Inv))
Ranking_None(A,Cipher)
Ranking_Chisquare(A,Cipher)
Ranking_Squared(A,Cipher)
def GenerateKeys(self):
a,b=self.random_key()
return a,b
def GenerateInverseKeys(self,a,b):
aInverse,bInverse=self.inverse_key(a,b)
return aInverse,bInverse
def Encryption(self,P,a,b):
E=self(a,b)
C=E(P)
return C
def Decryption(self,aInverse,bInverse):
D=self(aInverse,bInverse)
return D
def Read_TextFile():
with open("/home/EmreOvunc/Desktop/Message.txt", 'r') as TextFile:
lines=TextFile.read()
TextFile.close()
return lines
def Write_EncryptedText(Cipher):
with open("/home/EmreOvunc/Desktop/EncryptedMessage.txt", "w") as EncryptedText:
EncryptedText.write(str(Cipher))
EncryptedText.close()
def Write_DecryptedText(self,D):
with open('/home/EmreOvunc/Desktop/EncryptedMessage.txt', 'r') as ReadEncryptedFile:
line=ReadEncryptedFile.read()
line=self.encoding(line)
DecryptedText= open("/home/EmreOvunc/Desktop/DecryptedMessage.txt", "w")
DecryptedText.write(str(D(line)))
DecryptedText.close()
def Ranking_Squared(self,Cipher):
Rank = self.brute_force(Cipher, ranking="squared_differences")
with open("/home/EmreOvunc/Desktop/Ranking_Squared.txt", "w") as Ranking_Squared:
Ranking_Squared.write(str(Rank))
Ranking_Squared.close()
def Ranking_Chisquare(self,Cipher):
Rank = self.brute_force(Cipher, ranking="chisquare")
with open("/home/EmreOvunc/Desktop/Ranking_Chisquare.txt", "w") as Ranking_Chisquare:
Ranking_Chisquare.write(str(Rank))
Ranking_Chisquare.close()
def Ranking_None(self,Cipher):
L=self.brute_force(Cipher)
with open("/home/EmreOvunc/Desktop/Ranking_None.txt", "w") as Ranking_None:
Ranking_None.write(str(L))
Ranking_None.close()
AffineCryptoSystem()