From 404228b04c7a874518922be37c97d04c8ca86fb2 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 4 Oct 2021 14:06:28 +0530 Subject: [PATCH] island: Modify KeyBasedEncryptor to get rid of redundant encoding and decoding --- .../encryption/encryptors/key_based_encryptor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/monkey/monkey_island/cc/server_utils/encryption/encryptors/key_based_encryptor.py b/monkey/monkey_island/cc/server_utils/encryption/encryptors/key_based_encryptor.py index 78aaacf9c..cb37a8c08 100644 --- a/monkey/monkey_island/cc/server_utils/encryption/encryptors/key_based_encryptor.py +++ b/monkey/monkey_island/cc/server_utils/encryption/encryptors/key_based_encryptor.py @@ -30,16 +30,16 @@ class KeyBasedEncryptor(IEncryptor): def encrypt(self, plaintext: str) -> str: cipher_iv = Random.new().read(AES.block_size) cipher = AES.new(self._key, AES.MODE_CBC, cipher_iv) - return base64.b64encode(cipher_iv + cipher.encrypt(self._pad(plaintext).encode())).decode() + return base64.b64encode(cipher_iv + cipher.encrypt(self._pad(plaintext))).decode() def decrypt(self, ciphertext: str): enc_message = base64.b64decode(ciphertext) cipher_iv = enc_message[0 : AES.block_size] cipher = AES.new(self._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 :])) - def _pad(self, message: str) -> str: - return Padding.pad(message.encode(), self._BLOCK_SIZE).decode() + def _pad(self, message: str) -> bytes: + return Padding.pad(message.encode(), self._BLOCK_SIZE) - def _unpad(self, message: str) -> str: - return Padding.unpad(message.encode(), self._BLOCK_SIZE).decode() + def _unpad(self, message: bytes) -> str: + return Padding.unpad(message, self._BLOCK_SIZE).decode()