Skip to content

aes_crypto

data.transform.aes_crypto

BUFFER_SIZE module-attribute

BUFFER_SIZE = block_size

SAIGE_GENERATED_IMAGE_PASSWORD module-attribute

SAIGE_GENERATED_IMAGE_PASSWORD = 'AI that works'

encrypt

encrypt(data: Any, password: str = SAIGE_GENERATED_IMAGE_PASSWORD, buffer_size: int = BUFFER_SIZE) -> bytes

Encrypt image data with AES.

Parameters:

  • data (Any) –

    encrypt aes data with aes cipher.

  • password (str, default: SAIGE_GENERATED_IMAGE_PASSWORD ) –

    password for encrypting. Defaults to SAIGE_GENERATED_IMAGE_PASSWORD.

  • buffer_size (int, default: BUFFER_SIZE ) –

    buffer size for padding. Defaults to BUFFER_SIZE.

Returns:

  • bytes ( bytes ) –

    encrypted data with aes cipher.

Source code in SaigeToolkit/data/transform/aes_crypto.py
def encrypt(
    data: Any,
    password: str = SAIGE_GENERATED_IMAGE_PASSWORD,
    buffer_size: int = BUFFER_SIZE,
) -> bytes:
    """Encrypt image data with AES.

    Args:
        data (Any): encrypt aes data with aes cipher.
        password (str): password for encrypting. Defaults to SAIGE_GENERATED_IMAGE_PASSWORD.
        buffer_size (int, optional): buffer size for padding. Defaults to BUFFER_SIZE.

    Returns:
        bytes: encrypted data with aes cipher.
    """
    key = sha256(password.encode()).digest()

    cipher = AES.new(key, AES.MODE_CBC)
    iv = cipher.iv
    encrypted_data = cipher.encrypt(pad(data, buffer_size))
    return iv + encrypted_data

decrypt

decrypt(encrypted_data: bytes, password: str = SAIGE_GENERATED_IMAGE_PASSWORD, buffer_size: int = BUFFER_SIZE) -> Any

Decrypt image data with AES.

Parameters:

  • encrypted_data (bytes) –

    encrypted data with aes cipher.

  • password (str, default: SAIGE_GENERATED_IMAGE_PASSWORD ) –

    password for decrypting. Defaults to SAIGE_GENERATED_IMAGE_PASSWORD.

  • buffer_size (int, default: BUFFER_SIZE ) –

    buffer size for padding. Defaults to BUFFER_SIZE.

Returns:

  • Any ( Any ) –

    decrypted data with aes cipher.

Source code in SaigeToolkit/data/transform/aes_crypto.py
def decrypt(
    encrypted_data: bytes,
    password: str = SAIGE_GENERATED_IMAGE_PASSWORD,
    buffer_size: int = BUFFER_SIZE,
) -> Any:
    """Decrypt image data with AES.

    Args:
        encrypted_data (bytes): encrypted data with aes cipher.
        password (str): password for decrypting. Defaults to SAIGE_GENERATED_IMAGE_PASSWORD.
        buffer_size (int, optional): buffer size for padding. Defaults to BUFFER_SIZE.

    Returns:
        Any: decrypted data with aes cipher.
    """
    key = sha256(password.encode()).digest()

    iv = encrypted_data[:buffer_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    decrypted_data = unpad(cipher.decrypt(encrypted_data[buffer_size:]), buffer_size)
    return decrypted_data

decrypt_load_image

decrypt_load_image(path: str, password: str = SAIGE_GENERATED_IMAGE_PASSWORD) -> Image

Decrypt and load image file with AES.

Parameters:

  • path (str) –

    encrypted image file path.

  • password (str, default: SAIGE_GENERATED_IMAGE_PASSWORD ) –

    password for decrypting. Defaults to SAIGE_GENERATED_IMAGE_PASSWORD.

Returns:

  • Image ( Image ) –

    decrypted image.

Source code in SaigeToolkit/data/transform/aes_crypto.py
def decrypt_load_image(
    path: str,
    password: str = SAIGE_GENERATED_IMAGE_PASSWORD,
) -> Image:
    """Decrypt and load image file with AES.

    Args:
        path (str): encrypted image file path.
        password (str): password for decrypting. Defaults to SAIGE_GENERATED_IMAGE_PASSWORD.

    Returns:
        Image: decrypted image.
    """
    with open(path, "rb") as f:
        encrypted_data = f.read()
    decrypted_data = decrypt(encrypted_data, password)
    image = Image.open(BytesIO(base64.b64decode(decrypted_data)))
    return image