Skip to content

Commit 0fa47cd

Browse files
authored
Merge pull request #932 from kaliappan01/aes-py
aes_py added
2 parents 3a0a40e + 0bd187d commit 0fa47cd

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

aes_py/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# aes_py
2+
This script will be able to encrypt and decrypt text using AES256 using password based key. -
3+
4+
## Usage
5+
`
6+
$ python3 main.py --text "this is a secret message" -op 0
7+
Enter password :
8+
b'8jeJABqjpdK0aVExWZars2Uzd+vBZIWQFRvdIqt5DJz3X/bSuHBnX4YW7MjYCrSg'
9+
$ python3 main.py --text "8jeJABqjpdK0aVExWZars2Uzd+vBZIWQFRvdIqt5DJz3X/bSuHBnX4YW7MjYCrSg" -op 1
10+
Enter password :
11+
b'this is a secret message'
12+
`

aes_py/aes_tool.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import base64
2+
from Crypto.Cipher import AES
3+
from Crypto import Random
4+
from Crypto.Protocol.KDF import PBKDF2
5+
6+
BLOCK_SIZE = 16
7+
8+
9+
def pad(s):
10+
return s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(
11+
BLOCK_SIZE - len(s) % BLOCK_SIZE
12+
)
13+
14+
15+
def unpad(s):
16+
return s[: -ord(s[len(s) - 1:])]
17+
18+
19+
def get_private_key(password):
20+
salt = b"this is a salt"
21+
kdf = PBKDF2(password, salt, 64, 1000)
22+
key = kdf[:32]
23+
return key
24+
25+
26+
def encrypt(raw, password):
27+
private_key = get_private_key(password)
28+
raw = pad(raw)
29+
iv = Random.new().read(AES.block_size)
30+
cipher = AES.new(private_key, AES.MODE_CBC, iv)
31+
cipher = iv + cipher.encrypt(raw.encode("utf-8"))
32+
return base64.b64encode(cipher)
33+
34+
35+
def decrypt(enc, password):
36+
private_key = get_private_key(password)
37+
enc = base64.b64decode(enc)
38+
iv = enc[:16]
39+
cipher = AES.new(private_key, AES.MODE_CBC, iv)
40+
return unpad(cipher.decrypt(enc[16:]))
41+
42+
43+
if __name__ == "__main__":
44+
password = input("Enter encryption password: ")
45+
# First let us encrypt secret message
46+
encrypted = encrypt("This is a secret message", password)
47+
print(encrypted)
48+
49+
# Let us decrypt using our original password
50+
decrypted = decrypt(encrypted, password)
51+
print(bytes.decode(decrypted))

aes_py/main.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import argparse
2+
from getpass import getpass
3+
import aes_tool
4+
5+
6+
parser = argparse.ArgumentParser(description="Aes \
7+
encryption and decryption command")
8+
9+
parser.add_argument("--text", help=argparse.SUPPRESS)
10+
parser.add_argument(
11+
"--operation",
12+
"-op",
13+
choices=["0", "1"],
14+
help="0 for encryption and 1 for decryption",
15+
)
16+
args = parser.parse_args()
17+
password = getpass("Enter password : ")
18+
19+
if args.operation == "0":
20+
print(aes_tool.encrypt(args.text, password))
21+
22+
elif args.operation == "1":
23+
print(aes_tool.decrypt(args.text, password))

aes_py/makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
VENV ?= .venv
2+
REQUIREMENTS_FILE ?= requirements.txt
3+
4+
init:
5+
python3 -m venv $(VENV)
6+
$(VENV)/bin/python -m pip install --upgrade pip
7+
if [ -f $(REQUIREMENTS_FILE) ]; \
8+
then $(VENV)/bin/python -m pip install -r $(REQUIREMENTS_FILE); \
9+
fi

aes_py/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pycryptodome==3.15.0

0 commit comments

Comments
 (0)