A pure Python implementation of cryptographic primitives, written in a clean, Pythonic, and type-safe way.
Project description
๐ pycrypt
A pure Python implementation of cryptographic primitives, written in a clean, Pythonic, and type-safe way.
โ ๏ธ Disclaimer:
pycryptis an educational cryptography library. It is not safe for production use. Use only for learning how cryptographic algorithms work under the hood.
Do not roll your own crypto in production code. Use a safe, audited library that has been vetted by professionals.
๐ Overview
pycrypt implements major cryptographic primitives from scratch in pure Python
with minimal dependencies. It is designed for learners and developers interested
in the inner workings of cryptography.
๐ฆ Features
| Category | Algorithm | Description |
|---|---|---|
| Asymmetric | RSA | OAEP encryption/decryption, PSS signing/verification |
| DiffieโHellman (DH) | Modular exponentiation and HKDF-based key derivation | |
| Symmetric | AES | ECB, CBC, CTR, and GCM modes |
| Hashing | SHA-1, SHA-256 | HMAC and HKDF included |
๐๏ธ Project Structure
pycrypt/
โโโ asymmetric/
โ โโโ dh/
โ โ โโโ core.py
โ โ โโโ groups.py
โ โ โโโ keyformat.py
โ โโโ rsa/
โ โโโ core.py
โ โโโ keyformat.py
โ โโโ utils.py
โโโ hash/
โ โโโ sha/
โ โ โโโ core.py
โ โ โโโ hmac.py
โ โ โโโ variants.py
โโโ symmetric/
โ โโโ aes/
โ โโโ core.py
โ โโโ modes.py
โ โโโ utils.py
โโโ utils/
โ โโโ asn1.py
โ โโโ padding.py
โ โโโ utils.py
โโโ main.py
๐ Installation
pip install pycrypt-lib
๐งฉ Examples
๐ธ DiffieโHellman (DH) Key Exchange
from pycrypt.asymmetric import DH
params = DH.generate_parameters(2048)
alice_priv = params.generate_private_key()
bob_priv = params.generate_private_key()
alice_shared = alice_priv.exchange(bob_priv.public_key())
bob_shared = bob_priv.exchange(alice_priv.public_key())
assert alice_shared == bob_shared
print(f"Shared secret: {alice_shared.hex()}")
๐ธ RSA Encryption and Signing
from pycrypt.asymmetric import RSAKey
key = RSAKey.generate(2048)
message = b"Hello RSA!"
cipher = key.oaep_encrypt(message)
plain = key.oaep_decrypt(cipher)
signature = key.pss_sign(message)
assert key.pss_verify(message, signature)
๐ธ AES (GCM Mode)
from secrets import token_bytes
from pycrypt.symmetric import AES_GCM
key = token_bytes(16)
nonce = token_bytes(12)
aes = AES_GCM(key)
ciphertext, tag = aes.encrypt(b"Top Secret", nonce=nonce)
plaintext = aes.decrypt(ciphertext, nonce=nonce, tag=tag)
print(plaintext.decode())
๐ธ SHA-256 Hash
from pycrypt.hash import SHA256
sha = SHA256()
sha.update(b"hello world")
print(sha.hexdigest())
๐ชช License
MIT License
Copyright (c) 2025 Aravindaksha Balaji
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
โ ๏ธ Note: This library is not secure for production use. It is a learning and exploration tool only.
๐ Links
๐ Cryptography Reference Standards
Project details
Release history Release notifications | RSS feed
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 pycrypt_lib-1.1.2.tar.gz.
File metadata
- Download URL: pycrypt_lib-1.1.2.tar.gz
- Upload date:
- Size: 29.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5a92ef83c3effd0463cb0ffd227aed0bdac9f07240c68b5dc68c86a8d8616c1
|
|
| MD5 |
c300b6615e037264b84366e6a96116b5
|
|
| BLAKE2b-256 |
9bec01dd7f37f228bade949d5821f3b107212373f5c010be782d768538ada8e8
|
File details
Details for the file pycrypt_lib-1.1.2-py3-none-any.whl.
File metadata
- Download URL: pycrypt_lib-1.1.2-py3-none-any.whl
- Upload date:
- Size: 35.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dec310aae96fbab9a65d78665558a2559fa891696545f04bb72a89bff3d774f1
|
|
| MD5 |
1f8162ceea0d2c3f58484ffaf9cd784d
|
|
| BLAKE2b-256 |
cd146a3e2845e960b0c99a97b661c6076c2d35840301ebb33a73964e3fa065af
|