forked from p15670423/monkey
cc: format encryptor.py with black
This commit is contained in:
parent
3f6c268f40
commit
d265238107
|
@ -25,30 +25,33 @@ class Encryptor:
|
||||||
|
|
||||||
def _init_key(self, password_file):
|
def _init_key(self, password_file):
|
||||||
self._cipher_key = Random.new().read(self._BLOCK_SIZE)
|
self._cipher_key = Random.new().read(self._BLOCK_SIZE)
|
||||||
with open(password_file, 'wb') as f:
|
with open(password_file, "wb") as f:
|
||||||
f.write(self._cipher_key)
|
f.write(self._cipher_key)
|
||||||
|
|
||||||
def _load_existing_key(self, password_file):
|
def _load_existing_key(self, password_file):
|
||||||
with open(password_file, 'rb') as f:
|
with open(password_file, "rb") as f:
|
||||||
self._cipher_key = f.read()
|
self._cipher_key = f.read()
|
||||||
|
|
||||||
def _pad(self, message):
|
def _pad(self, message):
|
||||||
return message + (self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE)) * chr(
|
return message + (self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE)) * chr(
|
||||||
self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE))
|
self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE)
|
||||||
|
)
|
||||||
|
|
||||||
def _unpad(self, message: str):
|
def _unpad(self, message: str):
|
||||||
return message[0:-ord(message[len(message) - 1])]
|
return message[0 : -ord(message[len(message) - 1])]
|
||||||
|
|
||||||
def enc(self, message: str):
|
def enc(self, message: str):
|
||||||
cipher_iv = Random.new().read(AES.block_size)
|
cipher_iv = Random.new().read(AES.block_size)
|
||||||
cipher = AES.new(self._cipher_key, AES.MODE_CBC, cipher_iv)
|
cipher = AES.new(self._cipher_key, AES.MODE_CBC, cipher_iv)
|
||||||
return base64.b64encode(cipher_iv + cipher.encrypt(self._pad(message).encode())).decode()
|
return base64.b64encode(
|
||||||
|
cipher_iv + cipher.encrypt(self._pad(message).encode())
|
||||||
|
).decode()
|
||||||
|
|
||||||
def dec(self, enc_message):
|
def dec(self, enc_message):
|
||||||
enc_message = base64.b64decode(enc_message)
|
enc_message = base64.b64decode(enc_message)
|
||||||
cipher_iv = enc_message[0:AES.block_size]
|
cipher_iv = enc_message[0 : AES.block_size]
|
||||||
cipher = AES.new(self._cipher_key, AES.MODE_CBC, cipher_iv)
|
cipher = AES.new(self._cipher_key, AES.MODE_CBC, cipher_iv)
|
||||||
return self._unpad(cipher.decrypt(enc_message[AES.block_size:]).decode())
|
return self._unpad(cipher.decrypt(enc_message[AES.block_size :]).decode())
|
||||||
|
|
||||||
|
|
||||||
def initialize_encryptor(data_dir):
|
def initialize_encryptor(data_dir):
|
||||||
|
|
Loading…
Reference in New Issue