Pure Python implementation of the Serpent block cipher with CBC mode and PKCS#7 padding
Project description
pyserpent
[!CAUTION] Version 1.2.0 introduces a BREAKING CHANGE in CBC mode. The IV is now automatically prepended to the ciphertext. Data encrypted with v1.1.0 and earlier will not be compatible with the new decrypt function unless migrated. See below.
Install
pip install pyserpent
Quick Start - ECB
from pyserpent import Serpent
key = Serpent.generate_key()
serpent = Serpent(key)
plaintext = b"Hello, Serpent!!"
ciphertext = serpent.encrypt(plaintext)
decrypted = serpent.decrypt(ciphertext)
print(decrypted.decode())
Example Serpent CBC Mode
from pyserpent import Serpent, serpent_cbc_encrypt, serpent_cbc_decrypt
key = Serpent.generate_key()
plaintext = "Hello, Serpent!!"
# IV is generated automatically. To use your own: serpent_cbc_encrypt(key, plaintext, iv=your_iv)
ciphertext = serpent_cbc_encrypt(key, plaintext)
decrypted = serpent_cbc_decrypt(key, ciphertext)
print(decrypted.decode())
Migration from v1.1.0 to v1.2.0
To decrypt data created with version 1.1.0, you need to manually prepend the 16-byte IV to your ciphertext so the new version can recognize it.
If you used the default (zero) IV in v1.1.0:
# v1.1.0 used 16 zero bytes as default IV
old_iv = b'\x00' * 16
# Prepend it to your old ciphertext
prepared_ciphertext = old_iv + old_ciphertext
# Now it's compatible with v1.2.0 decrypt
plaintext = serpent_cbc_decrypt(key, prepared_ciphertext)
If you provided a custom IV in v1.1.0:
# Just prepend your IV to the ciphertext
prepared_ciphertext = your_old_iv + old_ciphertext
plaintext = serpent_cbc_decrypt(key, prepared_ciphertext)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyserpent-1.2.0.tar.gz.
File metadata
- Download URL: pyserpent-1.2.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80bb908de84618c312d240f0498f9158e37d65a240d7f3959b6e51a4e1ddae8a
|
|
| MD5 |
a2083a572ae97be569669cf5292f593b
|
|
| BLAKE2b-256 |
2442284a84cf8cad5a6b2009a86f74006254860e431413e5e984290289db463a
|
File details
Details for the file pyserpent-1.2.0-py3-none-any.whl.
File metadata
- Download URL: pyserpent-1.2.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99468594b5fc5e484933a6af89d4ba4d4455709363656bc24017196513884fd8
|
|
| MD5 |
b27acf4ead21efa6f25316772daa9219
|
|
| BLAKE2b-256 |
7f56eedc2f6a0842c53b8106f6c4b51a2fc0a9cf9f1d449658cf76834be085c4
|