encrypt_symmetric

encrypt_symmetric

Functions

Name Description
encrypt_symmetric Encrypts a plaintext message using AES in Counter (CTR) mode.

encrypt_symmetric

encrypt_symmetric.encrypt_symmetric(message, key)

Encrypts a plaintext message using AES in Counter (CTR) mode.

Parameters

Name Type Description Default
message str The human-readable string to be encrypted. Must be 256 characters or fewer. required
key str The shared secret key. If a string is provided, it must be Base64 encoded. OR a path to a file containing the key. required

Returns

Name Type Description
str A Base64-encoded string containing the 8-byte nonce followed by the ciphertext.

Raises

Name Type Description
ValueError If the key is not properly Base64 encoded, the message exceeds 256 characters, or the key length is invalid for AES.

See Also

generate_symmetric_key : Function used to create a valid key.

Notes

This function uses AES-CTR mode. CTR mode turns a block cipher into a stream cipher. It generates a unique nonce for every encryption call, meaning the same plaintext will produce different ciphertext every time it is encrypted with the same key.

Examples

>>> # Basic encryption with key
>>> from imitation_game.generate_symmetric_key     ... import generate_symmetric_key
>>> from imitation_game.encrypt_symmetric import encrypt_symmetric
>>> key = generate_symmetric_key()
>>> msg = "Top Secret"
>>> encrypted = encrypt_symmetric(msg, key)
>>> encrypted
'X0QsE3P0MVblGhPOWKdrgbaD'
>>> # CTR mode produces different results for the same input
>>> key = generate_symmetric_key()
>>> enc1 = encrypt_symmetric("Top Secret", key)
>>> enc2 = encrypt_symmetric("Top Secret", key)
>>> enc1 == enc2
False
>>> # Encryption using a key file
>>> key_path = "secret.key"
>>> generate_symmetric_key(key_path)
>>> msg = "Top Secret"
>>> encrypted = encrypt_symmetric(msg, key_path)
>>> isinstance(encrypted, str)
True
>>> # Encryption using an existing key file
>>> with open("secret.key", "w") as f:
...     f.write("aNa/PmjyUk2hrFFm+lqOTFE/nPhi+elUFg3SGt6EETg=")
>>> encrypted = encrypt_symmetric("Top Secret", "secret.key")
>>> encrypted
'0rH1lkuVpskHbqMkgAOx0+pd'