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 and simple Python, using minimal external libraries.
Analysing the code may help in:
- Learning how encryption, hashing, and key exchange algorithms work.
- Exploring low level arithmetic operations.
- Teaching cryptography fundamentals to students familiar with intermediate-level Python.
๐ฆ 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
โ โโโ rsa/
โ โโโ asn1.py
โ โโโ core.py
โ โโโ utils.py
โโโ hash/
โ โโโ sha/
โ โ โโโ core.py
โ โ โโโ hmac.py
โ โ โโโ variants.py
โโโ symmetric/
โ โโโ aes/
โ โโโ core.py
โ โโโ modes.py
โ โโโ utils.py
โโโ utils/
โ โโโ 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())
๐ง Design Philosophy
- Pure Python - minimal external dependencies
- Readable and modular - each algorithm is split into core and implementation parts
- Type-safe - uses
typingandrufffor static checks - Educational focus - clarity > performance
- Standards-aligned - based on official cryptographic specs
Understanding how cryptography works is the first step toward using it safely.
๐ชช License
MIT License
Copyright (c) 2025 Aravindaksha Balaji, Arnav Guntur
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.
๐ 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-0.9.0.tar.gz.
File metadata
- Download URL: pycrypt_lib-0.9.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b7e91c5fe7546c6ddd02534de13e48ee58b70f091e34d543f66efa70b2ce8d5
|
|
| MD5 |
a33173debec9e5ac82733dd111ad8127
|
|
| BLAKE2b-256 |
440bad3ff50b947ed3a94aafe66be3ce2b92f5235109e12feac4724945c7cc4a
|
File details
Details for the file pycrypt_lib-0.9.0-py3-none-any.whl.
File metadata
- Download URL: pycrypt_lib-0.9.0-py3-none-any.whl
- Upload date:
- Size: 28.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b6df93d389e1b58319848ca8c3c298fcb1434edc5da518ee4b681945194f59c
|
|
| MD5 |
24b8a94e3bbb84f6cf15875362794791
|
|
| BLAKE2b-256 |
7853e883162652b80766b80c5fff52855e8f48966d126ff80672989aa4bdbae2
|