forked from GrosJeje1/git-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.py
39 lines (37 loc) · 1.4 KB
/
encrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python3
from __future__ import print_function
def encrypt(in_file,out_file,key=[255],blocksize=4096):
"""
This function takes a file a xor each byte with 255
It will then write this "anti-file" to the disk
NOTE: the file is loaded and written in one pass, so its size should not
exceed the available memory!
args:
in_file (str): Name of the input file
out_file (str): Name of the output file
NOTE: This encryption is NOT safe and should NOT be used
to protect your data!
"""
offset = 0
with open(in_file,"rb") as fi:
with open(out_file,"wb") as fo:
while True:
# bytearrays are mutable unlike bytes
content = bytearray(fi.read(blocksize)) # Read chunks of the file
if len(content) == 0: # Means we are done!
break
for i,b in enumerate(content):
content[i] = b ^ key[(offset+i)%len(key)] # ^ means bitwise XOR
fo.write(content)
offset += len(content)
print("Encrypted",offset,"bytes")
if __name__ == "__main__":
import sys
if sys.version_info.major < 3:
# For input to return a string and not execute the input in Python 2
input = raw_input
# If calling the file directly: prompt the files and run it once
f_in = input("File to encrypt?> ")
f_out = input("File to write (will be overwritten!)> ")
key = bytearray(input("Key? (leave empty for 255)> "),"utf-8") or [255]
encrypt(f_in,f_out,key)