This repository was archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvernam.py
116 lines (104 loc) · 2.72 KB
/
vernam.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Module providing CLI to (en/de)crypt and attack files using the Vernam cipher."""
import sys
import click
from src.application.preprocess import sanitize_to_alpha
from src.application.commands.encrypt import encrypt
from src.application.commands.decrypt import decrypt
from src.application.commands.attack import attack
@click.group()
def cli():
"""
CLI to (en/de)crypt and attack files using the Vernam cipher.
"""
@cli.command("encrypt")
@click.option(
"--in",
"input_file",
type=click.File("r"),
default=sys.stdin,
required=True,
help="Input file path. Use - to read from stdin. [default: stdin]",
)
@click.option(
"--out",
"output_file",
type=click.File("wb"),
default=sys.stdout,
required=True,
help="Output file path. Use - to output on stdout. [default: stdout]",
)
@click.option(
"--key",
"key",
type=str,
default=None,
required=True,
help="Key to use for encryption/decryption.",
)
def encrypt_command(input_file: click.File, output_file: click.File, key: str):
"""Encrypt text."""
input_text = input_file.read()
output_file.write(encrypt(sanitize_to_alpha(input_text), key))
@cli.command("decrypt")
@click.option(
"--in",
"input_file",
type=click.File("rb"),
default=sys.stdin,
required=True,
help="Input file path. Use - to read from stdin. [default: stdin]",
)
@click.option(
"--out",
"output_file",
type=click.File("w"),
default=sys.stdout,
required=True,
help="Output file path. Use - to output on stdout. [default: stdout]",
)
@click.option(
"--key",
"key",
type=str,
default=None,
required=True,
help="Key to use for encryption/decryption.",
)
def decrypt_command(input_file: click.File, output_file: click.File, key: str):
"""Decrypt text."""
cipher_text = input_file.read()
output_file.write(decrypt(cipher_text, key))
@cli.command("attack")
@click.option(
"--in",
"input_file",
type=click.File("rb"),
default=sys.stdin,
required=True,
help="Input file path. Use - to read from stdin. [default: stdin]",
)
@click.option(
"--out",
"output_file",
type=click.File("w"),
default=sys.stdout,
required=True,
help="Output file path. Use - to output on stdout. [default: stdout]",
)
@click.option(
"--silent",
"silent",
type=bool,
default=False,
required=False,
is_flag=True,
help="Silence the output.",
)
def attack_command(input_file: click.File, output_file: click.File, silent: bool):
"""Attack text."""
cipher_text = input_file.read()
output_file.write(
attack(cipher_text, click.echo if not silent else lambda *args, **kwargs: None)
)
if __name__ == "__main__":
cli()