# Practical 1 – Security Goals
# Install: pip install cryptography
from cryptography.fernet import Fernet
import hashlib
import shutil
from datetime import datetime

# 1. Confidentiality: encrypt and decrypt a message
key = Fernet.generate_key()
cipher = Fernet(key)
encrypted = cipher.encrypt(b"Secret Message!!!")
print("Key:", key)
print("Encrypted:", encrypted)
print("Decrypted:", cipher.decrypt(encrypted).decode())

# 2. Integrity: compare message hashes
first = hashlib.sha256(b"This is important").hexdigest()
changed = hashlib.sha256(b"This is IMPORTANT").hexdigest()
print("Hashes:", first, changed)
print("Integrity verified?", first == changed)

# 3. Availability: make a backup
shutil.copyfile("data.txt", "backup_data.txt")
print("Backup completed!")

# 4. Authentication: check a username and password
users = {"admin": "password123", "user1": "hello123"}
if users.get(input("Username: ")) == input("Password: "):
    print("Access granted")
else:
    print("Access denied")

# 5. Non-repudiation concept: compare message digests
message = b"Approve $5000 transfer"
signature = hashlib.sha256(message).hexdigest()
received = hashlib.sha256(message).hexdigest()
print("Signature verified:", signature == received)

# 6. Accountability: record user actions
with open("audit_log.txt", "a") as log:
    for user, action in [("admin", "Logged in"), ("user1", "Viewed report")]:
        log.write(f"{datetime.now()} - {user}: {action}\n")

# What this code does: demonstrates confidentiality, integrity, availability,
# authentication, a simple digest comparison, and activity logging.
