Skip to content

improvement XOR_cipher.py #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 16 additions & 46 deletions ciphers/XOR_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,14 @@ def encrypt(self, content, key):
# precondition
assert (isinstance(key,int) and isinstance(content,str))

# testing for default arguments
if (key == 0):
if (self.__key == 0):
key = 1
else:
key = self.__key
key = key or self.__key or 1

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

# This will be returned
ans = []
resultNumber = 0

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

# testing for default arguments
if (key == 0):
if (self.__key == 0):
key = 1
else:
key = self.__key
key = key or self.__key or 1

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

# This will be returned
ans = []
resultNumber = 0

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

# testing for default arguments
if (key == 0):
if (self.__key == 0):
key = 1
else:
key = self.__key
key = key or self.__key or 1

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

# This will be returned
ans = ""
resultNumber = 0

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

# testing for default arguments
if (key == 0):
if (self.__key == 0):
key = 1
else:
key = self.__key
key = key or self.__key or 1

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

# This will be returned
ans = ""
resultNumber = 0


for ch in content:
ans += chr(ord(ch) ^ key)

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

try:
fin = open(file,"r")
fout = open("encrypt.out","w+")

# actual encrypt-process
for line in fin:
fout.write(self.encrypt_string(line,key))
with open(file,"r") as fin:
with open("encrypt.out","w+") as fout:

fin.close()
fout.close()
# actual encrypt-process
for line in fin:
fout.write(self.encrypt_string(line,key))

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

try:
fin = open(file,"r")
fout = open("decrypt.out","w+")

# actual encrypt-process
for line in fin:
fout.write(self.decrypt_string(line,key))
with open(file,"r") as fin:
with open("decrypt.out","w+") as fout:

fin.close()
fout.close()
# actual encrypt-process
for line in fin:
fout.write(self.decrypt_string(line,key))

except:
return False
Expand Down Expand Up @@ -233,7 +203,7 @@ def decrypt_file(self,file, key):
# else:
# print "encrypt unsuccessful"

# if (crypt.decrypt_file("a.out",key)):
# if (crypt.decrypt_file("encrypt.out",key)):
# print "decrypt successful"
# else:
# print "decrypt unsuccessful"