@@ -38,20 +38,14 @@ def encrypt(self, content, key):
38
38
# precondition
39
39
assert (isinstance (key ,int ) and isinstance (content ,str ))
40
40
41
- # testing for default arguments
42
- if (key == 0 ):
43
- if (self .__key == 0 ):
44
- key = 1
45
- else :
46
- key = self .__key
41
+ key = key or self .__key or 1
47
42
48
43
# make sure key can be any size
49
44
while (key > 255 ):
50
45
key -= 255
51
46
52
47
# This will be returned
53
48
ans = []
54
- resultNumber = 0
55
49
56
50
for ch in content :
57
51
ans .append (chr (ord (ch ) ^ key ))
@@ -69,20 +63,14 @@ def decrypt(self,content,key):
69
63
# precondition
70
64
assert (isinstance (key ,int ) and isinstance (content ,list ))
71
65
72
- # testing for default arguments
73
- if (key == 0 ):
74
- if (self .__key == 0 ):
75
- key = 1
76
- else :
77
- key = self .__key
66
+ key = key or self .__key or 1
78
67
79
68
# make sure key can be any size
80
69
while (key > 255 ):
81
70
key -= 255
82
71
83
72
# This will be returned
84
73
ans = []
85
- resultNumber = 0
86
74
87
75
for ch in content :
88
76
ans .append (chr (ord (ch ) ^ key ))
@@ -101,20 +89,14 @@ def encrypt_string(self,content, key = 0):
101
89
# precondition
102
90
assert (isinstance (key ,int ) and isinstance (content ,str ))
103
91
104
- # testing for default arguments
105
- if (key == 0 ):
106
- if (self .__key == 0 ):
107
- key = 1
108
- else :
109
- key = self .__key
92
+ key = key or self .__key or 1
110
93
111
94
# make sure key can be any size
112
95
while (key > 255 ):
113
96
key -= 255
114
97
115
98
# This will be returned
116
99
ans = ""
117
- resultNumber = 0
118
100
119
101
for ch in content :
120
102
ans += chr (ord (ch ) ^ key )
@@ -132,21 +114,15 @@ def decrypt_string(self,content,key = 0):
132
114
# precondition
133
115
assert (isinstance (key ,int ) and isinstance (content ,str ))
134
116
135
- # testing for default arguments
136
- if (key == 0 ):
137
- if (self .__key == 0 ):
138
- key = 1
139
- else :
140
- key = self .__key
117
+ key = key or self .__key or 1
141
118
142
119
# make sure key can be any size
143
120
while (key > 255 ):
144
121
key -= 255
145
122
146
123
# This will be returned
147
124
ans = ""
148
- resultNumber = 0
149
-
125
+
150
126
for ch in content :
151
127
ans += chr (ord (ch ) ^ key )
152
128
@@ -166,15 +142,12 @@ def encrypt_file(self, file, key = 0):
166
142
assert (isinstance (file ,str ) and isinstance (key ,int ))
167
143
168
144
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 ))
145
+ with open (file ,"r" ) as fin :
146
+ with open ("encrypt.out" ,"w+" ) as fout :
175
147
176
- fin .close ()
177
- fout .close ()
148
+ # actual encrypt-process
149
+ for line in fin :
150
+ fout .write (self .encrypt_string (line ,key ))
178
151
179
152
except :
180
153
return False
@@ -195,15 +168,12 @@ def decrypt_file(self,file, key):
195
168
assert (isinstance (file ,str ) and isinstance (key ,int ))
196
169
197
170
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 ))
171
+ with open (file ,"r" ) as fin :
172
+ with open ("decrypt.out" ,"w+" ) as fout :
204
173
205
- fin .close ()
206
- fout .close ()
174
+ # actual encrypt-process
175
+ for line in fin :
176
+ fout .write (self .decrypt_string (line ,key ))
207
177
208
178
except :
209
179
return False
@@ -214,26 +184,26 @@ def decrypt_file(self,file, key):
214
184
215
185
216
186
# Tests
217
- # crypt = XORCipher()
218
- # key = 67
187
+ crypt = XORCipher ()
188
+ key = 67
219
189
220
- # # test enrcypt
221
- # print crypt.encrypt("hallo welt",key)
222
- # # test decrypt
223
- # print crypt.decrypt(crypt.encrypt("hallo welt",key), key)
190
+ # test enrcypt
191
+ print crypt .encrypt ("hallo welt" ,key )
192
+ # test decrypt
193
+ print crypt .decrypt (crypt .encrypt ("hallo welt" ,key ), key )
224
194
225
- # # test encrypt_string
226
- # print crypt.encrypt_string("hallo welt",key)
195
+ # test encrypt_string
196
+ print crypt .encrypt_string ("hallo welt" ,key )
227
197
228
- # # test decrypt_string
229
- # print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)
198
+ # test decrypt_string
199
+ print crypt .decrypt_string (crypt .encrypt_string ("hallo welt" ,key ),key )
230
200
231
- # if (crypt.encrypt_file("test.txt",key)):
232
- # print "encrypt successful"
233
- # else:
234
- # print "encrypt unsuccessful"
201
+ if (crypt .encrypt_file ("test.txt" ,key )):
202
+ print "encrypt successful"
203
+ else :
204
+ print "encrypt unsuccessful"
235
205
236
- # if (crypt.decrypt_file("a .out",key)):
237
- # print "decrypt successful"
238
- # else:
239
- # print "decrypt unsuccessful"
206
+ if (crypt .decrypt_file ("encrypt .out" ,key )):
207
+ print "decrypt successful"
208
+ else :
209
+ print "decrypt unsuccessful"
0 commit comments