Skip to content

Commit 6302536

Browse files
authored
Merge pull request #410 from adewale-devflow/Adewale-Afolabi
Simple Encrytion Algorithim in python
2 parents 3e11294 + f2c6b5d commit 6302536

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

OTHERS/Encryption/README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
# 🔐 Simple Symmetric Encryption in Python
3+
4+
This project demonstrates the basics of **symmetric encryption** using Python and the [`cryptography`](https://cryptography.io/en/latest/) library. It's a great starting point for beginners interested in **cybersecurity**, **cryptography**, or contributing to **open-source security tools**.
5+
6+
---
7+
8+
## 📚 What You'll Learn
9+
10+
- How to generate secure keys
11+
- How to encrypt and decrypt messages
12+
- Basic key management (saving & loading keys)
13+
- How `Fernet` (AES-based encryption) works under the hood
14+
15+
---
16+
17+
## 🚀 Getting Started
18+
19+
### 1. Clone the Repository
20+
21+
```bash
22+
git clone https://github.com/your-username/simple-encryption-python.git
23+
cd simple-encryption-python
24+
```
25+
26+
### 2. Install Dependencies
27+
28+
Make sure you have Python 3.6+ installed.
29+
30+
Install required package using `pip`:
31+
32+
```bash
33+
pip install cryptography
34+
```
35+
36+
### 3. Run the Code
37+
38+
```bash
39+
python simple_encryption.py
40+
```
41+
42+
On first run, it will generate a `secret.key` file that is used for both encryption and decryption. Each time you run the script, it:
43+
- Loads the existing key
44+
- Encrypts a sample message
45+
- Decrypts it back and displays the result
46+
47+
---
48+
49+
## 📂 File Structure
50+
51+
```
52+
simple-encryption-python/
53+
54+
├── simple_encryption.py # Main script to run encryption and decryption
55+
├── secret.key # Auto-generated AES-based symmetric key (DO NOT SHARE)
56+
├── README.md # Documentation
57+
```
58+
59+
---
60+
61+
## 🔒 Security Note
62+
63+
This example is for educational purposes only. If you’re building a production-grade application:
64+
- Never store raw keys in plaintext
65+
- Use environment variables or secure vaults (e.g., AWS KMS, HashiCorp Vault)
66+
- Handle exceptions and errors securely
67+
68+
---
69+
70+
## 🧠 How It Works (In Brief)
71+
72+
- **Fernet** is a module in the `cryptography` package that provides:
73+
- AES-128 in CBC mode
74+
- HMAC-SHA256 authentication
75+
- Random IVs for each encryption
76+
77+
- The encryption key is:
78+
- Generated once and saved to `secret.key`
79+
- Loaded on subsequent runs
80+
81+
- The message is:
82+
- Encrypted using `.encrypt()`
83+
- Decrypted using `.decrypt()`
84+
85+
---
86+
87+
## 💡 Sample Output
88+
89+
```
90+
[*] Key loaded from 'secret.key'
91+
92+
Original Message: This is a secret message.
93+
Encrypted Message: b'gAAAAABlZ...'
94+
Decrypted Message: This is a secret message.
95+
```
96+
97+
---
98+
99+
## 🤝 Contributing
100+
101+
Contributions are welcome! You can help by:
102+
- Improving the CLI interface
103+
- Adding file encryption support
104+
- Implementing password-based key derivation
105+
- Writing unit tests
106+
107+
To contribute:
108+
1. Fork the repo
109+
2. Create a new branch (`git checkout -b my-feature`)
110+
3. Commit your changes
111+
4. Push and create a Pull Request
112+
113+
---
114+
115+
## 📜 License
116+
117+
This project is licensed under the **MIT License**. See [LICENSE](LICENSE) for details.
118+
119+
---
120+
121+
## 👨‍💻 Author
122+
123+
**Afolabi Adewale**
124+
A data and security enthusiast exploring the intersection of Python, encryption, and open-source software.
125+
[GitHub Profile](https://github.com/your-username)
126+
127+
---
128+
129+
## 🔗 Related Resources
130+
131+
- [Python `cryptography` docs](https://cryptography.io/en/latest/)
132+
- [Understanding Symmetric vs Asymmetric Encryption](https://www.cloudflare.com/learning/ssl/how-does-ssl-work/)
133+
- [OWASP Crypto Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# simple_encryption.py
2+
3+
"""
4+
A simple example of symmetric encryption using Python's 'cryptography' package.
5+
6+
This script:
7+
1. Generates a secure encryption key
8+
2. Encrypts a message using the key
9+
3. Decrypts it back to the original message
10+
11+
Author: Afolabi Adewale
12+
"""
13+
14+
from cryptography.fernet import Fernet
15+
16+
# 1. Generate a key for encryption and decryption
17+
def generate_key():
18+
"""
19+
Generates a symmetric key for Fernet (uses AES encryption internally).
20+
"""
21+
key = Fernet.generate_key()
22+
with open("secret.key", "wb") as key_file:
23+
key_file.write(key)
24+
print("[+] Key generated and saved to 'secret.key'")
25+
return key
26+
27+
# 2. Load the existing key from file
28+
def load_key():
29+
"""
30+
Loads the previously generated key from the file.
31+
"""
32+
return open("secret.key", "rb").read()
33+
34+
# 3. Encrypt a message
35+
def encrypt_message(message: str, key: bytes) -> bytes:
36+
"""
37+
Encrypts a message using the provided symmetric key.
38+
"""
39+
f = Fernet(key)
40+
encrypted = f.encrypt(message.encode())
41+
return encrypted
42+
43+
44+
# 4. Decrypt a message
45+
def decrypt_message(encrypted_message: bytes, key: bytes) -> str:
46+
"""
47+
Decrypts an encrypted message using the same symmetric key.
48+
"""
49+
f = Fernet(key)
50+
decrypted = f.decrypt(encrypted_message)
51+
return decrypted.decode()
52+
53+
# 5. Main runner
54+
if __name__ == "__main__":
55+
# Create or load the key
56+
try:
57+
key = load_key()
58+
print("[*] Key loaded from 'secret.key'")
59+
except FileNotFoundError:
60+
key = generate_key()
61+
62+
# Example message
63+
message = "This is a secret message."
64+
print(f"\nOriginal Message: {message}")
65+
66+
# Encrypt it
67+
encrypted = encrypt_message(message, key)
68+
print(f"Encrypted Message: {encrypted}")
69+
70+
# Decrypt it
71+
decrypted = decrypt_message(encrypted, key)
72+
print(f"Decrypted Message: {decrypted}")

0 commit comments

Comments
 (0)