Skip to content

Commit fbdb25b

Browse files
authored
upload symcryptool
1 parent 857d7e0 commit fbdb25b

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

symcryptool/cryptool.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import os
2+
import argparse
3+
from cryptography.fernet import Fernet
4+
5+
parser = argparse.ArgumentParser(description=r"""
6+
7+
_________ __ .__
8+
\_ ___ \_______ ___.__.______ _/ |_ ____ ____ | |
9+
/ \ \/\_ __ < | |\____ \ ______ \ __\/ _ \ / _ \| |
10+
\ \____| | \/\___ || |_> > /_____/ | | ( <_> | <_> ) |__
11+
\______ /|__| / ____|| __/ |__| \____/ \____/|____/
12+
\/ \/ |__|
13+
14+
15+
Usage:
16+
py SCRIPT.py -f FILE -k KEY -e/d
17+
18+
Flags:
19+
-c --create_key Create a new encryption key and save it as a new file.
20+
21+
-f --files Specify the file you want to encrypt or decrypt.
22+
Note: Does not encrypt .enc files and only decrypts .enc files.
23+
24+
-e --encrypt Use -e to encrypt the specified file with the selected key file.
25+
Note: An encrypted copy with the extension The fileAME.EXTENSION.enc will be created in the current directory.
26+
27+
-d --decrypt Use -d to decrypt the specified file with the selected key file.
28+
Note: A decrypted copy with the The fileame decrypted_The fileAME.EXTENSION will be created in the current directory.
29+
30+
-k --key Specify the key file you want to use for encryption or decryption.
31+
32+
33+
Examples of correct usage:
34+
Create a new key: py cryptool.py -c new_key.key
35+
36+
Encrypt a file: py cryptool.py -f testfile.txt -k test.key -e
37+
38+
Decrypt a file: py cryptool.py -f testfile.txt.enc -k test.key -d""",
39+
epilog="See information above", formatter_class=argparse.RawDescriptionHelpFormatter)
40+
41+
def generate_key_mode(name_key_in_gkm):
42+
newkey = Fernet.generate_key()
43+
with open(name_key_in_gkm, "wb") as key_file:
44+
key_file.write(newkey)
45+
46+
def encrypt_and_store_info(file, key):
47+
with open(key, "rb") as key_file:
48+
key = key_file.read()
49+
50+
cipher_suite = Fernet(key)
51+
52+
with open(file, "rb") as file_to_encrypt:
53+
content = file_to_encrypt.read()
54+
cipher_content = cipher_suite.encrypt(content)
55+
56+
encrypted_file = file + ".enc"
57+
with open(encrypted_file, "wb") as file_to_encrypt:
58+
file_to_encrypt.write(cipher_content)
59+
60+
def decrypt_and_store_info(file, key):
61+
with open(key, "rb") as key_file:
62+
key = key_file.read()
63+
64+
cipher_suite = Fernet(key)
65+
66+
with open(file, "rb") as file_to_decrypt:
67+
content = file_to_decrypt.read()
68+
cipher_content = cipher_suite.decrypt(content)
69+
70+
decrypted_file = "decrypted_" + file.replace(".enc", "")
71+
with open(decrypted_file, "wb") as file_to_decrypt:
72+
file_to_decrypt.write(cipher_content)
73+
74+
def main():
75+
76+
parser.add_argument("-c", "--create_key", metavar="Create new key", help="Create a new encryption key")
77+
parser.add_argument("-f", "--file", metavar="Choose: File", type=str, help="Choose file to encrypt or decrypt")
78+
parser.add_argument("-k", "--key", metavar="Choose: Key", type=str, help="Choose key-file to encrypt or decrypt file")
79+
80+
group = parser.add_mutually_exclusive_group()
81+
group.add_argument("-e", "--encrypt", action="store_true", help="Choose for encryption")
82+
group.add_argument("-d", "--decrypt", action="store_true", help="Choose for decryption")
83+
84+
args = parser.parse_args()
85+
86+
if len(os.sys.argv) == 1:
87+
parser.print_help()
88+
return
89+
90+
if args.create_key:
91+
if not os.path.exists(args.create_key):
92+
generate_key_mode(args.create_key)
93+
print(f"Key {args.create_key} created.")
94+
else:
95+
print(f"Key {args.create_key} already exists.")
96+
97+
if (args.file and args.key) and not (args.encrypt or args.decrypt):
98+
print("Error: You need to specify the encryption mode '-e' or '-d'.")
99+
return
100+
101+
if args.file and (args.decrypt or args.encrypt):
102+
if not os.path.exists(args.file):
103+
print(f"File {args.file} does not exist.")
104+
return
105+
excisting_file = args.file
106+
else:
107+
print("Error: Choose a file. Choose '-f' and file.")
108+
return
109+
110+
if args.key and (args.decrypt or args.encrypt):
111+
if not os.path.exists(args.key):
112+
print(f"Keyfile {args.key}finns ej.")
113+
return
114+
excisting_key = args.key
115+
else:
116+
print("Error: Key file missing. Choose '-k' and key-file.")
117+
return
118+
119+
120+
if args.encrypt:
121+
if excisting_file.endswith(".enc"):
122+
print(f"The file {excisting_file} is already encrypted.")
123+
else:
124+
encrypt_and_store_info(excisting_file, excisting_key)
125+
print(f"The file {excisting_file} is encrypted.")
126+
127+
if args.decrypt:
128+
if not excisting_file.endswith(".enc"):
129+
print(f"The file {excisting_file} is already decrypted.")
130+
else:
131+
decrypt_and_store_info(excisting_file, excisting_key)
132+
print(f"The file {excisting_file} is decrypted.")
133+
134+
135+
if __name__ == "__main__":
136+
main()

0 commit comments

Comments
 (0)