Skip to content

Instantly share code, notes, and snippets.

@copley
Created September 7, 2024 09:08
Show Gist options
  • Save copley/4427e39ac7d1f1f1b6bebdff4b6b0caf to your computer and use it in GitHub Desktop.
Save copley/4427e39ac7d1f1f1b6bebdff4b6b0caf to your computer and use it in GitHub Desktop.
Password Time Capsule python3 app
import time
from datetime import datetime, timedelta
from tinydb import TinyDB, Query
from cryptography.fernet import Fernet
import base64
import os
# Generate a key for encryption
def generate_key():
return base64.urlsafe_b64encode(os.urandom(32))
# Encrypt the data using Fernet encryption
def encrypt_data(data, key):
f = Fernet(key)
encrypted_data = f.encrypt(data.encode())
return encrypted_data
# Decrypt the data using Fernet encryption
def decrypt_data(encrypted_data, key):
f = Fernet(key)
decrypted_data = f.decrypt(encrypted_data).decode()
return decrypted_data
# Initialize database
db = TinyDB('password_manager_db.json')
# Generate or load encryption key
if not os.path.exists('encryption.key'):
key = generate_key()
with open('encryption.key', 'wb') as key_file:
key_file.write(key)
else:
with open('encryption.key', 'rb') as key_file:
key = key_file.read()
# Function to add a new password entry
def add_password_entry():
data = input("Enter the data (e.g., password or text): ")
encrypted_data = encrypt_data(data, key)
timestamp = datetime.now()
db.insert({'data': encrypted_data.decode(), 'timestamp': timestamp.isoformat()})
print("Data stored successfully and will be accessible after 30 seconds.")
# Function to access the password entry after 30 days
def access_password_entry():
entry = db.all()
if not entry:
print("No data stored.")
return
for item in entry:
encrypted_data = item['data']
timestamp = datetime.fromisoformat(item['timestamp'])
if datetime.now() < timestamp + timedelta(seconds=30):
print("Data is still locked. Please wait until 30 seconds have passed.")
else:
decrypted_data = decrypt_data(encrypted_data.encode(), key)
print(f"Data after decryption: {decrypted_data}")
# Main function to run the app
def main():
print("Password Manager with Timed Access")
print("1. Add new password/data")
print("2. Access stored data")
choice = input("Enter your choice (1 or 2): ")
if choice == '1':
add_password_entry()
elif choice == '2':
access_password_entry()
else:
print("Invalid choice. Exiting.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment