├── LICENSE ├── main.py └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Prince 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. 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from cryptography.fernet import Fernet 2 | 3 | # Function to generate a key and save it into a file 4 | def write_key(): 5 | key = Fernet.generate_key() 6 | with open("secret.key", "wb") as key_file: 7 | key_file.write(key) 8 | 9 | # Function to load the key from the file 10 | def load_key(): 11 | return open("secret.key", "rb").read() 12 | 13 | # Function to encrypt a message 14 | def encrypt_message(message): 15 | key = load_key() 16 | fernet = Fernet(key) 17 | encrypted_message = fernet.encrypt(message.encode()) 18 | return encrypted_message 19 | 20 | # Function to decrypt a message 21 | def decrypt_message(encrypted_message): 22 | key = load_key() 23 | fernet = Fernet(key) 24 | decrypted_message = fernet.decrypt(encrypted_message).decode() 25 | return decrypted_message 26 | 27 | def main(): 28 | choice = input("Choose (E)ncrypt or (D)ecrypt: ").strip().upper() 29 | if choice == 'E': 30 | message = input("Enter the message to encrypt: ").strip() 31 | encrypted_message = encrypt_message(message) 32 | print(f"Encrypted message: {encrypted_message.decode()}") 33 | elif choice == 'D': 34 | encrypted_message = input("Enter the message to decrypt: ").strip().encode() 35 | try: 36 | decrypted_message = decrypt_message(encrypted_message) 37 | print(f"Decrypted message: {decrypted_message}") 38 | except Exception as e: 39 | print("An error occurred during decryption. Please check the input.") 40 | else: 41 | print("Invalid choice! Please choose either 'E' for encryption or 'D' for decryption.") 42 | 43 | if __name__ == "__main__": 44 | # Check if the key file exists, if not, generate it 45 | try: 46 | load_key() 47 | except FileNotFoundError: 48 | write_key() 49 | 50 | main() 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🔒 Encryption and Decryption Program 2 | 3 | This is a Python program that uses the `cryptography` library's `Fernet` method for encryption and decryption. It provides a simple command-line interface to choose between encrypting and decrypting a message. 4 | 5 | ## 🚀 Features 6 | 7 | - Encrypt any message using the Fernet encryption method. 8 | - Decrypt any previously encrypted message using the same method. 9 | - Automatically generates and uses a secret key for encryption and decryption. 10 | 11 | ## 📦 Installation 12 | 13 | 1. Clone the repository: 14 | 15 | ```bash 16 | git clone https://github.com/yourusername/encryption-decryption.git 17 | cd encryption-decryption 18 | ``` 19 | 20 | 2. Install the required dependencies: 21 | 22 | ```bash 23 | pip install cryptography 24 | ``` 25 | 26 | ## 🛠️ Usage 27 | 28 | 1. Run the program: 29 | 30 | ```bash 31 | python main.py 32 | ``` 33 | 34 | 2. Choose between encryption (`E`) and decryption (`D`): 35 | 36 | - For encryption, enter the message you want to encrypt. 37 | - For decryption, enter the encrypted message. 38 | 39 | ## 🔑 Key Management 40 | 41 | - The program generates a `secret.key` file if it doesn't exist. 42 | - This key is used for both encryption and decryption. 43 | - **Keep the `secret.key` file safe**. Losing this file will make it impossible to decrypt any messages encrypted with it. 44 | 45 | ## 📄 Example 46 | 47 | ```plaintext 48 | Choose (E)ncrypt or (D)ecrypt: E 49 | Enter the message to encrypt: Hello, world! 50 | Encrypted message: gAAAAABeJc2V-MvhF7L3qfjzIHHGJ9tF5MEP3Gj2Pi8Y5kbU6iE4z6MSv1JazbTtXRXFwG1Y1NRpkRznlx0Kigc= 51 | ``` 52 | 53 | ```plaintext 54 | Choose (E)ncrypt or (D)ecrypt: D 55 | Enter the message to decrypt: gAAAAABeJc2V-MvhF7L3qfjzIHHGJ9tF5MEP3Gj2Pi8Y5kbU6iE4z6MSv1JazbTtXRXFwG1Y1NRpkRznlx0Kigc= 56 | Decrypted message: Hello, world! 57 | ``` 58 | 59 | ## 📂 File Structure 60 | 61 | ```plaintext 62 | encryption-decryption/ 63 | │ 64 | ├── main.py # The main script to run the program 65 | ├── secret.key # The secret key file (auto-generated) 66 | └── README.md # This README file 67 | ``` 68 | 69 | ## 🤝 Contributing 70 | 71 | 1. Fork the repository. 72 | 2. Create a new branch (`git checkout -b feature-branch`). 73 | 3. Make your changes. 74 | 4. Commit your changes (`git commit -am 'Add new feature'`). 75 | 5. Push to the branch (`git push origin feature-branch`). 76 | 6. Create a new Pull Request. 77 | 78 | ## 📜 License 79 | 80 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 81 | 82 | 83 | --- 84 | 85 | Made with ❤️ by [Prince](https://github.com/pkprajapati7402) 86 | ``` 87 | --------------------------------------------------------------------------------