command-line-file-encryption 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 2.4
2
+ Name: command-line-file-encryption
3
+ Version: 0.1.0
4
+ Summary: Command line utility for file encryption and decryption
5
+ Author: Sid
6
+ Requires-Python: >=3.9
7
+ Requires-Dist: cryptography>=42.0.0
8
+ Dynamic: author
9
+ Dynamic: requires-dist
10
+ Dynamic: requires-python
11
+ Dynamic: summary
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 2.4
2
+ Name: command-line-file-encryption
3
+ Version: 0.1.0
4
+ Summary: Command line utility for file encryption and decryption
5
+ Author: Sid
6
+ Requires-Python: >=3.9
7
+ Requires-Dist: cryptography>=42.0.0
8
+ Dynamic: author
9
+ Dynamic: requires-dist
10
+ Dynamic: requires-python
11
+ Dynamic: summary
@@ -0,0 +1,12 @@
1
+ setup.py
2
+ command_line_file_encryption.egg-info/PKG-INFO
3
+ command_line_file_encryption.egg-info/SOURCES.txt
4
+ command_line_file_encryption.egg-info/dependency_links.txt
5
+ command_line_file_encryption.egg-info/entry_points.txt
6
+ command_line_file_encryption.egg-info/requires.txt
7
+ command_line_file_encryption.egg-info/top_level.txt
8
+ command_line_file_encryption_sid/__init__.py
9
+ command_line_file_encryption_sid/main.py
10
+ command_line_file_encryption_sid/lib/__init__.py
11
+ command_line_file_encryption_sid/lib/argument.py
12
+ command_line_file_encryption_sid/lib/functions.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ filecrypt = command_line_file_encryption_sid.main:main
@@ -0,0 +1 @@
1
+ command_line_file_encryption_sid
@@ -0,0 +1,40 @@
1
+ import argparse
2
+
3
+ def parse_args():
4
+ parser = argparse.ArgumentParser(
5
+ prog="filecrypt",
6
+ description="This tool help to Encrypt or decrypt files using either FERNET oe AES algorithm",
7
+ usage="filecrypt --file(or)--f <file path> --operation(or)--o <encrypt|decrypt> --algorithm(or)--a <AES|FERNET> --key(or)--k <master password>"
8
+ )
9
+
10
+ parser.add_argument(
11
+ "-f", "--file",
12
+ required=True,
13
+ help="Path to file"
14
+ )
15
+
16
+ parser.add_argument(
17
+ "-o", "--operation",
18
+ required=True,
19
+ choices=["encrypt", "decrypt"],
20
+ help="Operation to perform"
21
+ )
22
+
23
+ parser.add_argument(
24
+ "-a", "--algorithm",
25
+ required=True,
26
+ choices=["AES", "FERNET"],
27
+ help="Encryption algorithm"
28
+ )
29
+
30
+ parser.add_argument(
31
+ "-k", "--key",
32
+ required=True,
33
+ help="Encryption key"
34
+ )
35
+
36
+ return parser.parse_args()
37
+
38
+
39
+
40
+
@@ -0,0 +1,76 @@
1
+ #! /usr/bin/python
2
+ from cryptography.fernet import Fernet
3
+ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
4
+ from cryptography.hazmat.primitives import hashes
5
+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM
6
+ from cryptography.exceptions import InvalidTag
7
+ import base64
8
+ import os
9
+
10
+ def fernet_encrypt(filepath,key):
11
+ password_bytes = key.encode()
12
+ salt = os.urandom(16)
13
+ kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),length=32,salt=salt,iterations=100000)
14
+ dkey = kdf.derive(password_bytes)
15
+ fer_key = base64.urlsafe_b64encode(dkey)
16
+ openfile = open(filepath,"rb")
17
+ readfilename = openfile.read()
18
+ f = Fernet(fer_key)
19
+ encypted = f.encrypt(readfilename)
20
+ output = filepath+ ".enc"
21
+ with open(output, "wb") as enc:
22
+ enc.write(salt + encypted)
23
+ print(f"Encrypted file created in : {output}")
24
+
25
+ def fernet_decrypt(filepath,key):
26
+ try:
27
+ password_bytes = key.encode()
28
+ with open(filepath, "rb") as f:
29
+ content = f.read()
30
+ salt = content[:16]
31
+ encrypted_data = content[16:]
32
+ kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),length=32,salt=salt,iterations=100000)
33
+ dkey = kdf.derive(password_bytes)
34
+ fer_key = base64.urlsafe_b64encode(dkey)
35
+ f = Fernet(fer_key)
36
+ decrypt = f.decrypt(encrypted_data)
37
+ output = filepath.removesuffix(".enc")
38
+ with open(output, "wb") as decrp:
39
+ decrp.write(decrypt)
40
+ print(f"Decrypted file created in : {output}")
41
+ except InvalidTag:
42
+ print("Error: Wrong Password, Please re-try with right password")
43
+
44
+ def aes_encrypt(filepath,key):
45
+ password_bytes = key.encode()
46
+ salt = os.urandom(16)
47
+ kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),length=32,salt=salt,iterations=100000)
48
+ dkey = kdf.derive(password_bytes)
49
+ openfile = open(filepath,"rb")
50
+ readfilename = openfile.read()
51
+ encypted = AESGCM(dkey).encrypt(salt,readfilename,None)
52
+ output = filepath+ ".enc"
53
+ with open(output, "wb") as enc:
54
+ enc.write(salt + encypted)
55
+ print(f"Encrypted file created in : {output}")
56
+
57
+ def aes_decrypt(filepath,key):
58
+ try:
59
+ password_bytes = key.encode()
60
+ with open(filepath, "rb") as f:
61
+ content = f.read()
62
+ salt = content[:16]
63
+ encrypted_data = content[16:]
64
+ kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),length=32,salt=salt,iterations=100000)
65
+ dkey = kdf.derive(password_bytes)
66
+ decrypted = AESGCM(dkey).decrypt(salt,encrypted_data,None)
67
+ output = filepath.removesuffix(".enc")
68
+ with open(output, "wb") as decrp:
69
+ decrp.write(decrypted)
70
+ print(f"Decrypted file created in : {output}")
71
+
72
+ except InvalidTag:
73
+ print("Error: Wrong Password, Please re-try with right password")
74
+
75
+
76
+
@@ -0,0 +1,23 @@
1
+ #! /usr/bin/python
2
+
3
+ from lib.argument import parse_args
4
+ from lib.functions import *
5
+
6
+ def main():
7
+
8
+ args = parse_args()
9
+
10
+ if args.algorithm.upper() == "FERNET":
11
+ if args.operation == "encrypt":
12
+ fernet_encrypt(args.file, args.key)
13
+ else:
14
+ fernet_decrypt(args.file, args.key)
15
+
16
+ elif args.algorithm.upper() == "AES":
17
+ if args.operation == "encrypt":
18
+ aes_encrypt(args.file, args.key)
19
+ else:
20
+ aes_decrypt(args.file, args.key)
21
+
22
+ if __name__ == "__main__":
23
+ main()
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,20 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="command-line-file-encryption",
5
+ version="0.1.0",
6
+ author="Sid",
7
+ description="Command line utility for file encryption and decryption",
8
+ packages=find_packages(),
9
+ python_requires=">=3.9",
10
+ install_requires=[
11
+ "cryptography>=42.0.0"
12
+ ],
13
+
14
+
15
+ entry_points={
16
+ "console_scripts": [
17
+ "filecrypt=command_line_file_encryption_sid.main:main",
18
+ ],
19
+ },
20
+ )