Skip to content

Commit ecdfd99

Browse files
authored
Merge pull request TheAlgorithms#229 from christianbender/master
XOR cipher algorithm
2 parents 8bf6952 + ad265b9 commit ecdfd99

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed

ciphers/XOR_cipher.py

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
"""
2+
author: Christian Bender
3+
date: 21.12.2017
4+
class: XORCipher
5+
6+
This class implements the XOR-cipher algorithm and provides
7+
some useful methods for encrypting and decrypting strings and
8+
files.
9+
10+
Overview about methods
11+
12+
- encrypt : list of char
13+
- decrypt : list of char
14+
- encrypt_string : str
15+
- decrypt_string : str
16+
- encrypt_file : boolean
17+
- decrypt_file : boolean
18+
"""
19+
class XORCipher(object):
20+
21+
def __init__(self, key = 0):
22+
"""
23+
simple constructor that receives a key or uses
24+
default key = 0
25+
"""
26+
27+
#private field
28+
self.__key = key
29+
30+
def encrypt(self, content, key):
31+
"""
32+
input: 'content' of type string and 'key' of type int
33+
output: encrypted string 'content' as a list of chars
34+
if key not passed the method uses the key by the constructor.
35+
otherwise key = 1
36+
"""
37+
38+
# precondition
39+
assert (isinstance(key,int) and isinstance(content,str))
40+
41+
# testing for default arguments
42+
if (key == 0):
43+
if (self.__key == 0):
44+
key = 1
45+
else:
46+
key = self.__key
47+
48+
# make sure key can be any size
49+
while (key > 255):
50+
key -= 255
51+
52+
# This will be returned
53+
ans = []
54+
resultNumber = 0
55+
56+
for ch in content:
57+
ans.append(chr(ord(ch) ^ key))
58+
59+
return ans
60+
61+
def decrypt(self,content,key):
62+
"""
63+
input: 'content' of type list and 'key' of type int
64+
output: decrypted string 'content' as a list of chars
65+
if key not passed the method uses the key by the constructor.
66+
otherwise key = 1
67+
"""
68+
69+
# precondition
70+
assert (isinstance(key,int) and isinstance(content,list))
71+
72+
# testing for default arguments
73+
if (key == 0):
74+
if (self.__key == 0):
75+
key = 1
76+
else:
77+
key = self.__key
78+
79+
# make sure key can be any size
80+
while (key > 255):
81+
key -= 255
82+
83+
# This will be returned
84+
ans = []
85+
resultNumber = 0
86+
87+
for ch in content:
88+
ans.append(chr(ord(ch) ^ key))
89+
90+
return ans
91+
92+
93+
def encrypt_string(self,content, key = 0):
94+
"""
95+
input: 'content' of type string and 'key' of type int
96+
output: encrypted string 'content'
97+
if key not passed the method uses the key by the constructor.
98+
otherwise key = 1
99+
"""
100+
101+
# precondition
102+
assert (isinstance(key,int) and isinstance(content,str))
103+
104+
# testing for default arguments
105+
if (key == 0):
106+
if (self.__key == 0):
107+
key = 1
108+
else:
109+
key = self.__key
110+
111+
# make sure key can be any size
112+
while (key > 255):
113+
key -= 255
114+
115+
# This will be returned
116+
ans = ""
117+
resultNumber = 0
118+
119+
for ch in content:
120+
ans += chr(ord(ch) ^ key)
121+
122+
return ans
123+
124+
def decrypt_string(self,content,key = 0):
125+
"""
126+
input: 'content' of type string and 'key' of type int
127+
output: decrypted string 'content'
128+
if key not passed the method uses the key by the constructor.
129+
otherwise key = 1
130+
"""
131+
132+
# precondition
133+
assert (isinstance(key,int) and isinstance(content,str))
134+
135+
# testing for default arguments
136+
if (key == 0):
137+
if (self.__key == 0):
138+
key = 1
139+
else:
140+
key = self.__key
141+
142+
# make sure key can be any size
143+
while (key > 255):
144+
key -= 255
145+
146+
# This will be returned
147+
ans = ""
148+
resultNumber = 0
149+
150+
for ch in content:
151+
ans += chr(ord(ch) ^ key)
152+
153+
return ans
154+
155+
156+
def encrypt_file(self, file, key = 0):
157+
"""
158+
input: filename (str) and a key (int)
159+
output: returns true if encrypt process was
160+
successful otherwise false
161+
if key not passed the method uses the key by the constructor.
162+
otherwise key = 1
163+
"""
164+
165+
#precondition
166+
assert (isinstance(file,str) and isinstance(key,int))
167+
168+
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))
175+
176+
fin.close()
177+
fout.close()
178+
179+
except:
180+
return False
181+
182+
return True
183+
184+
185+
def decrypt_file(self,file, key):
186+
"""
187+
input: filename (str) and a key (int)
188+
output: returns true if decrypt process was
189+
successful otherwise false
190+
if key not passed the method uses the key by the constructor.
191+
otherwise key = 1
192+
"""
193+
194+
#precondition
195+
assert (isinstance(file,str) and isinstance(key,int))
196+
197+
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))
204+
205+
fin.close()
206+
fout.close()
207+
208+
except:
209+
return False
210+
211+
return True
212+
213+
214+
215+
216+
# Tests
217+
# crypt = XORCipher()
218+
# key = 67
219+
220+
# # test enrcypt
221+
# print crypt.encrypt("hallo welt",key)
222+
# # test decrypt
223+
# print crypt.decrypt(crypt.encrypt("hallo welt",key), key)
224+
225+
# # test encrypt_string
226+
# print crypt.encrypt_string("hallo welt",key)
227+
228+
# # test decrypt_string
229+
# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)
230+
231+
# if (crypt.encrypt_file("test.txt",key)):
232+
# print "encrypt successful"
233+
# else:
234+
# print "encrypt unsuccessful"
235+
236+
# if (crypt.decrypt_file("a.out",key)):
237+
# print "decrypt successful"
238+
# else:
239+
# print "decrypt unsuccessful"

0 commit comments

Comments
 (0)