from pymongo import MongoClient from cryptography.fernet import Fernet import json # MongoDB connection string connection_string = "mongodb+srv://soham2000:soham2000@cluster0.lbu4i.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0" # Encryption key (generate this once and store securely) # encryption_key = Fernet.generate_key() cipher = Fernet(b"kFRvFTtpl6WDr0eceOteS5IAnv3ps0YtCavmUwllO0k=") # Replace with your actual password in the connection string client = MongoClient(connection_string) db = client['nodetest'] # Database name collection = db['logs'] # Collection name file_path = "audit_logs.json" # Path to your logs file with open(file_path, 'r') as file: logs = json.load(file) # Encrypt and insert logs for log in logs: log_json = json.dumps(log) # Convert the log object to JSON string encrypted_log = cipher.encrypt(log_json.encode()) # Encrypt the JSON string collection.insert_one({"encrypted_data": encrypted_log}) # Insert encrypted log print("Logs have been encrypted and inserted into the database.") # Decrypt a log (for testing) for encrypted_record in collection.find(): encrypted_data = encrypted_record['encrypted_data'] decrypted_data = cipher.decrypt(encrypted_data).decode() print("Decrypted Log:", json.loads(decrypted_data))