-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (40 loc) · 1.96 KB
/
main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import argparse
import operator
import pathlib
import random
from lfsr import LFSR
from PIL import Image
def encrypt(image_path: str, password: int, tap_code: int, _reverse: bool=False):
operation = operator.sub if _reverse else operator.add
lfsr_gen = LFSR(password, tap_code)
img = Image.open(image_path).convert("RGB")
img_copy = img.copy()
for i in range(img_copy.width):
for j in range(img_copy.height):
red, green, blue = img_copy.getpixel((i, j))
red = operation(red, lfsr_gen.next_bits(8)) % 256
green = operation(green, lfsr_gen.next_bits(8)) % 256
blue = operation(blue, lfsr_gen.next_bits(8)) % 256
img_copy.putpixel((i, j), (red, green, blue))
return img_copy
def save_img(img, path):
path = pathlib.Path(path)
new_name = f"{path.stem}{random.choice(range(10))}.png"
img.save(new_name)
return new_name
def main():
parser = argparse.ArgumentParser()
option = parser.add_mutually_exclusive_group(required=True)
option.add_argument("-e", "--encrypt", action="store_true", help="encrypt image")
option.add_argument("-d", "--decrypt", action="store_true", help="decrypt image")
parser.add_argument("-i", "--image-path", type=str, required=True, help="full path of image")
parser.add_argument("-p", "--password", type=int, required=True, help="password to encrypt/decrypt image")
parser.add_argument("-t", "--tap-code", type=int, required=True, help="tap-code to encrypt/decrypt image")
args = parser.parse_args()
if not pathlib.Path(args.image_path).exists():
print(f"error: {args.image_path} not found"); return
img = encrypt(args.image_path, args.password, args.tap_code, args.decrypt)
new_img = save_img(img, args.image_path)
print(f"info: {chr(100) * args.decrypt}e{chr(110) * args.encrypt}crypted image saved as {new_img}")
if __name__ == "__main__":
main()