mirror of
https://github.com/Deathemonic/BA-AD.git
synced 2025-07-29 03:37:24 +02:00

- refactored entire codebase - remove --category - remove --version - docs: updated tutorial - docs: readme update - renamed files to proper python style guide - replace old crypto with ba-cy - moved images to .github/resources - moved extractors to its own folder (extractors) - renamed lib to crypto - moved some functions to its own helper - added helper scripts
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from base64 import b64decode, b64encode
|
|
|
|
from Crypto.Cipher import AES
|
|
from Crypto.Protocol.KDF import PBKDF2
|
|
from Crypto.Random import get_random_bytes
|
|
from Crypto.Util.Padding import pad, unpad
|
|
|
|
# TODO: Deprecated (Moving to BA-CY)
|
|
# Keep here for compatibility
|
|
def encrypt(plain_text: str, pass_phrase: str) -> str:
|
|
block_size = 16
|
|
|
|
salt = get_random_bytes(block_size)
|
|
iv = get_random_bytes(block_size)
|
|
|
|
derived = PBKDF2(pass_phrase, salt, 16, count=1000)
|
|
cipher = AES.new(key=derived[:16], iv=iv, mode=AES.MODE_CBC)
|
|
data = pad(cipher.encrypt(plain_text.encode('utf-8')), block_size, style='pkcs7')
|
|
|
|
return b64encode(salt + iv + data).decode('utf-8')
|
|
|
|
|
|
def decrypt(cipher_text: str | bytes, pass_phrase: str) -> str:
|
|
block_size = 16
|
|
|
|
raw_cipher_text = b64decode(cipher_text)
|
|
salt, iv, encrypted_data = (
|
|
raw_cipher_text[:block_size],
|
|
raw_cipher_text[block_size : block_size * 2],
|
|
raw_cipher_text[block_size * 2 :],
|
|
)
|
|
|
|
derived = PBKDF2(pass_phrase, salt, 16, count=1000)
|
|
cipher = AES.new(key=derived, iv=iv, mode=AES.MODE_CBC)
|
|
|
|
return unpad(cipher.decrypt(encrypted_data), block_size, style='pkcs7').decode('utf-8')
|
|
|
|
|
|
def encrypt_string_to_bytes(plain_text: str, key: bytes, iv: bytes) -> bytes:
|
|
block_size = 16
|
|
|
|
cipher = AES.new(key=key, iv=iv, mode=AES.MODE_CBC)
|
|
return pad(cipher.encrypt(plain_text.encode('utf-8')), block_size)
|
|
|
|
|
|
def decrypt_string_from_bytes(cipher_text: bytes, key: bytes, iv: bytes) -> str:
|
|
block_size = 16
|
|
|
|
cipher = AES.new(key=key, iv=iv, mode=AES.MODE_CBC)
|
|
return unpad(cipher.decrypt(cipher_text), block_size).decode('utf-8')
|