MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / encrypt_file

Method encrypt_file

ciphers/xor_cipher.py:185–210  ·  view source on GitHub ↗

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

(self, file: str, key: int = 0)

Source from the content-addressed store, hash-verified

183 return ans
184
185 def encrypt_file(self, file: str, key: int = 0) -> bool:
186 """
187 input: filename (str) and a key (int)
188 output: returns true if encrypt 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)
196 assert isinstance(key, int)
197
198 # make sure key is an appropriate size
199 key %= 256
200
201 try:
202 with open(file) as fin, open("encrypt.out", "w+") as fout:
203 # actual encrypt-process
204 for line in fin:
205 fout.write(self.encrypt_string(line, key))
206
207 except OSError:
208 return False
209
210 return True
211
212 def decrypt_file(self, file: str, key: int) -> bool:
213 """

Callers

nothing calls this directly

Calls 1

encrypt_stringMethod · 0.95

Tested by

no test coverage detected