1+ from cryptography import fernet
2+ import os
3+
4+ KEY_FILE = "secret.key"
5+ PASSWORD_FILE = "saved_passwords.txt"
6+
7+ # STEP 1: Load the encryption key
8+ def load_key ():
9+ if not os .path .exists (KEY_FILE ):
10+ print ("❌ Encryption key not found. Cannot decrypt passwords." )
11+ return None
12+ with open (KEY_FILE , "rb" ) as f :
13+ return f .read ()
14+
15+ # STEP 2: Decrypt the passwords
16+ def decrypt_password (encrypted_text , fernet_cipher_suite ): # Renamed parameter for clarity
17+ try :
18+ return fernet_cipher_suite .decrypt (encrypted_text .encode ()).decode ()
19+ except Exception :
20+ return "[Decryption Failed]"
21+
22+ # STEP 3: Read and decrypt all entries
23+ def view_passwords ():
24+ if not os .path .exists (PASSWORD_FILE ):
25+ print ("❌ No saved passwords found" )
26+ return
27+
28+ key = load_key ()
29+ if not key :
30+ return
31+
32+ # Fernet = fernet(key) # Original line causing TypeError
33+ active_fernet_cipher = fernet .Fernet (key ) # Correctly instantiate Fernet class from the module
34+
35+ print ("🔐 Saved Encrypted Passwords\n " + "=" * 40 )
36+ with open (PASSWORD_FILE , "r" ) as file :
37+ lines = file .readlines ()
38+
39+ current_block = {}
40+
41+ for line in lines :
42+ line = line .strip ()
43+
44+ if line .startswith ("[" ) and "]" in line :
45+ current_block ["timestamp" ] = line .strip ("[]" )
46+ elif line .startswith ("Label:" ):
47+ current_block ["label" ] = line .split ("Label:" )[1 ].strip ()
48+ elif line .startswith ("Encrypted Password:" ):
49+ current_block ["encrypted" ] = line .split ("Encrypted Password:" )[1 ].strip ()
50+ elif line .startswith ("Included -" ):
51+ current_block ["options" ] = line .split ("Included -" )[1 ].strip ()
52+ elif line .startswith ("-" * 10 ):
53+ # print everything together
54+ print (f"\n 📅 Date/Time: { current_block .get ('timestamp' , '[Unknown]' )} " )
55+ print (f"🏷️ Label: { current_block .get ('label' , '[None]' )} " )
56+ print (f"🔓 Password: { decrypt_password (current_block .get ('encrypted' , '' ), active_fernet_cipher )} " ) # Pass the Fernet instance
57+ print (f"⚙️ Options: { current_block .get ('options' , '[Not specified]' )} " )
58+ print ("-" * 40 )
59+ current_block = {} # Reset for next block
60+
61+ # STEP 4: Entry point
62+ if __name__ == '__main__' :
63+ view_passwords ()
0 commit comments