Skip to content

Commit 7ec7032

Browse files
authored
Add files via upload
0 parents  commit 7ec7032

7 files changed

Lines changed: 312 additions & 0 deletions

File tree

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 K9Crypt
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# K9Crypt
2+
3+
K9Crypt is a powerful Python library that provides multi-layer encryption. It securely protects your data using five different AES-256-based encryption modes.
4+
5+
## Features
6+
7+
- 5-layer AES-256 encryption (GCM, CBC, CFB, OFB, CTR)
8+
- Strong key derivation with PBKDF2
9+
- HMAC-SHA512 verification at each layer
10+
- Brotli compression support
11+
- Asynchronous (async/await) API
12+
- Protection against timing attacks
13+
14+
## Installation
15+
16+
```bash
17+
pip install k9crypt
18+
```
19+
20+
## Usage Example
21+
22+
```python
23+
from k9crypt import K9Crypt
24+
import asyncio
25+
26+
async def test():
27+
secret_key = "VeryLongSecretKey!@#1234567890"
28+
encryptor = K9Crypt(secret_key)
29+
plaintext = "Hello, World!"
30+
31+
try:
32+
encrypted = await encryptor.encrypt(plaintext)
33+
print("Encrypted data:", encrypted)
34+
35+
decrypted = await encryptor.decrypt(encrypted)
36+
print("Decrypted data:", decrypted)
37+
except Exception as error:
38+
print("Encryption error:", str(error))
39+
40+
asyncio.run(test())
41+
```
42+
43+
## Security Features
44+
45+
1. **Multi-Layer Encryption**: Each layer uses a different AES-256 mode
46+
2. **HMAC Verification**: Integrity check at each layer
47+
3. **Strong Key Derivation**: 600,000 iterations with PBKDF2
48+
4. **Secure Comparison**: Protection against timing attacks
49+
5. **Salt and Pepper**: Unique salt used for each encryption
50+
51+
## Requirements
52+
53+
- Python 3.7+
54+
- cryptography>=41.0.7
55+
- brotli>=1.1.0
56+
57+
## License
58+
59+
MIT License
60+
61+
## Contribution
62+
63+
1. Fork this repository
64+
2. Create a new branch (`git checkout -b feature/new-feature`)
65+
3. Commit your changes (`git commit -am 'Added new feature'`)
66+
4. Push your branch (`git push origin feature/new-feature`)
67+
5. Create a Pull Request

‎k9crypt/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .k9crypt import K9Crypt
2+
3+
__version__ = "0.1.0"
4+
__author__ = "K9Crypt"
5+
__all__ = ["K9Crypt"]

‎k9crypt/constants.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HMAC_KEY = b"K9CryptHMAC2024!@#$%^&*()"

‎k9crypt/k9crypt.py‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import os
2+
import base64
3+
import hmac as hmac_module
4+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
5+
from cryptography.hazmat.primitives import padding, hashes, hmac
6+
from cryptography.hazmat.backends import default_backend
7+
import brotli
8+
9+
HMAC_KEY = b"K9CryptHMAC2024!@#$%^&*()"
10+
11+
class K9Crypt:
12+
def __init__(self, key: str):
13+
hasher = hashes.Hash(hashes.SHA512(), backend=default_backend())
14+
hasher.update(key.encode())
15+
self.key = hasher.finalize()[:32]
16+
17+
def _generate_iv(self) -> bytes:
18+
return os.urandom(16)
19+
20+
def _pad(self, data: bytes) -> bytes:
21+
padder = padding.PKCS7(128).padder()
22+
return padder.update(data) + padder.finalize()
23+
24+
def _unpad(self, data: bytes) -> bytes:
25+
unpadder = padding.PKCS7(128).unpadder()
26+
return unpadder.update(data) + unpadder.finalize()
27+
28+
def _hash_data(self, data: bytes) -> bytes:
29+
h = hmac.HMAC(HMAC_KEY, hashes.SHA512(), backend=default_backend())
30+
h.update(data)
31+
digest = h.finalize()
32+
return digest
33+
34+
def _verify_hash(self, data: bytes, expected_hash: bytes) -> bool:
35+
calculated_hash = self._hash_data(data)
36+
try:
37+
return hmac_module.compare_digest(calculated_hash, expected_hash)
38+
except Exception:
39+
return False
40+
41+
def _encrypt_gcm(self, data: bytes) -> tuple[bytes, bytes]:
42+
iv = self._generate_iv()
43+
cipher = Cipher(algorithms.AES(self.key), modes.GCM(iv), backend=default_backend())
44+
encryptor = cipher.encryptor()
45+
ciphertext = encryptor.update(data) + encryptor.finalize()
46+
return ciphertext + encryptor.tag, iv
47+
48+
def _decrypt_gcm(self, data: bytes, iv: bytes) -> bytes:
49+
tag = data[-16:]
50+
ciphertext = data[:-16]
51+
cipher = Cipher(algorithms.AES(self.key), modes.GCM(iv, tag), backend=default_backend())
52+
decryptor = cipher.decryptor()
53+
return decryptor.update(ciphertext) + decryptor.finalize()
54+
55+
def _encrypt_cbc(self, data: bytes) -> tuple[bytes, bytes]:
56+
iv = self._generate_iv()
57+
cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=default_backend())
58+
encryptor = cipher.encryptor()
59+
padded_data = self._pad(data)
60+
return encryptor.update(padded_data) + encryptor.finalize(), iv
61+
62+
def _decrypt_cbc(self, data: bytes, iv: bytes) -> bytes:
63+
cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=default_backend())
64+
decryptor = cipher.decryptor()
65+
padded_data = decryptor.update(data) + decryptor.finalize()
66+
return self._unpad(padded_data)
67+
68+
def _encrypt_cfb(self, data: bytes) -> tuple[bytes, bytes]:
69+
iv = self._generate_iv()
70+
cipher = Cipher(algorithms.AES(self.key), modes.CFB(iv), backend=default_backend())
71+
encryptor = cipher.encryptor()
72+
return encryptor.update(data) + encryptor.finalize(), iv
73+
74+
def _decrypt_cfb(self, data: bytes, iv: bytes) -> bytes:
75+
cipher = Cipher(algorithms.AES(self.key), modes.CFB(iv), backend=default_backend())
76+
decryptor = cipher.decryptor()
77+
return decryptor.update(data) + decryptor.finalize()
78+
79+
def _encrypt_ofb(self, data: bytes) -> tuple[bytes, bytes]:
80+
iv = self._generate_iv()
81+
cipher = Cipher(algorithms.AES(self.key), modes.OFB(iv), backend=default_backend())
82+
encryptor = cipher.encryptor()
83+
return encryptor.update(data) + encryptor.finalize(), iv
84+
85+
def _decrypt_ofb(self, data: bytes, iv: bytes) -> bytes:
86+
cipher = Cipher(algorithms.AES(self.key), modes.OFB(iv), backend=default_backend())
87+
decryptor = cipher.decryptor()
88+
return decryptor.update(data) + decryptor.finalize()
89+
90+
def _encrypt_ctr(self, data: bytes) -> tuple[bytes, bytes]:
91+
iv = self._generate_iv()
92+
cipher = Cipher(algorithms.AES(self.key), modes.CTR(iv), backend=default_backend())
93+
encryptor = cipher.encryptor()
94+
return encryptor.update(data) + encryptor.finalize(), iv
95+
96+
def _decrypt_ctr(self, data: bytes, iv: bytes) -> bytes:
97+
cipher = Cipher(algorithms.AES(self.key), modes.CTR(iv), backend=default_backend())
98+
decryptor = cipher.decryptor()
99+
return decryptor.update(data) + decryptor.finalize()
100+
101+
async def _compress(self, data: bytes) -> bytes:
102+
try:
103+
return brotli.compress(data)
104+
except Exception as e:
105+
raise ValueError(f"Compression error: {str(e)}")
106+
107+
async def _decompress(self, data: bytes) -> bytes:
108+
try:
109+
return brotli.decompress(data)
110+
except Exception as e:
111+
raise ValueError(f"Decompression error: {str(e)}")
112+
113+
async def encrypt(self, plaintext: str) -> str:
114+
data = plaintext.encode()
115+
data = await self._compress(data)
116+
117+
data, iv1 = self._encrypt_gcm(data)
118+
hash1 = self._hash_data(data)
119+
data = hash1 + data
120+
121+
data, iv2 = self._encrypt_cbc(data)
122+
hash2 = self._hash_data(data)
123+
data = hash2 + data
124+
125+
data, iv3 = self._encrypt_cfb(data)
126+
hash3 = self._hash_data(data)
127+
data = hash3 + data
128+
129+
data, iv4 = self._encrypt_ofb(data)
130+
hash4 = self._hash_data(data)
131+
data = hash4 + data
132+
133+
data, iv5 = self._encrypt_ctr(data)
134+
hash5 = self._hash_data(data)
135+
data = hash5 + data
136+
137+
combined = iv1 + iv2 + iv3 + iv4 + iv5 + data
138+
139+
return base64.b64encode(combined).decode()
140+
141+
async def decrypt(self, ciphertext: str) -> str:
142+
data = base64.b64decode(ciphertext)
143+
144+
iv1 = data[:16]
145+
iv2 = data[16:32]
146+
iv3 = data[32:48]
147+
iv4 = data[48:64]
148+
iv5 = data[64:80]
149+
data = data[80:]
150+
151+
hash5 = data[:64]
152+
data = data[64:]
153+
if not self._verify_hash(data, hash5):
154+
raise ValueError("Layer 5 integrity check failed")
155+
data = self._decrypt_ctr(data, iv5)
156+
157+
hash4 = data[:64]
158+
data = data[64:]
159+
if not self._verify_hash(data, hash4):
160+
raise ValueError("Layer 4 integrity check failed")
161+
data = self._decrypt_ofb(data, iv4)
162+
163+
hash3 = data[:64]
164+
data = data[64:]
165+
if not self._verify_hash(data, hash3):
166+
raise ValueError("Layer 3 integrity check failed")
167+
data = self._decrypt_cfb(data, iv3)
168+
169+
hash2 = data[:64]
170+
data = data[64:]
171+
if not self._verify_hash(data, hash2):
172+
raise ValueError("Layer 2 integrity check failed")
173+
data = self._decrypt_cbc(data, iv2)
174+
175+
hash1 = data[:64]
176+
data = data[64:]
177+
if not self._verify_hash(data, hash1):
178+
raise ValueError("Layer 1 integrity check failed")
179+
data = self._decrypt_gcm(data, iv1)
180+
181+
data = await self._decompress(data)
182+
return data.decode()

‎requirements.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cryptography>=41.0.7
2+
pytest>=7.4.3
3+
brotli>=1.1.0

‎setup.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from setuptools import setup, find_packages
2+
3+
with open("README.md", "r", encoding="utf-8") as fh:
4+
long_description = fh.read()
5+
6+
setup(
7+
name="k9crypt",
8+
version="0.1.0",
9+
author="K9Crypt",
10+
author_email="hi@k9crypt.xyz",
11+
description="A special encryption algorithm created for K9Crypt.",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/k9crypt/k9crypt-python",
15+
packages=find_packages(),
16+
classifiers=[
17+
"Development Status :: 4 - Beta",
18+
"Intended Audience :: Developers",
19+
"Topic :: Security :: Cryptography",
20+
"License :: OSI Approved :: MIT License",
21+
"Programming Language :: Python :: 3",
22+
"Programming Language :: Python :: 3.7",
23+
"Programming Language :: Python :: 3.8",
24+
"Programming Language :: Python :: 3.9",
25+
"Programming Language :: Python :: 3.10",
26+
"Programming Language :: Python :: 3.11",
27+
],
28+
python_requires=">=3.7",
29+
install_requires=[
30+
"cryptography>=41.0.7",
31+
"brotli>=1.1.0",
32+
],
33+
)

0 commit comments

Comments
 (0)