AES CTR 256 bits encryption written in Rust & compiled to Python extension.
Requires
Python 3.12
Download Asset from latest release on github.
Setup a venv and install the downloaded wheel via pip...
python3.12 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install Downloads/aes_ctr_rspy*.whlfrom aes_ctr_rspy import AesCtrSecret
key = bytearray(("2" * 32).encode())
nonce = bytearray(("4" * 8).encode())
secret = AesCtrSecret(key, nonce)
data = bytearray("This is a little longer test message than usual, to check if CTR is working as intended...".encode())
print("Plaintext: ", data, "\n")
ciphertext = bytearray(secret.encrypt(data))
print("Ciphertext: ", ciphertext, "\n")
key = bytearray(("2" * 32).encode())
nonce = bytearray(("4" * 8).encode())
secret2 = AesCtrSecret(key, nonce)
deciphered = secret2.encrypt(ciphertext)
print("Deciphered text: ", deciphered)Expected output
Plaintext: bytearray(b'This is a little longer test message than usual, to check if CTR is working as intended...')
Ciphertext: bytearray(b'...')
Deciphered text: b'This is a little longer test message than usual, to check if CTR is working as intended...'
Note: The AesCtrSecret struct overwrites the Python ByteArray passed as arguements to it, and zeroizes corresponding memory in Rust, thus has to be initialized/constructed for every encryption/decryption cycle.
--
from aes_ctr_rspy import aes_ctr_py as aesify
data = b"Hello, world!"
key = b"0" * 32
nonce = b"0" * 8
encrypted = aesify(data, key, nonce)
decrypted = aesify(encrypted, key, nonce)
print(decrypted)Should print b'Hello, world!'
Building from source
- Python 3.12
- Rust (via rustup.rs)
- maturin (PyPI)
pip install -r requirements.txtmaturin build --releaseThe wheel will be in target/wheels/, installable per pip via
pip install target/wheels/*.whlMIT License.