From b25bc2cb30644276eb7415dcfdb0edce4a838310 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Fri, 22 Dec 2017 17:30:11 +0100 Subject: [PATCH 1/2] improvement I improve my code --- ciphers/XOR_cipher.py | 96 +++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 63 deletions(-) diff --git a/ciphers/XOR_cipher.py b/ciphers/XOR_cipher.py index daa014d98..1fe00a870 100644 --- a/ciphers/XOR_cipher.py +++ b/ciphers/XOR_cipher.py @@ -38,12 +38,7 @@ class XORCipher(object): # 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): @@ -51,7 +46,6 @@ class XORCipher(object): # This will be returned ans = [] - resultNumber = 0 for ch in content: ans.append(chr(ord(ch) ^ key)) @@ -69,12 +63,7 @@ class XORCipher(object): # 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): @@ -82,7 +71,6 @@ class XORCipher(object): # This will be returned ans = [] - resultNumber = 0 for ch in content: ans.append(chr(ord(ch) ^ key)) @@ -101,12 +89,7 @@ class XORCipher(object): # 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): @@ -114,7 +97,6 @@ class XORCipher(object): # This will be returned ans = "" - resultNumber = 0 for ch in content: ans += chr(ord(ch) ^ key) @@ -132,12 +114,7 @@ class XORCipher(object): # 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): @@ -145,8 +122,7 @@ class XORCipher(object): # This will be returned ans = "" - resultNumber = 0 - + for ch in content: ans += chr(ord(ch) ^ key) @@ -166,15 +142,12 @@ class XORCipher(object): assert (isinstance(file,str) and isinstance(key,int)) try: - fin = open(file,"r") - fout = open("encrypt.out","w+") + with open(file,"r") as fin: + with open("encrypt.out","w+") as fout: - # actual encrypt-process - for line in fin: - fout.write(self.encrypt_string(line,key)) - - fin.close() - fout.close() + # actual encrypt-process + for line in fin: + fout.write(self.encrypt_string(line,key)) except: return False @@ -195,15 +168,12 @@ class XORCipher(object): assert (isinstance(file,str) and isinstance(key,int)) try: - fin = open(file,"r") - fout = open("decrypt.out","w+") + with open(file,"r") as fin: + with open("decrypt.out","w+") as fout: - # actual encrypt-process - for line in fin: - fout.write(self.decrypt_string(line,key)) - - fin.close() - fout.close() + # actual encrypt-process + for line in fin: + fout.write(self.decrypt_string(line,key)) except: return False @@ -214,26 +184,26 @@ class XORCipher(object): # Tests -# crypt = XORCipher() -# key = 67 +crypt = XORCipher() +key = 67 -# # test enrcypt -# print crypt.encrypt("hallo welt",key) -# # test decrypt -# print crypt.decrypt(crypt.encrypt("hallo welt",key), key) +# test enrcypt +print crypt.encrypt("hallo welt",key) +# test decrypt +print crypt.decrypt(crypt.encrypt("hallo welt",key), key) -# # test encrypt_string -# print crypt.encrypt_string("hallo welt",key) +# test encrypt_string +print crypt.encrypt_string("hallo welt",key) -# # test decrypt_string -# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key) +# test decrypt_string +print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key) -# if (crypt.encrypt_file("test.txt",key)): -# print "encrypt successful" -# else: -# print "encrypt unsuccessful" +if (crypt.encrypt_file("test.txt",key)): + print "encrypt successful" +else: + print "encrypt unsuccessful" -# if (crypt.decrypt_file("a.out",key)): -# print "decrypt successful" -# else: -# print "decrypt unsuccessful" \ No newline at end of file +if (crypt.decrypt_file("encrypt.out",key)): + print "decrypt successful" +else: + print "decrypt unsuccessful" \ No newline at end of file From 0e7d4483ca18786ab45388fb2f191cb2567f3705 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Fri, 22 Dec 2017 17:33:09 +0100 Subject: [PATCH 2/2] comment out I comment out some code lines in the test-section --- ciphers/XOR_cipher.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ciphers/XOR_cipher.py b/ciphers/XOR_cipher.py index 1fe00a870..727fac3b0 100644 --- a/ciphers/XOR_cipher.py +++ b/ciphers/XOR_cipher.py @@ -184,26 +184,26 @@ class XORCipher(object): # Tests -crypt = XORCipher() -key = 67 +# crypt = XORCipher() +# key = 67 -# test enrcypt -print crypt.encrypt("hallo welt",key) -# test decrypt -print crypt.decrypt(crypt.encrypt("hallo welt",key), key) +# # test enrcypt +# print crypt.encrypt("hallo welt",key) +# # test decrypt +# print crypt.decrypt(crypt.encrypt("hallo welt",key), key) -# test encrypt_string -print crypt.encrypt_string("hallo welt",key) +# # test encrypt_string +# print crypt.encrypt_string("hallo welt",key) -# test decrypt_string -print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key) +# # test decrypt_string +# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key) -if (crypt.encrypt_file("test.txt",key)): - print "encrypt successful" -else: - print "encrypt unsuccessful" +# if (crypt.encrypt_file("test.txt",key)): +# print "encrypt successful" +# else: +# print "encrypt unsuccessful" -if (crypt.decrypt_file("encrypt.out",key)): - print "decrypt successful" -else: - print "decrypt unsuccessful" \ No newline at end of file +# if (crypt.decrypt_file("encrypt.out",key)): +# print "decrypt successful" +# else: +# print "decrypt unsuccessful" \ No newline at end of file