-
Notifications
You must be signed in to change notification settings - Fork 963
Simple Encrytion Algorithim in python #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
OTHERS/Encryption/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
|
||
# 🔐 Simple Symmetric Encryption in Python | ||
|
||
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**. | ||
|
||
--- | ||
|
||
## 📚 What You'll Learn | ||
|
||
- How to generate secure keys | ||
- How to encrypt and decrypt messages | ||
- Basic key management (saving & loading keys) | ||
- How `Fernet` (AES-based encryption) works under the hood | ||
|
||
--- | ||
|
||
## 🚀 Getting Started | ||
|
||
### 1. Clone the Repository | ||
|
||
```bash | ||
git clone https://github.com/your-username/simple-encryption-python.git | ||
cd simple-encryption-python | ||
``` | ||
|
||
### 2. Install Dependencies | ||
|
||
Make sure you have Python 3.6+ installed. | ||
|
||
Install required package using `pip`: | ||
|
||
```bash | ||
pip install cryptography | ||
``` | ||
|
||
### 3. Run the Code | ||
|
||
```bash | ||
python simple_encryption.py | ||
``` | ||
|
||
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: | ||
- Loads the existing key | ||
- Encrypts a sample message | ||
- Decrypts it back and displays the result | ||
|
||
--- | ||
|
||
## 📂 File Structure | ||
|
||
``` | ||
simple-encryption-python/ | ||
│ | ||
├── simple_encryption.py # Main script to run encryption and decryption | ||
├── secret.key # Auto-generated AES-based symmetric key (DO NOT SHARE) | ||
├── README.md # Documentation | ||
``` | ||
|
||
--- | ||
|
||
## 🔒 Security Note | ||
|
||
This example is for educational purposes only. If you’re building a production-grade application: | ||
- Never store raw keys in plaintext | ||
- Use environment variables or secure vaults (e.g., AWS KMS, HashiCorp Vault) | ||
- Handle exceptions and errors securely | ||
|
||
--- | ||
|
||
## 🧠 How It Works (In Brief) | ||
|
||
- **Fernet** is a module in the `cryptography` package that provides: | ||
- AES-128 in CBC mode | ||
- HMAC-SHA256 authentication | ||
- Random IVs for each encryption | ||
|
||
- The encryption key is: | ||
- Generated once and saved to `secret.key` | ||
- Loaded on subsequent runs | ||
|
||
- The message is: | ||
- Encrypted using `.encrypt()` | ||
- Decrypted using `.decrypt()` | ||
|
||
--- | ||
|
||
## 💡 Sample Output | ||
|
||
``` | ||
[*] Key loaded from 'secret.key' | ||
|
||
Original Message: This is a secret message. | ||
Encrypted Message: b'gAAAAABlZ...' | ||
Decrypted Message: This is a secret message. | ||
``` | ||
|
||
--- | ||
|
||
## 🤝 Contributing | ||
|
||
Contributions are welcome! You can help by: | ||
- Improving the CLI interface | ||
- Adding file encryption support | ||
- Implementing password-based key derivation | ||
- Writing unit tests | ||
|
||
To contribute: | ||
1. Fork the repo | ||
2. Create a new branch (`git checkout -b my-feature`) | ||
3. Commit your changes | ||
4. Push and create a Pull Request | ||
|
||
--- | ||
|
||
## 📜 License | ||
|
||
This project is licensed under the **MIT License**. See [LICENSE](LICENSE) for details. | ||
|
||
--- | ||
|
||
## 👨💻 Author | ||
|
||
**Afolabi Adewale** | ||
A data and security enthusiast exploring the intersection of Python, encryption, and open-source software. | ||
[GitHub Profile](https://github.com/your-username) | ||
|
||
--- | ||
|
||
## 🔗 Related Resources | ||
|
||
- [Python `cryptography` docs](https://cryptography.io/en/latest/) | ||
- [Understanding Symmetric vs Asymmetric Encryption](https://www.cloudflare.com/learning/ssl/how-does-ssl-work/) | ||
- [OWASP Crypto Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html) |
72 changes: 72 additions & 0 deletions
OTHERS/Encryption/symmetricEncryption.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# simple_encryption.py | ||
|
||
""" | ||
A simple example of symmetric encryption using Python's 'cryptography' package. | ||
|
||
This script: | ||
1. Generates a secure encryption key | ||
2. Encrypts a message using the key | ||
3. Decrypts it back to the original message | ||
|
||
Author: Afolabi Adewale | ||
""" | ||
|
||
from cryptography.fernet import Fernet | ||
|
||
# 1. Generate a key for encryption and decryption | ||
def generate_key(): | ||
""" | ||
Generates a symmetric key for Fernet (uses AES encryption internally). | ||
""" | ||
key = Fernet.generate_key() | ||
with open("secret.key", "wb") as key_file: | ||
key_file.write(key) | ||
print("[+] Key generated and saved to 'secret.key'") | ||
return key | ||
|
||
# 2. Load the existing key from file | ||
def load_key(): | ||
""" | ||
Loads the previously generated key from the file. | ||
""" | ||
return open("secret.key", "rb").read() | ||
|
||
# 3. Encrypt a message | ||
def encrypt_message(message: str, key: bytes) -> bytes: | ||
""" | ||
Encrypts a message using the provided symmetric key. | ||
""" | ||
f = Fernet(key) | ||
encrypted = f.encrypt(message.encode()) | ||
return encrypted | ||
|
||
|
||
# 4. Decrypt a message | ||
def decrypt_message(encrypted_message: bytes, key: bytes) -> str: | ||
""" | ||
Decrypts an encrypted message using the same symmetric key. | ||
""" | ||
f = Fernet(key) | ||
decrypted = f.decrypt(encrypted_message) | ||
return decrypted.decode() | ||
|
||
# 5. Main runner | ||
if __name__ == "__main__": | ||
# Create or load the key | ||
try: | ||
key = load_key() | ||
print("[*] Key loaded from 'secret.key'") | ||
except FileNotFoundError: | ||
key = generate_key() | ||
|
||
# Example message | ||
message = "This is a secret message." | ||
print(f"\nOriginal Message: {message}") | ||
|
||
# Encrypt it | ||
encrypted = encrypt_message(message, key) | ||
print(f"Encrypted Message: {encrypted}") | ||
|
||
# Decrypt it | ||
decrypted = decrypt_message(encrypted, key) | ||
print(f"Decrypted Message: {decrypted}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.