Install Requirements
pip install face_recognition opencv-python cryptography
Capture Reference Face
# Optional: capture face from webcam and save as known_face.jpg
import cv2
cam = cv2.VideoCapture(0)
ret, frame = cam.read()
cv2.imwrite("known_face.jpg", frame)
cam.release()
print("Reference face saved.")
Secure Diary Code
import face_recognition
import cv2
import os
from cryptography.fernet import Fernet
from getpass import getpass
import base64
KEY_FILE = "secret.key"
ENC_FILE = "diary.enc"
KNOWN_FACE_FILE = "known_face.jpg"
# === Face Unlock ===
def verify_face():
if not os.path.exists(KNOWN_FACE_FILE):
print("Known face image not found.")
return False
known_image = face_recognition.load_image_file(KNOWN_FACE_FILE)
known_encoding = face_recognition.face_encodings(known_image)[0]
cam = cv2.VideoCapture(0)
print("Looking for face...")
ret, frame = cam.read()
cam.release()
try:
unknown_encoding = face_recognition.face_encodings(frame)[0]
result = face_recognition.compare_faces([known_encoding], unknown_encoding)[0]
return result
except IndexError:
print("No face detected.")
return False
# === Encryption Helpers ===
def generate_key(password):
return base64.urlsafe_b64encode(password.encode().ljust(32)[:32])
def encrypt_diary(content, key):
f = Fernet(key)
with open(ENC_FILE, "wb") as file:
file.write(f.encrypt(content.encode()))
print("Diary encrypted & saved.")
def decrypt_diary(key):
f = Fernet(key)
with open(ENC_FILE, "rb") as file:
data = file.read()
return f.decrypt(data).decode()
# === Main Diary App ===
def diary_app():
print("š Secure Diary Access")
if not verify_face():
print("Face verification failed. Access denied.")
return
password = getpass("Enter your diary password: ")
key = generate_key(password)
if os.path.exists(ENC_FILE):
try:
decrypted = decrypt_diary(key)
print("\nš Your Diary:\n", decrypted)
except:
print("Failed to decrypt. Wrong password?")
return
else:
print("No diary found. Creating new one...")
new_entry = input("\n✍️ Write new entry (append to diary):\n> ")
combined = decrypted + "\n\n" + new_entry if 'decrypted' in locals() else new_entry
encrypt_diary(combined, key)
if __name__ == "__main__":
diary_app()
No comments:
Post a Comment