mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Merge pull request #230 from christianbender/master
improvement XOR_cipher.py
This commit is contained in:
commit
eb9fe88e16
@ -38,12 +38,7 @@ class XORCipher(object):
|
|||||||
# precondition
|
# precondition
|
||||||
assert (isinstance(key,int) and isinstance(content,str))
|
assert (isinstance(key,int) and isinstance(content,str))
|
||||||
|
|
||||||
# testing for default arguments
|
key = key or self.__key or 1
|
||||||
if (key == 0):
|
|
||||||
if (self.__key == 0):
|
|
||||||
key = 1
|
|
||||||
else:
|
|
||||||
key = self.__key
|
|
||||||
|
|
||||||
# make sure key can be any size
|
# make sure key can be any size
|
||||||
while (key > 255):
|
while (key > 255):
|
||||||
@ -51,7 +46,6 @@ class XORCipher(object):
|
|||||||
|
|
||||||
# This will be returned
|
# This will be returned
|
||||||
ans = []
|
ans = []
|
||||||
resultNumber = 0
|
|
||||||
|
|
||||||
for ch in content:
|
for ch in content:
|
||||||
ans.append(chr(ord(ch) ^ key))
|
ans.append(chr(ord(ch) ^ key))
|
||||||
@ -69,12 +63,7 @@ class XORCipher(object):
|
|||||||
# precondition
|
# precondition
|
||||||
assert (isinstance(key,int) and isinstance(content,list))
|
assert (isinstance(key,int) and isinstance(content,list))
|
||||||
|
|
||||||
# testing for default arguments
|
key = key or self.__key or 1
|
||||||
if (key == 0):
|
|
||||||
if (self.__key == 0):
|
|
||||||
key = 1
|
|
||||||
else:
|
|
||||||
key = self.__key
|
|
||||||
|
|
||||||
# make sure key can be any size
|
# make sure key can be any size
|
||||||
while (key > 255):
|
while (key > 255):
|
||||||
@ -82,7 +71,6 @@ class XORCipher(object):
|
|||||||
|
|
||||||
# This will be returned
|
# This will be returned
|
||||||
ans = []
|
ans = []
|
||||||
resultNumber = 0
|
|
||||||
|
|
||||||
for ch in content:
|
for ch in content:
|
||||||
ans.append(chr(ord(ch) ^ key))
|
ans.append(chr(ord(ch) ^ key))
|
||||||
@ -101,12 +89,7 @@ class XORCipher(object):
|
|||||||
# precondition
|
# precondition
|
||||||
assert (isinstance(key,int) and isinstance(content,str))
|
assert (isinstance(key,int) and isinstance(content,str))
|
||||||
|
|
||||||
# testing for default arguments
|
key = key or self.__key or 1
|
||||||
if (key == 0):
|
|
||||||
if (self.__key == 0):
|
|
||||||
key = 1
|
|
||||||
else:
|
|
||||||
key = self.__key
|
|
||||||
|
|
||||||
# make sure key can be any size
|
# make sure key can be any size
|
||||||
while (key > 255):
|
while (key > 255):
|
||||||
@ -114,7 +97,6 @@ class XORCipher(object):
|
|||||||
|
|
||||||
# This will be returned
|
# This will be returned
|
||||||
ans = ""
|
ans = ""
|
||||||
resultNumber = 0
|
|
||||||
|
|
||||||
for ch in content:
|
for ch in content:
|
||||||
ans += chr(ord(ch) ^ key)
|
ans += chr(ord(ch) ^ key)
|
||||||
@ -132,12 +114,7 @@ class XORCipher(object):
|
|||||||
# precondition
|
# precondition
|
||||||
assert (isinstance(key,int) and isinstance(content,str))
|
assert (isinstance(key,int) and isinstance(content,str))
|
||||||
|
|
||||||
# testing for default arguments
|
key = key or self.__key or 1
|
||||||
if (key == 0):
|
|
||||||
if (self.__key == 0):
|
|
||||||
key = 1
|
|
||||||
else:
|
|
||||||
key = self.__key
|
|
||||||
|
|
||||||
# make sure key can be any size
|
# make sure key can be any size
|
||||||
while (key > 255):
|
while (key > 255):
|
||||||
@ -145,7 +122,6 @@ class XORCipher(object):
|
|||||||
|
|
||||||
# This will be returned
|
# This will be returned
|
||||||
ans = ""
|
ans = ""
|
||||||
resultNumber = 0
|
|
||||||
|
|
||||||
for ch in content:
|
for ch in content:
|
||||||
ans += chr(ord(ch) ^ key)
|
ans += chr(ord(ch) ^ key)
|
||||||
@ -166,16 +142,13 @@ class XORCipher(object):
|
|||||||
assert (isinstance(file,str) and isinstance(key,int))
|
assert (isinstance(file,str) and isinstance(key,int))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fin = open(file,"r")
|
with open(file,"r") as fin:
|
||||||
fout = open("encrypt.out","w+")
|
with open("encrypt.out","w+") as fout:
|
||||||
|
|
||||||
# actual encrypt-process
|
# actual encrypt-process
|
||||||
for line in fin:
|
for line in fin:
|
||||||
fout.write(self.encrypt_string(line,key))
|
fout.write(self.encrypt_string(line,key))
|
||||||
|
|
||||||
fin.close()
|
|
||||||
fout.close()
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -195,16 +168,13 @@ class XORCipher(object):
|
|||||||
assert (isinstance(file,str) and isinstance(key,int))
|
assert (isinstance(file,str) and isinstance(key,int))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fin = open(file,"r")
|
with open(file,"r") as fin:
|
||||||
fout = open("decrypt.out","w+")
|
with open("decrypt.out","w+") as fout:
|
||||||
|
|
||||||
# actual encrypt-process
|
# actual encrypt-process
|
||||||
for line in fin:
|
for line in fin:
|
||||||
fout.write(self.decrypt_string(line,key))
|
fout.write(self.decrypt_string(line,key))
|
||||||
|
|
||||||
fin.close()
|
|
||||||
fout.close()
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -233,7 +203,7 @@ class XORCipher(object):
|
|||||||
# else:
|
# else:
|
||||||
# print "encrypt unsuccessful"
|
# print "encrypt unsuccessful"
|
||||||
|
|
||||||
# if (crypt.decrypt_file("a.out",key)):
|
# if (crypt.decrypt_file("encrypt.out",key)):
|
||||||
# print "decrypt successful"
|
# print "decrypt successful"
|
||||||
# else:
|
# else:
|
||||||
# print "decrypt unsuccessful"
|
# print "decrypt unsuccessful"
|
Loading…
Reference in New Issue
Block a user