@@ -35,6 +35,22 @@ def encrypt(self, content: str, key: int) -> list[str]:
35
35
output: encrypted string 'content' as a list of chars
36
36
if key not passed the method uses the key by the constructor.
37
37
otherwise key = 1
38
+
39
+ Empty list
40
+ >>> XORCipher().encrypt("", 5)
41
+ []
42
+
43
+ One key
44
+ >>> XORCipher().encrypt("hallo welt", 1)
45
+ ['i', '`', 'm', 'm', 'n', '!', 'v', 'd', 'm', 'u']
46
+
47
+ Normal key
48
+ >>> XORCipher().encrypt("HALLO WELT", 32)
49
+ ['h', 'a', 'l', 'l', 'o', '\\ x00', 'w', 'e', 'l', 't']
50
+
51
+ Key greater than 255
52
+ >>> XORCipher().encrypt("hallo welt", 256)
53
+ ['h', 'a', 'l', 'l', 'o', ' ', 'w', 'e', 'l', 't']
38
54
"""
39
55
40
56
# precondition
@@ -44,7 +60,7 @@ def encrypt(self, content: str, key: int) -> list[str]:
44
60
key = key or self .__key or 1
45
61
46
62
# make sure key is an appropriate size
47
- key %= 255
63
+ key %= 256
48
64
49
65
return [chr (ord (ch ) ^ key ) for ch in content ]
50
66
@@ -54,16 +70,32 @@ def decrypt(self, content: str, key: int) -> list[str]:
54
70
output: decrypted string 'content' as a list of chars
55
71
if key not passed the method uses the key by the constructor.
56
72
otherwise key = 1
73
+
74
+ Empty list
75
+ >>> XORCipher().decrypt("", 5)
76
+ []
77
+
78
+ One key
79
+ >>> XORCipher().decrypt("hallo welt", 1)
80
+ ['i', '`', 'm', 'm', 'n', '!', 'v', 'd', 'm', 'u']
81
+
82
+ Normal key
83
+ >>> XORCipher().decrypt("HALLO WELT", 32)
84
+ ['h', 'a', 'l', 'l', 'o', '\\ x00', 'w', 'e', 'l', 't']
85
+
86
+ Key greater than 255
87
+ >>> XORCipher().decrypt("hallo welt", 256)
88
+ ['h', 'a', 'l', 'l', 'o', ' ', 'w', 'e', 'l', 't']
57
89
"""
58
90
59
91
# precondition
60
92
assert isinstance (key , int )
61
- assert isinstance (content , list )
93
+ assert isinstance (content , str )
62
94
63
95
key = key or self .__key or 1
64
96
65
97
# make sure key is an appropriate size
66
- key %= 255
98
+ key %= 256
67
99
68
100
return [chr (ord (ch ) ^ key ) for ch in content ]
69
101
@@ -73,6 +105,22 @@ def encrypt_string(self, content: str, key: int = 0) -> str:
73
105
output: encrypted string 'content'
74
106
if key not passed the method uses the key by the constructor.
75
107
otherwise key = 1
108
+
109
+ Empty list
110
+ >>> XORCipher().encrypt_string("", 5)
111
+ ''
112
+
113
+ One key
114
+ >>> XORCipher().encrypt_string("hallo welt", 1)
115
+ 'i`mmn!vdmu'
116
+
117
+ Normal key
118
+ >>> XORCipher().encrypt_string("HALLO WELT", 32)
119
+ 'hallo\\ x00welt'
120
+
121
+ Key greater than 255
122
+ >>> XORCipher().encrypt_string("hallo welt", 256)
123
+ 'hallo welt'
76
124
"""
77
125
78
126
# precondition
@@ -81,9 +129,8 @@ def encrypt_string(self, content: str, key: int = 0) -> str:
81
129
82
130
key = key or self .__key or 1
83
131
84
- # make sure key can be any size
85
- while key > 255 :
86
- key -= 255
132
+ # make sure key is an appropriate size
133
+ key %= 256
87
134
88
135
# This will be returned
89
136
ans = ""
@@ -99,6 +146,22 @@ def decrypt_string(self, content: str, key: int = 0) -> str:
99
146
output: decrypted string 'content'
100
147
if key not passed the method uses the key by the constructor.
101
148
otherwise key = 1
149
+
150
+ Empty list
151
+ >>> XORCipher().decrypt_string("", 5)
152
+ ''
153
+
154
+ One key
155
+ >>> XORCipher().decrypt_string("hallo welt", 1)
156
+ 'i`mmn!vdmu'
157
+
158
+ Normal key
159
+ >>> XORCipher().decrypt_string("HALLO WELT", 32)
160
+ 'hallo\\ x00welt'
161
+
162
+ Key greater than 255
163
+ >>> XORCipher().decrypt_string("hallo welt", 256)
164
+ 'hallo welt'
102
165
"""
103
166
104
167
# precondition
@@ -107,9 +170,8 @@ def decrypt_string(self, content: str, key: int = 0) -> str:
107
170
108
171
key = key or self .__key or 1
109
172
110
- # make sure key can be any size
111
- while key > 255 :
112
- key -= 255
173
+ # make sure key is an appropriate size
174
+ key %= 256
113
175
114
176
# This will be returned
115
177
ans = ""
@@ -132,6 +194,9 @@ def encrypt_file(self, file: str, key: int = 0) -> bool:
132
194
assert isinstance (file , str )
133
195
assert isinstance (key , int )
134
196
197
+ # make sure key is an appropriate size
198
+ key %= 256
199
+
135
200
try :
136
201
with open (file ) as fin , open ("encrypt.out" , "w+" ) as fout :
137
202
# actual encrypt-process
@@ -156,6 +221,9 @@ def decrypt_file(self, file: str, key: int) -> bool:
156
221
assert isinstance (file , str )
157
222
assert isinstance (key , int )
158
223
224
+ # make sure key is an appropriate size
225
+ key %= 256
226
+
159
227
try :
160
228
with open (file ) as fin , open ("decrypt.out" , "w+" ) as fout :
161
229
# actual encrypt-process
@@ -168,6 +236,11 @@ def decrypt_file(self, file: str, key: int) -> bool:
168
236
return True
169
237
170
238
239
+ if __name__ == "__main__" :
240
+ from doctest import testmod
241
+
242
+ testmod ()
243
+
171
244
# Tests
172
245
# crypt = XORCipher()
173
246
# key = 67
0 commit comments