forked from 0x00wolf/AES256-encoder-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.py
124 lines (113 loc) · 4.08 KB
/
encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# This program is an AES-256 cryptographic tool for the command line.
# It uses bit streaming to read in data so it can encrypt large files rapidly.
# There are four modes. See the print_usage function below for more information.
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
import sys
import os
default_key_path = './AES256key'
buffer_size = 65536 # 64kb
banner = """
_____ AES-256 CFB __ __
/ ____/___ _________ ____/ /__ _____ _/_/
/ __/ / __ \/ ___/ __ \/ __ / _ \/ ___/ _/_/
/ /___/ / / / /__/ /_/ / /_/ / __/ / _/_/
/_____/_/ /_/\___/\____/\__,_/\___/_/ __ /_/
/ __ \___ _________ ____/ /__ _____
/ / / / _ \/ ___/ __ \/ __ / _ \/ ___/
/ /_/ / __/ /__/ /_/ / /_/ / __/ /
/_____/\___/\___/\____/\__,_/\___/_/"""
def print_banner():
print("\n"*40)
print(banner +"\n")
def print_usage():
print_banner()
print('[-] Modes:\n')
print('-h display this message')
print('-e encrypt')
print('-d decrypt')
print('-k Generate new AES-256 key to parent directory\n')
print('[-] Examples:\n')
print('>>decoder.py -e <./file.type> <./key>')
print('>>decoder.py -d <./file.type.encrypted> <./key>')
print('>>decoder.py -k\n')
sys.exit(0)
def parse_error(e):
print_banner()
sys.stderr.write('[!] Error...')
sys.stderr.write(f'[-] Exception: {e}')
sys.stderr.write("[+] For usage instructions: encoder.py -h or -help")
sys.stderr.write('[-] Exiting.')
sys.exit(1)
def open_file(file_path):
with open(file_path, 'r') as f:
message = f.read()
return message
def get_key(key_path):
with open(key_path, 'rb') as f:
key = f.read()
return key
def generate_key():
new_key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits)
with open(default_key_path, 'wb') as f:
f.write(new_key)
print_banner()
print('[+] Key successfully written to ./AES256key')
print('[-] Exiting...')
sys.exit(0)
def encrypt(file_path, key_path):
key = get_key(key_path)
with open(file_path, 'rb') as input_file, \
open(f'{file_path}.encrypted', 'wb') as output_file:
cipher = AES.new(key, AES.MODE_CFB)
output_file.write(cipher.iv)
buffer = input_file.read(buffer_size)
while len(buffer) > 0:
ciphered_bytes = cipher.encrypt(buffer)
output_file.write(ciphered_bytes)
buffer = input_file.read(buffer_size)
# Delete original file? Comment out if no.
os.remove(file_path)
print_banner()
print('[+] File successfully encrypted!.')
print(f'[+] Encoded file output to: {file_path}.encrypted')
print('[-] Exiting...')
def decrypt(file_path, key_path):
remove_suffix = file_path.removesuffix('.encrypted')
key = get_key(key_path)
with open(file_path, 'rb') as input_file, \
open(remove_suffix, 'wb') as output_file:
iv = input_file.read(16)
cipher = AES.new(key, AES.MODE_CFB, iv=iv)
buffer = input_file.read(buffer_size)
while len(buffer) > 0:
bytes = cipher.decrypt(buffer)
output_file.write(bytes)
buffer = input_file.read(buffer_size)
input_file.close()
output_file.close()
os.remove(file_path)
print_banner()
print('\n[+] File successfully decrypted.')
print(f'[+] Encoded file output to: {remove_suffix}.encrypted')
print('[-] Exiting...')
def main():
try:
mode = sys.argv[1]
if len(sys.argv) == 2 and mode == '-k':
generate_key()
if len(sys.argv) != 4:
print_usage()
file_path = sys.argv[2]
key_path = sys.argv[3]
if mode == '-e':
encrypt(file_path, key_path)
elif mode == '-d':
decrypt(file_path, key_path)
else:
print_usage()
sys.exit(0)
except Exception as e:
parse_error(e)
if __name__ == '__main__':
main()