diff --git a/OTHERS/Encryption/README.md b/OTHERS/Encryption/README.md new file mode 100644 index 00000000..46b85add --- /dev/null +++ b/OTHERS/Encryption/README.md @@ -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) diff --git a/OTHERS/Encryption/symmetricEncryption.py b/OTHERS/Encryption/symmetricEncryption.py new file mode 100644 index 00000000..9d132e07 --- /dev/null +++ b/OTHERS/Encryption/symmetricEncryption.py @@ -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}")