Skip to main content

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.

Python 3.9+ MIT License Release Build status PyPI version

โš ๏ธ Disclaimer: pycrypt is 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
โ”‚   โ””โ”€โ”€ 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())

๐Ÿชช 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycrypt_lib-1.0.4.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycrypt_lib-1.0.4-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

Details for the file pycrypt_lib-1.0.4.tar.gz.

File metadata

  • Download URL: pycrypt_lib-1.0.4.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"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

Hashes for pycrypt_lib-1.0.4.tar.gz
Algorithm Hash digest
SHA256 4e6ca4c5d04f69c38feb6a1a865c77e8f7246f66950afc5afcb67d93cb257697
MD5 d01197d6e2c215947b979931b6773731
BLAKE2b-256 4df57770c2161e92d93946b96f6361f4ca4df572c7f3f28889b66d2e2bec88da

See more details on using hashes here.

File details

Details for the file pycrypt_lib-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: pycrypt_lib-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 33.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"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

Hashes for pycrypt_lib-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 40c8cb5746f128cca498f378aa82b3ec562c44eea1206c3d5ce13fdadd657bc4
MD5 461364012e625c3da3ff047f13467273
BLAKE2b-256 a5ac2666c72ddc00c8f802d8bda33ca0cf0d2e94874b2f40eff251c4f455f0b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page