Skip to content

custom_encryption

Description: This module provides encryption and decryption of text using AES algorithm. User can encrypt and decrypt text using a specified key.

CustomEncryption

A class that provides static methods to encrypt and decrypt text using an AES algorithm in GCM mode.

Methods:

Name Description
- custom_encrypt

Encrypts the given text using a specified key.

- custom_decrypt

Decrypts the given encrypted text using

Usage

encrypted = CustomEncryption.custom_encrypt("Hello, World!", b"example!KeyLen24or16or32") decrypted = CustomEncryption.custom_decrypt(encrypted, b"example!KeyLen24or16or32")

Source code in libs\cafex_core\src\cafex_core\utils\custom_encryption.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class CustomEncryption:
    """A class that provides static methods to encrypt and decrypt text using
    an AES algorithm in GCM mode.

    Methods:
        - custom_encrypt(plain_text, secret_key) : Encrypts the given text using a specified key.
        - custom_decrypt(encrypted_text, key): Decrypts the given encrypted text using
        the specified key.

    Usage:
        encrypted = CustomEncryption.custom_encrypt("Hello, World!", b"example!KeyLen24or16or32")
        decrypted = CustomEncryption.custom_decrypt(encrypted, b"example!KeyLen24or16or32")
    """

    @staticmethod
    def custom_encrypt(plain_text, secret_key):
        """Encrypts the given text using a specified key.

        Args:
            plain_text (str): The text to be encrypted.
            secret_key (bytes): The encryption key ex: b"example!KeyLen24or16or32"

        Returns:
            str: The encrypted text.

        Example:
            encrypted = CustomEncryption.custom_encrypt("Hello, World!",
            b"example!KeyLen24or16or32")
        Notes:
            Size of a key (in bytes)
            key_size = (16, 24, 32)
        """
        try:
            aes_obj = AES.new(secret_key, AES.MODE_GCM)
            nonce = aes_obj.nonce
            ciphertext, tag = aes_obj.encrypt_and_digest(plain_text.encode('utf-8'))
            encrypted_data = base64.b64encode(nonce + tag + ciphertext).decode('utf-8')
            return encrypted_data
        except Exception as e:
            raise e

    @staticmethod
    def custom_decrypt(cipher_text, secret_key):
        """Decrypts the given encrypted text using the specified key.

        Args:
            cipher_text (str): The text to be decrypted.
            secret_key (bytes): The decryption key. The decryption key ex:
            b"example!KeyLen24or16or32" (same as encryption key)

        Returns:
            str: The decrypted text.

        Example:
            decrypted = CustomEncryption.custom_decrypt("encrypted", b"example!KeyLen24or16or32")
        Notes:
            Size of a key (in bytes)
            key_size = (16, 24, 32)
        """
        try:
            encrypted_data = base64.b64decode(cipher_text)
            nonce = encrypted_data[:16]
            tag = encrypted_data[16:32]
            ciphertext = encrypted_data[32:]
            aes_obj = AES.new(secret_key, AES.MODE_GCM, nonce=nonce)
            decrypted_data = aes_obj.decrypt_and_verify(ciphertext, tag)
            return decrypted_data.decode('utf-8')
        except Exception as e:
            raise e

custom_decrypt(cipher_text, secret_key) staticmethod

Decrypts the given encrypted text using the specified key.

Parameters:

Name Type Description Default
cipher_text str

The text to be decrypted.

required
secret_key bytes

The decryption key. The decryption key ex:

required

Returns:

Name Type Description
str

The decrypted text.

Example

decrypted = CustomEncryption.custom_decrypt("encrypted", b"example!KeyLen24or16or32")

Notes: Size of a key (in bytes) key_size = (16, 24, 32)

Source code in libs\cafex_core\src\cafex_core\utils\custom_encryption.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@staticmethod
def custom_decrypt(cipher_text, secret_key):
    """Decrypts the given encrypted text using the specified key.

    Args:
        cipher_text (str): The text to be decrypted.
        secret_key (bytes): The decryption key. The decryption key ex:
        b"example!KeyLen24or16or32" (same as encryption key)

    Returns:
        str: The decrypted text.

    Example:
        decrypted = CustomEncryption.custom_decrypt("encrypted", b"example!KeyLen24or16or32")
    Notes:
        Size of a key (in bytes)
        key_size = (16, 24, 32)
    """
    try:
        encrypted_data = base64.b64decode(cipher_text)
        nonce = encrypted_data[:16]
        tag = encrypted_data[16:32]
        ciphertext = encrypted_data[32:]
        aes_obj = AES.new(secret_key, AES.MODE_GCM, nonce=nonce)
        decrypted_data = aes_obj.decrypt_and_verify(ciphertext, tag)
        return decrypted_data.decode('utf-8')
    except Exception as e:
        raise e

custom_encrypt(plain_text, secret_key) staticmethod

Encrypts the given text using a specified key.

Parameters:

Name Type Description Default
plain_text str

The text to be encrypted.

required
secret_key bytes

The encryption key ex: b"example!KeyLen24or16or32"

required

Returns:

Name Type Description
str

The encrypted text.

Example

encrypted = CustomEncryption.custom_encrypt("Hello, World!", b"example!KeyLen24or16or32")

Notes: Size of a key (in bytes) key_size = (16, 24, 32)

Source code in libs\cafex_core\src\cafex_core\utils\custom_encryption.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@staticmethod
def custom_encrypt(plain_text, secret_key):
    """Encrypts the given text using a specified key.

    Args:
        plain_text (str): The text to be encrypted.
        secret_key (bytes): The encryption key ex: b"example!KeyLen24or16or32"

    Returns:
        str: The encrypted text.

    Example:
        encrypted = CustomEncryption.custom_encrypt("Hello, World!",
        b"example!KeyLen24or16or32")
    Notes:
        Size of a key (in bytes)
        key_size = (16, 24, 32)
    """
    try:
        aes_obj = AES.new(secret_key, AES.MODE_GCM)
        nonce = aes_obj.nonce
        ciphertext, tag = aes_obj.encrypt_and_digest(plain_text.encode('utf-8'))
        encrypted_data = base64.b64encode(nonce + tag + ciphertext).decode('utf-8')
        return encrypted_data
    except Exception as e:
        raise e