-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCrypto.py
39 lines (31 loc) · 1.13 KB
/
MyCrypto.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
#!/usr/bin/python
import hashlib
import pickle
import base64
from Crypto.Cipher import AES
from Crypto import Random
class MyCrypto():
# hash password in SHA224
def hash_password_web(self, password):
hash_pass = hashlib.sha224(password).hexdigest()
return hash_pass
# hash password in SHA256
def hash_password(self, password):
hash_pass = hashlib.sha256(password).hexdigest()
return hash_pass
# padding message
def pad(self, s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encrypt(self, message, key):
key = self.hash_password_web(key)[:16]
message = self.pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(message)
def decrypt(self, ciphertext, key):
key = self.hash_password_web(key)[:16]
iv = self.pad(ciphertext)[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
# removes zeros from plaintext and returns
return plaintext.rstrip(b"\0")