decrypt_symmetric

decrypt_symmetric

Functions

Name Description
decrypt_symmetric Decrypts an AES-CTR encrypted message and restores it to plaintext.

decrypt_symmetric

decrypt_symmetric.decrypt_symmetric(ciphertext, key)

Decrypts an AES-CTR encrypted message and restores it to plaintext.

Parameters

Name Type Description Default
ciphertext str The Base64-encoded string to be decrypted. This must contain the 8-byte nonce followed by the actual encrypted data. 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 The original plaintext message, UTF-8 decoded.

Raises

Name Type Description
ValueError If the key or ciphertext is not valid Base64, or if the data cannot be decoded as UTF-8 after decryption.

See Also

encrypt_symmetric : The counterpart function used to create the ciphertext.

Notes

The function extracts the first 8 bytes of the decoded ciphertext to use as the nonce. This must match the nonce generated during encryption for the process to succeed.

Examples

>>> # Basic decryption with key
>>> from imitation_game.generate_symmetric_key     ... import generate_symmetric_key
>>> from imitation_game.encrypt_symmetric import encrypt_symmetric
>>> from imitation_game.decrypt_symmetric import decrypt_symmetric
>>> key = generate_symmetric_key()
>>> ciphertext = encrypt_symmetric("Top Secret", key)
>>> decrypted = decrypt_symmetric(ciphertext, key)
>>> decrypted
'Top Secret'
>>> # Decryption using a key file
>>> key_path = "secret.key"
>>> generate_symmetric_key(key_path)
>>> ciphertext = encrypt_symmetric("Top Secret", key_path)
>>> decrypted = decrypt_symmetric(ciphertext, key_path)
>>> decrypted
'Top Secret'
>>> # Decryption using an existing key file
>>> with open("secret.key", "w") as f:
...     f.write("aNa/PmjyUk2hrFFm+lqOTFE/nPhi+elUFg3SGt6EETg=")
>>> ciphertext = "0rH1lkuVpskHbqMkgAOx0+pd"
>>> decrypted = decrypt_symmetric(ciphertext, "secret.key")
>>> decrypted
'Top Secret'