forked from p15670423/monkey
island: Change KeyBasedEncryptor's padding functions to use Crypto.Util.Padding
This commit is contained in:
parent
3ab660b8fe
commit
fc1affc0e7
|
@ -5,6 +5,7 @@ import logging
|
||||||
# is maintained.
|
# is maintained.
|
||||||
from Crypto import Random # noqa: DUO133 # nosec: B413
|
from Crypto import Random # noqa: DUO133 # nosec: B413
|
||||||
from Crypto.Cipher import AES # noqa: DUO133 # nosec: B413
|
from Crypto.Cipher import AES # noqa: DUO133 # nosec: B413
|
||||||
|
from Crypto.Util import Padding # noqa: DUO133
|
||||||
|
|
||||||
from monkey_island.cc.server_utils.encryption import IEncryptor
|
from monkey_island.cc.server_utils.encryption import IEncryptor
|
||||||
|
|
||||||
|
@ -37,11 +38,8 @@ class KeyBasedEncryptor(IEncryptor):
|
||||||
cipher = AES.new(self._key, AES.MODE_CBC, cipher_iv)
|
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 :]).decode())
|
||||||
|
|
||||||
# TODO: Review and evaluate the security of the padding function
|
def _pad(self, message: str) -> str:
|
||||||
def _pad(self, message):
|
return Padding.pad(message.encode(), self._BLOCK_SIZE).decode()
|
||||||
return message + (self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE)) * chr(
|
|
||||||
self._BLOCK_SIZE - (len(message) % self._BLOCK_SIZE)
|
|
||||||
)
|
|
||||||
|
|
||||||
def _unpad(self, message: str):
|
def _unpad(self, message: str) -> str:
|
||||||
return message[0 : -ord(message[len(message) - 1])]
|
return Padding.unpad(message.encode(), self._BLOCK_SIZE).decode()
|
||||||
|
|
Loading…
Reference in New Issue