-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
151 lines (115 loc) · 4.2 KB
/
cipher.py
File metadata and controls
151 lines (115 loc) · 4.2 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Run from same working directory with: `python3 cipher.py`
# Install `cryptography` with: `pip3 install cryptography`
import cryptography
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
bold = '\033[1m'
underline = '\033[4m'
end = '\033[0m'
def keyGen():
print('\nFiles will be encrypted using a public key and can only be')
print('decrypted using the relevant private key')
print('\nYou should share your public key with people who should be')
print('able to encrypt files for you to receive')
print(bold + '\nDO NOT SHARE YOUR PRIVATE KEY')
print('\n**WARNING**: THIS WILL OVERWRITE PRE-EXISTING KEYS IN THE')
print('CURRENT WORKING DIRECTORY' + end)
response = input('\nDo you understand [Y/n]: ').lower()
if response != 'y':
return
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
public_key = private_key.public_key()
pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
with open('private_key.pem', 'wb') as f:
f.write(pem)
print('\nPrivate key saved to: ' + bold + 'private_key.pem' + end)
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open('public_key.pem', 'wb') as f:
f.write(pem)
print('\nPublic key saved to: ' + bold + 'public_key.pem' + end + '\n')
def encrypt(file):
with open("public_key.pem", "rb") as key_file:
public_key = serialization.load_pem_public_key(
key_file.read(),
backend=default_backend()
)
f = open(file, 'rb')
message = f.read()
f.close()
encrypted = public_key.encrypt(
message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
writeFile = file.replace('.txt', '.encrypted')
f = open(writeFile, 'wb')
f.write(encrypted)
f.close()
print('\nEncrypted file saved as: ' + bold + writeFile + end + '\n')
def decrypt(file):
with open("private_key.pem", "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
f = open(file, 'rb')
message = f.read()
f.close()
decrypted = private_key.decrypt(
message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
writeFile = file.replace('.encrypted', '.txt')
f = open(writeFile, 'wb')
f.write(decrypted)
f.close()
print('\nDecrypted file saved as: ' + bold + writeFile + end + '\n')
def selectionMenu():
print('\nWelcome to the file encrypter/decrypter')
print('You will need a public and private key')
print('These are necessary for encryption and decryption\n')
print(underline + 'Options:' + end)
print(bold + '[1] ' + end + 'Encryption')
print(bold + '[2] ' + end + 'Decryption')
print(bold + '[3] ' + end + 'Key generation')
response = int(input('\nChoose an option (1-3): '))
if response == 1:
file = input('\nInput name of .txt file to encrypt: ')
if file.split('.')[-1] != 'txt':
print('File type not supported, choose a .txt file')
selectionMenu()
encrypt(file)
elif response == 2:
file = input('\nInput name of file to decrypt: ')
if file.split('.')[-1] != 'encrypted':
print('File type not supported, choose a .encrypted file')
selectionMenu()
decrypt(file)
elif response == 3:
keyGen()
else:
print('Invalid choice, select an option 1-3')
selectionMenu()
selectionMenu()