-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython Code
More file actions
243 lines (221 loc) · 7.81 KB
/
Copy pathpython Code
File metadata and controls
243 lines (221 loc) · 7.81 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
turn= 'yes'
while turn == 'yes':
file = input("Would you want to creat a new file, or use an old one. new:0 old:1")
joined="anything"
lst=["0","1"]
while file not in lst:
file = input("invalid input try again")
if file == "0":
filename = open('Original.txt','w')
writing = input(" Type the input you want")
filename.write(writing)
filename.close()
filename= "Original.txt"
elif file=="1":
filename=input("enter the filename").title()+ ".txt"
order1=input("Do you want to Encrypt or Decrypt your file. for Encrypt:0 , for Decrypt: 1, count:2")
##########
def Ceaser (filename,wheresave,num,joined):
f = open(filename,'r')
answer = open(wheresave,'w')
string =""
for i in f:
x=i.split()
n=0
for j in x:
string+=(" ")
n+=1
m=0
for z in j:
if z.isupper():
Q=chr(65+((ord(z)+(num-65))%26))
else:
Q=chr(97+((ord(z)+(num-97))%26))
string+=(Q)
m+=1
if n == len(x)and m == (len(j)):
string+=("\n")
answer.write(string)
if joined != "false":
if num>0:
print ("encrypted text:",'\n',string)
else:
print ("dicrypt text:",'\n',string)
f.close()
answer.close()
return (answer)
###############
def indexLocator (char,cipherKeyMatrix):
indexOfChar = []
if char == "J":
char = "I"
for i,j in enumerate(cipherKeyMatrix):
for k,l in enumerate(j):
if char == l:
indexOfChar.append(i)
indexOfChar.append(k)
return indexOfChar
#############################
def playfair (f,wheresave,n,key,joined):
m = open(filename,'r')
line = m.readlines()
new = ' '.join([str(i) for i in line])
f = new.replace(" ","").upper()
lng=f.replace("\n","")
f=lng
answer = open(wheresave,'w')
string=""
for s in range(0,len(f)+1,2):
if s<len(f)-1:
if f[s] == f[s+1]:
f = f[:s+1]+'X'+f[s+1:]
if len(f)%2 != 0:
f = f[:]+'Q'
matrix_5x5 = [[0 for i in range (5)] for j in range(5)]
simpleKeyArr = []
for c in key:
if c not in simpleKeyArr:
if c == 'J':
simpleKeyArr.append('I')
else:
simpleKeyArr.append(c)
is_I_exist = "I" in simpleKeyArr
for i in range(65,91):
if chr(i) not in simpleKeyArr:
if i == 73 and not is_I_exist:
simpleKeyArr.append("I")
is_I_exist = True
elif i == 73 or i == 74 and is_I_exist:
pass
else:
simpleKeyArr.append(chr(i))
index = 0
for i in range(0,5):
for j in range(0,5):
matrix_5x5[i][j] = simpleKeyArr[index]
index += 1
keyMatrix = matrix_5x5
i = 0
while i < len(f):
n1 = indexLocator(f[i],keyMatrix)
n2 = indexLocator(f[i+1],keyMatrix)
if n1[1] == n2[1]:
i1 = (n1[0] + n) % 5
j1 = n1[1]
i2 = (n2[0] + n) % 5
j2 = n2[1]
string+=(keyMatrix[i1][j1]+keyMatrix[i2][j2])
elif n1[0] == n2[0]:
i1 = n1[0]
j1 = (n1[1] + n) % 5
i2 = n2[0]
j2 = (n2[1] + n) % 5
string+=(keyMatrix[i1][j1]+keyMatrix[i2][j2])
else:
i1 = n1[0]
j1 = n1[1]
i2 = n2[0]
j2 = n2[1]
string+=(keyMatrix[i1][j2]+keyMatrix[i2][j1])
i += 2
if n==-1:
newstring=string
for i in range(len(string)-2):
i+=1
if string[i]=="X" and string[i-1]==string[i+1]:
newstring=string[:i]+string[i+1:]
if string[-1]=="Q":
newstring=newstring[:len(newstring)-1]
answer.write(newstring)
else:
answer.write(string)
m.close()
answer.close()
if joined != "true":
if n==-1:
print("Decrypted text:",'\n',newstring)
else:
print("encrypted text:",'\n',string)
return (answer)
###############
def counter (filename):
f = open(filename,'r')
list2 = []
count = 0
letter = 0
chara = 0
num = 0
list = [0 for i in range(26)]
line = f.readlines()
numlines = len(line)
print(line)
for i in line:
x = i.split()
o = len(x)-1
num = num+o
for j in x:
count += 1
list2.append(" ")
for z in j:
chara += 1
if z.isalpha():
letter += 1
z = z.upper()
for m in range(26):
if (m+65) == ord(z):
list[m] += 1
f.close()
print("number of lines:",numlines)
print("number of words:",count)
print("number of characters",chara+num)
print("number of letters:",letter)
print()
for i in range(26):
print ("The letter",chr(i+65),"occured",list[i])
return (chara)
###############
lst.append("2")
while order1 not in lst:
order1 = input("invalid input try again")
if order1 == '0':
type1 = input("Enter type of Encrypt Ceaser:0 , Playfair:1, joined:2")
while type1 not in lst:
type1=input("invalid input try again")
wheresave='Ciphered.txt'
n=1
if type1 == '0':
num = int(input("enter the ciphered shift"))
Encrypt = Ceaser(filename,wheresave,num,joined)
elif type1 =='1':
key = input("enter the ciphere key").replace(" ","").upper()
Encrypt = playfair (filename,wheresave,n,key,joined)
elif type1 =='2':
joined= "false"
num = int(input("enter the ciphered shift"))
key = input("enter the ciphere key").replace(" ","").upper()
Encrypt = Ceaser(filename,wheresave,num,joined)
filename='Ciphered.txt'
Encrypt = playfair (filename,wheresave,n,key,joined)
elif order1 == '1':
type2 = input("Enter type of Decrypt Ceaser:0 , Playfair:1, joined:2")
while type2 not in lst:
type2=input("invalid input try again")
wheresave='Deciphered.txt'
n=-1
if type2 == '0':
num = int(input("enter the ciphered shift"))*(-1)
Decrypt = Ceaser(filename,wheresave,num,joined)
elif type2 == '1':
key = input("enter the ciphere key").replace(" ","").upper()
Decrypt = playfair (filename,wheresave,n,key,joined)
elif type2 =='2':
joined = "true"
num = int(input("enter the ciphered shift"))*(-1)
key = input("enter the ciphere key").replace(" ","").upper()
Decrypt = playfair (filename,wheresave,n,key,joined)
filename='Deciphered.txt'
Decrypt = Ceaser(filename,wheresave,num,joined)
elif order1 == '2':
count = counter(filename)
turn = input("would you like to continue , type 'yes'").lower()
print ("thank you for using my code")