Skip to content

Commit 23bf9c6

Browse files
authored
Initial commit
1 parent 4630fe5 commit 23bf9c6

12 files changed

+290
-0
lines changed

AES/aes.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
from Crypto.Random import get_random_bytes
3+
from Crypto.Protocol.KDF import PBKDF2
4+
from Crypto.Cipher import AES
5+
from Crypto.Util.Padding import pad, unpad
6+
7+
def aes_encrypt(file_path, key):
8+
salt = get_random_bytes(32)
9+
key = str(key).encode()
10+
key = PBKDF2(key, salt, dkLen=32)
11+
output_file_path = file_path + '.enc'
12+
13+
with open(file_path, 'rb') as f:
14+
plaintext = f.read()
15+
16+
cipher = AES.new(key, AES.MODE_CBC)
17+
ciphered_data = cipher.encrypt(pad(plaintext, AES.block_size))
18+
19+
with open(output_file_path, 'wb') as f:
20+
f.write(salt)
21+
f.write(cipher.iv)
22+
f.write(ciphered_data)
23+
24+
def aes_decrypt(file_path, key):
25+
with open(file_path, 'rb') as f:
26+
salt = f.read(32)
27+
iv = f.read(16)
28+
ciphered_data = f.read()
29+
30+
key = str(key).encode()
31+
key = PBKDF2(key, salt, dkLen=32)
32+
cipher = AES.new(key, AES.MODE_CBC, iv)
33+
original_data = unpad(cipher.decrypt(ciphered_data), AES.block_size)
34+
35+
with open(file_path[:-4], 'wb') as f:
36+
f.write(original_data)
37+

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# File Encryption CLI
2+
3+
## Description
4+
5+
File Encryption is command-line tool used to encrypt and decrypt files using AES and RSA encryption algorithms. It is written in Python and uses the `PyCryptoDome` library.
6+
7+
## Requirements
8+
9+
- Python 3.6 or above
10+
- `PyCryptoDome` library
11+
12+
## Installation
13+
14+
1. Install pycryptodome library
15+
16+
```
17+
pip install pycryptodome
18+
```
19+
20+
2. Clone the repository
21+
22+
```
23+
git clone https://github.com/ezhil56x/file-encryption-cli.git
24+
```
25+
26+
3. Go to file-encryption-cli directory
27+
28+
```
29+
cd file-encryption-cli
30+
```
31+
32+
4. Take a look at the help menu to see the usage of the tool
33+
34+
```
35+
python file_encrypt.py --help
36+
```
37+
38+
```
39+
python file_decrypt.py --help
40+
```
41+
42+
## Usage
43+
44+
### AES Encryption
45+
46+
#### Encrypting a File
47+
48+
To encrypt a file, using AES encryption, use the following command
49+
50+
```
51+
python file_encrypt.py --file <file> --algorithm AES --key <text>
52+
```
53+
54+
#### Decrypting a File
55+
56+
To decrypt a file, using AES encryption, use the following command
57+
58+
```
59+
python file_decrypt.py --file <file> --algorithm AES --key <text>
60+
```
61+
62+
### RSA Encryption
63+
64+
For RSA encryption, you need to generate public and private keys. You can use the `key_generator.py` script to generate the keys. And the keys will be stored in `public.pem` and `private.pem` files. You should move these files to the same directory as the `file_encrypt.py` and `file_decrypt.py` scripts.
65+
66+
#### Encrypting a File
67+
68+
To encrypt a file, using RSA encryption, use the following command
69+
70+
```
71+
python file_encrypt.py --file <file> --algorithm RSA --key public.pem
72+
```
73+
74+
#### Decrypting a File
75+
76+
To decrypt a file, using RSA encryption, use the following command
77+
78+
```
79+
python file_decrypt.py --file <file> --algorithm RSA --key private.pem
80+
```
81+
82+
#### Generating public and private keys
83+
84+
To generate public and private keys, go to RSA folder and run the following command
85+
86+
```
87+
python key_generator.py
88+
```

RSA/key_generator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import rsa
2+
3+
public_key, private_key = rsa.newkeys(1024)
4+
5+
with open('public.pem', 'wb') as f:
6+
f.write(public_key.save_pkcs1("PEM"))
7+
8+
with open('private.pem', 'wb') as f:
9+
f.write(private_key.save_pkcs1("PEM"))

RSA/private.pem

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICYQIBAAKBgQCRzqI97AECoIEFksgUxZdcfNWg6HBR4iekiYOwp1+pHm+zHNd4
3+
18+HC6P09DT4JM1wS0JEpJ+TYz0aUPeAN2vodhtjs7ZVLsbsWTPYTvU19pQpL7em
4+
ZRTwsbEXr+YicwmCi4nKJXxBWsbGNr1Vgx8dEiaRSpj9FGNEnrr35SJGWwIDAQAB
5+
AoGALg/5cVZ9ezb5i6kzTFyf1LgbRRopYbTwkSpgUA6QSf0zjIIwEB0dkqw6v8fm
6+
c/4saK1d4hGf2BAh3Y52cZbIdI1KB3I/HRFVtfxLC1atEOhl8fMsfZ100BF2IIIu
7+
N71eSFKVXOqksb9gPG6nO5sOYay5wKBJs8b94xmwXGzk/wECRQCZxYqhXf0P3J/K
8+
zZwZ4/tTIBaGnFdlQ1mGi7Doo0KE+XQTLIfM9dpUTExlxjSCJW1ArqfcyoQHGXLN
9+
FZsob1/6hQuXDQI9APK9nRPIV+2k0Ms0CHJAP9+yhUfFgX74GZPEWZ9Qes7sJ4KZ
10+
Dpya7nLGEBa/WeRztlzwFehuMX0jyhx5BwJEBPGX6mY2K5thO8juetqciuieMMe3
11+
+5Z1u2CkWGZfTh0jATbOSuWgllM5pZAAwipn38t9tBiCtCO6UCOgsGUcRdafL8UC
12+
PQCYCVtRALLSDeEY5UZFCSj4m4APLA2n0Q10GA9qUaYZ5J/6lJMqU3/2M0fwd3l+
13+
NVuLanTOV/Z3QutcWhECRQCYtaUpHTIN2/X3FiAanMNRTVVDJxxpss1L3ZvTzREF
14+
3jpz/PPq1LFeKpOyMX1X0uviRa9FevU9TQwFUMBBv8H+HFMnNg==
15+
-----END RSA PRIVATE KEY-----

RSA/public.pem

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN RSA PUBLIC KEY-----
2+
MIGJAoGBAJHOoj3sAQKggQWSyBTFl1x81aDocFHiJ6SJg7CnX6keb7Mc13jXz4cL
3+
o/T0NPgkzXBLQkSkn5NjPRpQ94A3a+h2G2OztlUuxuxZM9hO9TX2lCkvt6ZlFPCx
4+
sRev5iJzCYKLicolfEFaxsY2vVWDHx0SJpFKmP0UY0SeuvflIkZbAgMBAAE=
5+
-----END RSA PUBLIC KEY-----

RSA/rsa.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import rsa
2+
3+
def rsa_encrypt(file_path, key):
4+
5+
with open(key, "rb") as f:
6+
public_key = rsa.PublicKey.load_pkcs1(f.read())
7+
8+
with open(file_path, "rb") as f:
9+
data = f.read()
10+
encrypted_data = rsa.encrypt(data, public_key)
11+
with open(file_path + ".enc", "wb") as f:
12+
f.write(encrypted_data)
13+
14+
def rsa_decrypt(file_path, key):
15+
16+
with open(key, "rb") as f:
17+
private_key = rsa.PrivateKey.load_pkcs1(f.read())
18+
19+
with open(file_path, "rb") as f:
20+
data = f.read()
21+
decrypted_data = rsa.decrypt(data, private_key)
22+
with open(file_path[:-4], "wb") as f:
23+
f.write(decrypted_data)

file_decrypt.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
from lib.Argument import Argument
3+
from RSA.rsa import rsa_decrypt
4+
from AES.aes import aes_decrypt
5+
6+
parser = Argument()
7+
args = sys.argv
8+
9+
parser.parse_args(args)
10+
11+
if '--file' not in parser.option or '--algorithm' not in parser.option or '--key' not in parser.option:
12+
print("Usage: python3 file_decrypt.py --file <file> --algorithm <algorithm> --key <key>"
13+
"\n\t--file: file to decrypt"
14+
"\n\t--algorithm: algorithm to use for decryption (AES, RSA)"
15+
"\n\t--key: key to use for decryption"
16+
"\n\t--help: print this help message")
17+
exit(1)
18+
19+
file_path = parser.optionValues.get('--file')
20+
algorithm = parser.optionValues.get('--algorithm')
21+
key = parser.optionValues.get('--key')
22+
23+
if '--help' in parser.option:
24+
print("Usage: python3 file_decrypt.py --file <file> --algorithm <algorithm> --key <key>"
25+
"\n\t--file: file to decrypt"
26+
"\n\t--algorithm: algorithm to use for decryption (AES, RSA)"
27+
"\n\t--key: key to use for decryption"
28+
"\n\t--help: print this help message")
29+
exit(0)
30+
31+
if algorithm == 'AES':
32+
aes_decrypt(file_path, key)
33+
34+
elif algorithm == 'RSA':
35+
rsa_decrypt(file_path, key)
36+
37+
else:
38+
print('Algorithm not supported')
39+
40+

file_encrypt.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
from lib.Argument import Argument
3+
from RSA.rsa import rsa_encrypt
4+
from AES.aes import aes_encrypt
5+
6+
parser = Argument()
7+
args = sys.argv
8+
9+
parser.parse_args(args)
10+
11+
if '--file' not in parser.option or '--algorithm' not in parser.option or '--key' not in parser.option:
12+
print("Usage: python3 file_encrypt.py --file <file> --algorithm <algorithm> --key <key>"
13+
"\n\t--file: file to encrypt"
14+
"\n\t--algorithm: algorithm to use for encryption (AES, RSA)"
15+
"\n\t--key: key to use for encryption"
16+
"\n\t--help: print this help message")
17+
exit(1)
18+
19+
file_path = parser.optionValues.get('--file')
20+
algorithm = parser.optionValues.get('--algorithm')
21+
key = parser.optionValues.get('--key')
22+
23+
if '--help' in parser.option:
24+
print("Usage: python3 file_encrypt.py --file <file> --algorithm <algorithm> --key <key>"
25+
"\n\t--file: file to encrypt"
26+
"\n\t--algorithm: algorithm to use for encryption (AES, RSA)"
27+
"\n\t--key: key to use for encryption"
28+
"\n\t--help: print this help message")
29+
exit(0)
30+
31+
if algorithm == 'AES':
32+
aes_encrypt(file_path, key)
33+
34+
elif algorithm == 'RSA':
35+
rsa_encrypt(file_path, key)
36+
37+
else:
38+
print('Algorithm not supported')
39+
40+

lib/Argument.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Argument:
2+
def __init__(self):
3+
self.option = []
4+
self.optionValues = {}
5+
6+
def parse_args(self, args):
7+
for arg in args:
8+
if arg.startswith('--'):
9+
self.option.append(arg)
10+
if args.index(arg) + 1 < len(args):
11+
self.optionValues[arg] = args[args.index(arg) + 1]
12+

private.pem

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICYQIBAAKBgQCRzqI97AECoIEFksgUxZdcfNWg6HBR4iekiYOwp1+pHm+zHNd4
3+
18+HC6P09DT4JM1wS0JEpJ+TYz0aUPeAN2vodhtjs7ZVLsbsWTPYTvU19pQpL7em
4+
ZRTwsbEXr+YicwmCi4nKJXxBWsbGNr1Vgx8dEiaRSpj9FGNEnrr35SJGWwIDAQAB
5+
AoGALg/5cVZ9ezb5i6kzTFyf1LgbRRopYbTwkSpgUA6QSf0zjIIwEB0dkqw6v8fm
6+
c/4saK1d4hGf2BAh3Y52cZbIdI1KB3I/HRFVtfxLC1atEOhl8fMsfZ100BF2IIIu
7+
N71eSFKVXOqksb9gPG6nO5sOYay5wKBJs8b94xmwXGzk/wECRQCZxYqhXf0P3J/K
8+
zZwZ4/tTIBaGnFdlQ1mGi7Doo0KE+XQTLIfM9dpUTExlxjSCJW1ArqfcyoQHGXLN
9+
FZsob1/6hQuXDQI9APK9nRPIV+2k0Ms0CHJAP9+yhUfFgX74GZPEWZ9Qes7sJ4KZ
10+
Dpya7nLGEBa/WeRztlzwFehuMX0jyhx5BwJEBPGX6mY2K5thO8juetqciuieMMe3
11+
+5Z1u2CkWGZfTh0jATbOSuWgllM5pZAAwipn38t9tBiCtCO6UCOgsGUcRdafL8UC
12+
PQCYCVtRALLSDeEY5UZFCSj4m4APLA2n0Q10GA9qUaYZ5J/6lJMqU3/2M0fwd3l+
13+
NVuLanTOV/Z3QutcWhECRQCYtaUpHTIN2/X3FiAanMNRTVVDJxxpss1L3ZvTzREF
14+
3jpz/PPq1LFeKpOyMX1X0uviRa9FevU9TQwFUMBBv8H+HFMnNg==
15+
-----END RSA PRIVATE KEY-----

public.pem

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN RSA PUBLIC KEY-----
2+
MIGJAoGBAJHOoj3sAQKggQWSyBTFl1x81aDocFHiJ6SJg7CnX6keb7Mc13jXz4cL
3+
o/T0NPgkzXBLQkSkn5NjPRpQ94A3a+h2G2OztlUuxuxZM9hO9TX2lCkvt6ZlFPCx
4+
sRev5iJzCYKLicolfEFaxsY2vVWDHx0SJpFKmP0UY0SeuvflIkZbAgMBAAE=
5+
-----END RSA PUBLIC KEY-----

sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Created by ezhil56x

0 commit comments

Comments
 (0)