init
This commit is contained in:
commit
69bf22c9ec
21 changed files with 778 additions and 0 deletions
26
cbc.py
Normal file
26
cbc.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from Cryptodome.Cipher import AES
|
||||
|
||||
BLOCK_SIZE = 16
|
||||
|
||||
|
||||
class AESCipher(object):
|
||||
"""This class is compatible with crypto.createCipheriv('aes-256-cbc')"""
|
||||
|
||||
def __init__(self, key=None):
|
||||
self.key = key
|
||||
|
||||
def pad(self, data):
|
||||
length = BLOCK_SIZE - (len(data) % BLOCK_SIZE)
|
||||
return data + (chr(length) * length).encode()
|
||||
|
||||
def unpad(self, data):
|
||||
return data[: -(data[-1] if type(data[-1]) == int else ord(data[-1]))]
|
||||
|
||||
def encrypt(self, plain_text):
|
||||
cipher = AES.new(self.key, AES.MODE_CBC)
|
||||
b = plain_text.encode("UTF-8")
|
||||
return cipher.iv, cipher.encrypt(self.pad(b))
|
||||
|
||||
def decrypt(self, iv, enc_text):
|
||||
cipher = AES.new(self.key, AES.MODE_CBC, iv=iv)
|
||||
return self.unpad(cipher.decrypt(enc_text).decode("UTF-8"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue