Python/ciphers/xor_cipher.py

206 lines
4.8 KiB
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
"""
author: Christian Bender
date: 21.12.2017
class: XORCipher
This class implements the XOR-cipher algorithm and provides
some useful methods for encrypting and decrypting strings and
files.
Overview about methods
- encrypt : list of char
- decrypt : list of char
- encrypt_string : str
- decrypt_string : str
- encrypt_file : boolean
- decrypt_file : boolean
"""
2019-10-05 05:14:13 +00:00
class XORCipher(object):
def __init__(self, key=0):
"""
2018-10-19 12:48:28 +00:00
simple constructor that receives a key or uses
default key = 0
"""
2019-10-05 05:14:13 +00:00
# private field
self.__key = key
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def encrypt(self, content, key):
"""
2018-10-19 12:48:28 +00:00
input: 'content' of type string and 'key' of type int
output: encrypted string 'content' as a list of chars
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
2019-10-05 05:14:13 +00:00
# precondition
assert isinstance(key, int) and isinstance(content, str)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
key = key or self.__key or 1
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# make sure key can be any size
while key > 255:
key -= 255
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# This will be returned
ans = []
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
for ch in content:
ans.append(chr(ord(ch) ^ key))
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
return ans
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def decrypt(self, content, key):
"""
2018-10-19 12:48:28 +00:00
input: 'content' of type list and 'key' of type int
output: decrypted string 'content' as a list of chars
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
2019-10-05 05:14:13 +00:00
# precondition
assert isinstance(key, int) and isinstance(content, list)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
key = key or self.__key or 1
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# make sure key can be any size
while key > 255:
key -= 255
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# This will be returned
ans = []
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
for ch in content:
ans.append(chr(ord(ch) ^ key))
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
return ans
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def encrypt_string(self, content, key=0):
"""
2018-10-19 12:48:28 +00:00
input: 'content' of type string and 'key' of type int
output: encrypted string 'content'
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
2019-10-05 05:14:13 +00:00
# precondition
assert isinstance(key, int) and isinstance(content, str)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
key = key or self.__key or 1
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# make sure key can be any size
while key > 255:
key -= 255
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# This will be returned
ans = ""
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
for ch in content:
ans += chr(ord(ch) ^ key)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
return ans
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def decrypt_string(self, content, key=0):
"""
2018-10-19 12:48:28 +00:00
input: 'content' of type string and 'key' of type int
output: decrypted string 'content'
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
2019-10-05 05:14:13 +00:00
# precondition
assert isinstance(key, int) and isinstance(content, str)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
key = key or self.__key or 1
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# make sure key can be any size
while key > 255:
key -= 255
2019-10-05 05:14:13 +00:00
# This will be returned
ans = ""
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
for ch in content:
ans += chr(ord(ch) ^ key)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
return ans
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def encrypt_file(self, file, key=0):
"""
2018-10-19 12:48:28 +00:00
input: filename (str) and a key (int)
output: returns true if encrypt process was
successful otherwise false
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
2019-10-05 05:14:13 +00:00
# precondition
assert isinstance(file, str) and isinstance(key, int)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
try:
with open(file, "r") as fin:
with open("encrypt.out", "w+") as fout:
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# actual encrypt-process
for line in fin:
fout.write(self.encrypt_string(line, key))
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
except:
return False
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
return True
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
def decrypt_file(self, file, key):
"""
2018-10-19 12:48:28 +00:00
input: filename (str) and a key (int)
output: returns true if decrypt process was
successful otherwise false
if key not passed the method uses the key by the constructor.
otherwise key = 1
"""
2019-10-05 05:14:13 +00:00
# precondition
assert isinstance(file, str) and isinstance(key, int)
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
try:
with open(file, "r") as fin:
with open("decrypt.out", "w+") as fout:
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
# actual encrypt-process
for line in fin:
fout.write(self.decrypt_string(line, key))
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
except:
return False
2018-10-19 12:48:28 +00:00
2019-10-05 05:14:13 +00:00
return True
2018-10-19 12:48:28 +00:00
# Tests
# crypt = XORCipher()
# key = 67
# # test enrcypt
# print(crypt.encrypt("hallo welt",key))
2018-10-19 12:48:28 +00:00
# # test decrypt
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))
2018-10-19 12:48:28 +00:00
# # test encrypt_string
# print(crypt.encrypt_string("hallo welt",key))
2018-10-19 12:48:28 +00:00
# # test decrypt_string
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))
2018-10-19 12:48:28 +00:00
# if (crypt.encrypt_file("test.txt",key)):
# print("encrypt successful")
2018-10-19 12:48:28 +00:00
# else:
# print("encrypt unsuccessful")
2018-10-19 12:48:28 +00:00
# if (crypt.decrypt_file("encrypt.out",key)):
# print("decrypt successful")
2018-10-19 12:48:28 +00:00
# else:
# print("decrypt unsuccessful")