xxtea
raw JSON → 4.0.0 verified Mon Apr 27 auth: no python
xxtea is a simple block cipher implementing the XXTEA encryption algorithm. Current version 4.0.0, requires Python >=3.9. Released under BSD license. Active development with periodic releases.
pip install xxtea Common errors
error TypeError: a bytes-like object is required, not 'str' ↓
cause Key or data passed as string instead of bytes.
fix
Encode your key and data: key = b'secret', data = b'plaintext'
error xxtea.error: Invalid padding ↓
cause Decrypting with wrong key or corrupted ciphertext (padding validation fails).
fix
Ensure the correct key is used and that ciphertext was not tampered with. Check encryption parameters (e.g., padding).
Warnings
breaking The encrypt() function returns bytes, not hex string as in some older versions or wrappers. ↓
fix Use xxtea.encrypt_hex() if you need hex output, or call .hex() on the result.
gotcha The key must be a bytes object; passing a string raises TypeError. ↓
fix Always encode your key: b'key' or 'key'.encode()
gotcha Padding is enabled by default. Disable with padding=False only if you manually handle block size alignment. ↓
fix Leave padding=True unless you have a specific reason.
deprecated Python 2.7, 3.4, 3.5 support dropped in v3.0.0. ↓
fix Upgrade to Python >=3.6, preferably >=3.9.
Imports
- encrypt wrong
from xxtea import xxtea_encryptcorrectimport xxtea; xxtea.encrypt(data, key, padding=True) - decrypt wrong
from xxtea import decryptcorrectimport xxtea; xxtea.decrypt(ciphertext, key, padding=True)
Quickstart
import os
import xxtea
data = b'Hello, World!'
key = os.environ.get('XXTEA_KEY', 'secret').encode()
encrypted = xxtea.encrypt(data, key)
print(encrypted.hex())
decrypted = xxtea.decrypt(encrypted, key)
print(decrypted)