Data Encryption Tool
Abstract
Data Encryption Tool is a Python project that encrypts and decrypts data. The application features cryptography, file management, and a CLI interface, demonstrating best practices in security and data protection.
Prerequisites
- Python 3.8 or above
- A code editor or IDE
- Basic understanding of cryptography
- Required libraries:
cryptography
cryptography
Before you Start
Install Python and the required libraries:
Install dependencies
pip install cryptography
Install dependencies
pip install cryptography
Getting Started
Create a Project
- Create a folder named
data-encryption-tool
data-encryption-tool
. - Open the folder in your code editor or IDE.
- Create a file named
data_encryption_tool.py
data_encryption_tool.py
. - Copy the code below into your file.
Write the Code
⚙️ Data Encryption Tool
Data Encryption Tool
from cryptography.fernet import Fernet
class DataEncryptionTool:
def __init__(self):
self.key = Fernet.generate_key()
self.cipher = Fernet(self.key)
def encrypt(self, data):
encrypted = self.cipher.encrypt(data.encode())
print(f"Encrypted: {encrypted}")
return encrypted
def decrypt(self, encrypted):
decrypted = self.cipher.decrypt(encrypted).decode()
print(f"Decrypted: {decrypted}")
return decrypted
if __name__ == "__main__":
print("Data Encryption Tool Demo")
tool = DataEncryptionTool()
secret = tool.encrypt("Sensitive information")
tool.decrypt(secret)
Data Encryption Tool
from cryptography.fernet import Fernet
class DataEncryptionTool:
def __init__(self):
self.key = Fernet.generate_key()
self.cipher = Fernet(self.key)
def encrypt(self, data):
encrypted = self.cipher.encrypt(data.encode())
print(f"Encrypted: {encrypted}")
return encrypted
def decrypt(self, encrypted):
decrypted = self.cipher.decrypt(encrypted).decode()
print(f"Decrypted: {decrypted}")
return decrypted
if __name__ == "__main__":
print("Data Encryption Tool Demo")
tool = DataEncryptionTool()
secret = tool.encrypt("Sensitive information")
tool.decrypt(secret)
Example Usage
Run encryption tool
python data_encryption_tool.py
Run encryption tool
python data_encryption_tool.py
Explanation
Key Features
- Encryption/Decryption: Secures data using cryptography.
- File Management: Handles file input/output.
- Error Handling: Validates inputs and manages exceptions.
- CLI Interface: Interactive command-line usage.
Code Breakdown
- Import Libraries and Setup Tool
data_encryption_tool.py
from cryptography.fernet import Fernet
data_encryption_tool.py
from cryptography.fernet import Fernet
- Encryption and Decryption Functions
data_encryption_tool.py
def generate_key():
return Fernet.generate_key()
def encrypt_data(data, key):
f = Fernet(key)
return f.encrypt(data.encode())
def decrypt_data(token, key):
f = Fernet(key)
return f.decrypt(token).decode()
data_encryption_tool.py
def generate_key():
return Fernet.generate_key()
def encrypt_data(data, key):
f = Fernet(key)
return f.encrypt(data.encode())
def decrypt_data(token, key):
f = Fernet(key)
return f.decrypt(token).decode()
- CLI Interface and Error Handling
data_encryption_tool.py
def main():
print("Data Encryption Tool")
key = generate_key()
while True:
cmd = input('> ')
if cmd == 'encrypt':
data = input("Data to encrypt: ")
encrypted = encrypt_data(data, key)
print(f"Encrypted: {encrypted}")
elif cmd == 'decrypt':
token = input("Data to decrypt: ").encode()
try:
decrypted = decrypt_data(token, key)
print(f"Decrypted: {decrypted}")
except Exception as e:
print(f"Error: {e}")
elif cmd == 'exit':
break
else:
print("Unknown command. Type 'encrypt', 'decrypt', or 'exit'.")
if __name__ == "__main__":
main()
data_encryption_tool.py
def main():
print("Data Encryption Tool")
key = generate_key()
while True:
cmd = input('> ')
if cmd == 'encrypt':
data = input("Data to encrypt: ")
encrypted = encrypt_data(data, key)
print(f"Encrypted: {encrypted}")
elif cmd == 'decrypt':
token = input("Data to decrypt: ").encode()
try:
decrypted = decrypt_data(token, key)
print(f"Decrypted: {decrypted}")
except Exception as e:
print(f"Error: {e}")
elif cmd == 'exit':
break
else:
print("Unknown command. Type 'encrypt', 'decrypt', or 'exit'.")
if __name__ == "__main__":
main()
Features
- Data Encryption: Cryptography and file management
- Modular Design: Separate functions for each task
- Error Handling: Manages invalid inputs and exceptions
- Production-Ready: Scalable and maintainable code
Next Steps
Enhance the project by:
- Integrating with real-world datasets
- Supporting advanced encryption algorithms
- Creating a GUI for encryption
- Adding file encryption/decryption
- Unit testing for reliability
Educational Value
This project teaches:
- Security: Encryption and decryption
- Software Design: Modular, maintainable code
- Error Handling: Writing robust Python code
Real-World Applications
- Data Protection Platforms
- Secure File Management
- Enterprise Security
Conclusion
Data Encryption Tool demonstrates how to build a scalable and secure encryption tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in security, enterprise, and more. For more advanced projects, visit Python Central Hub.
Was this page helpful?
Let us know how we did