48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
import shutil
|
|
from argparse import ArgumentParser
|
|
from pathlib import Path
|
|
from random import randbytes
|
|
|
|
from rc4 import rc4
|
|
|
|
|
|
def generate_key(key_length: int) -> bytes:
|
|
"""
|
|
Generates a random key with a length of key_length bytes
|
|
"""
|
|
return randbytes(key_length)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = ArgumentParser()
|
|
parser.add_argument("group", type=Path, help="Group root directory")
|
|
parser.add_argument("file", type=Path, help="File to decrypt for this group")
|
|
key_opts = parser.add_mutually_exclusive_group(required=True)
|
|
key_opts.add_argument(
|
|
"--key-file",
|
|
"-k",
|
|
dest="key_file",
|
|
type=Path,
|
|
help="Key source file for this group",
|
|
)
|
|
key_opts.add_argument(
|
|
"--key-length",
|
|
"-l",
|
|
dest="key_length",
|
|
type=int,
|
|
help="Key length for this group",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
# Create required files in group directory
|
|
shutil.copy(args.file, args.group / "data_decrypted")
|
|
if args.key_file is not None:
|
|
try:
|
|
shutil.copy(args.key_file, args.group / "key")
|
|
except shutil.SameFileError:
|
|
pass
|
|
else:
|
|
with open(args.group / "key", "wb") as key_file:
|
|
key_file.write(generate_key(args.key_length))
|
|
rc4(args.group / "data_decrypted", args.group / "data_encrypted", args.group / "key")
|