Skip to content

Commit eb9fe88

Browse files
authored
Merge pull request TheAlgorithms#230 from christianbender/master
improvement XOR_cipher.py
2 parents ecdfd99 + 0e7d448 commit eb9fe88

File tree

1 file changed

+16
-46
lines changed

1 file changed

+16
-46
lines changed

ciphers/XOR_cipher.py

+16-46
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,14 @@ def encrypt(self, content, key):
3838
# precondition
3939
assert (isinstance(key,int) and isinstance(content,str))
4040

41-
# testing for default arguments
42-
if (key == 0):
43-
if (self.__key == 0):
44-
key = 1
45-
else:
46-
key = self.__key
41+
key = key or self.__key or 1
4742

4843
# make sure key can be any size
4944
while (key > 255):
5045
key -= 255
5146

5247
# This will be returned
5348
ans = []
54-
resultNumber = 0
5549

5650
for ch in content:
5751
ans.append(chr(ord(ch) ^ key))
@@ -69,20 +63,14 @@ def decrypt(self,content,key):
6963
# precondition
7064
assert (isinstance(key,int) and isinstance(content,list))
7165

72-
# testing for default arguments
73-
if (key == 0):
74-
if (self.__key == 0):
75-
key = 1
76-
else:
77-
key = self.__key
66+
key = key or self.__key or 1
7867

7968
# make sure key can be any size
8069
while (key > 255):
8170
key -= 255
8271

8372
# This will be returned
8473
ans = []
85-
resultNumber = 0
8674

8775
for ch in content:
8876
ans.append(chr(ord(ch) ^ key))
@@ -101,20 +89,14 @@ def encrypt_string(self,content, key = 0):
10189
# precondition
10290
assert (isinstance(key,int) and isinstance(content,str))
10391

104-
# testing for default arguments
105-
if (key == 0):
106-
if (self.__key == 0):
107-
key = 1
108-
else:
109-
key = self.__key
92+
key = key or self.__key or 1
11093

11194
# make sure key can be any size
11295
while (key > 255):
11396
key -= 255
11497

11598
# This will be returned
11699
ans = ""
117-
resultNumber = 0
118100

119101
for ch in content:
120102
ans += chr(ord(ch) ^ key)
@@ -132,21 +114,15 @@ def decrypt_string(self,content,key = 0):
132114
# precondition
133115
assert (isinstance(key,int) and isinstance(content,str))
134116

135-
# testing for default arguments
136-
if (key == 0):
137-
if (self.__key == 0):
138-
key = 1
139-
else:
140-
key = self.__key
117+
key = key or self.__key or 1
141118

142119
# make sure key can be any size
143120
while (key > 255):
144121
key -= 255
145122

146123
# This will be returned
147124
ans = ""
148-
resultNumber = 0
149-
125+
150126
for ch in content:
151127
ans += chr(ord(ch) ^ key)
152128

@@ -166,15 +142,12 @@ def encrypt_file(self, file, key = 0):
166142
assert (isinstance(file,str) and isinstance(key,int))
167143

168144
try:
169-
fin = open(file,"r")
170-
fout = open("encrypt.out","w+")
171-
172-
# actual encrypt-process
173-
for line in fin:
174-
fout.write(self.encrypt_string(line,key))
145+
with open(file,"r") as fin:
146+
with open("encrypt.out","w+") as fout:
175147

176-
fin.close()
177-
fout.close()
148+
# actual encrypt-process
149+
for line in fin:
150+
fout.write(self.encrypt_string(line,key))
178151

179152
except:
180153
return False
@@ -195,15 +168,12 @@ def decrypt_file(self,file, key):
195168
assert (isinstance(file,str) and isinstance(key,int))
196169

197170
try:
198-
fin = open(file,"r")
199-
fout = open("decrypt.out","w+")
200-
201-
# actual encrypt-process
202-
for line in fin:
203-
fout.write(self.decrypt_string(line,key))
171+
with open(file,"r") as fin:
172+
with open("decrypt.out","w+") as fout:
204173

205-
fin.close()
206-
fout.close()
174+
# actual encrypt-process
175+
for line in fin:
176+
fout.write(self.decrypt_string(line,key))
207177

208178
except:
209179
return False
@@ -233,7 +203,7 @@ def decrypt_file(self,file, key):
233203
# else:
234204
# print "encrypt unsuccessful"
235205

236-
# if (crypt.decrypt_file("a.out",key)):
206+
# if (crypt.decrypt_file("encrypt.out",key)):
237207
# print "decrypt successful"
238208
# else:
239209
# print "decrypt unsuccessful"

0 commit comments

Comments
 (0)