Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,3 @@
|
|
| 1 |
-
# Initialize session state variables if they don't exist
|
| 2 |
-
if 'failed_attempts' not in streamlit.session_state:
|
| 3 |
-
streamlit.session_state.failed_attempts = 0
|
| 4 |
-
if 'stored_data' not in streamlit.session_state:
|
| 5 |
-
streamlit.session_state.stored_data = {}
|
| 6 |
-
if 'current_page' not in streamlit.session_state:
|
| 7 |
-
streamlit.session_state.current_page = "Home"
|
| 8 |
-
if 'last_attempt_time' not in streamlit.session_state:
|
| 9 |
-
streamlit.session_state.last_attempt_time = 0
|
| 10 |
|
| 11 |
# Function to hash passkey
|
| 12 |
def hash_passkey(passkey):
|
|
@@ -37,16 +28,11 @@ def decrypt_data(encrypted_text, passkey, data_id):
|
|
| 37 |
streamlit.session_state.failed_attempts += 1
|
| 38 |
streamlit.session_state.last_attempt_time = time.time()
|
| 39 |
return None
|
| 40 |
-
except Exception
|
| 41 |
streamlit.session_state.failed_attempts += 1
|
| 42 |
streamlit.session_state.last_attempt_time = time.time()
|
| 43 |
return None
|
| 44 |
|
| 45 |
-
# Function to generate a unique ID for data
|
| 46 |
-
def generate_data_id():
|
| 47 |
-
import uuid
|
| 48 |
-
return str(uuid.uuid4())
|
| 49 |
-
|
| 50 |
# Function to reset failed attempts
|
| 51 |
def reset_failed_attempts():
|
| 52 |
streamlit.session_state.failed_attempts = 0
|
|
@@ -55,108 +41,125 @@ def reset_failed_attempts():
|
|
| 55 |
def change_page(page):
|
| 56 |
streamlit.session_state.current_page = page
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
streamlit.session_state
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
encrypted_text = streamlit.session_state.stored_data[data_id]["encrypted_text"]
|
| 125 |
-
decrypted_text = decrypt_data(encrypted_text, passkey, data_id)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
else:
|
| 132 |
-
streamlit.error(
|
| 133 |
-
else:
|
| 134 |
-
streamlit.error("β Data ID not found!")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
streamlit.rerun()
|
| 140 |
-
else:
|
| 141 |
-
streamlit.error("β οΈ Both fields are required!")
|
| 142 |
-
|
| 143 |
-
elif streamlit.session_state.current_page == "Login":
|
| 144 |
-
streamlit.subheader("π Reauthorization Required")
|
| 145 |
-
|
| 146 |
-
if time.time() - streamlit.session_state.last_attempt_time < 10 and streamlit.session_state.failed_attempts >= 3:
|
| 147 |
-
remaining_time = int(10 - (time.time() - streamlit.session_state.last_attempt_time))
|
| 148 |
-
streamlit.warning(f"π Please wait {remaining_time} seconds before trying again.")
|
| 149 |
-
else:
|
| 150 |
-
login_pass = streamlit.text_input("Enter Master Password:", type="password")
|
| 151 |
-
|
| 152 |
-
if streamlit.button("Login"):
|
| 153 |
-
if login_pass == "admin123":
|
| 154 |
-
reset_failed_attempts()
|
| 155 |
-
streamlit.success("β
Reauthorized successfully!")
|
| 156 |
-
streamlit.session_state.current_page = "Home"
|
| 157 |
-
streamlit.rerun()
|
| 158 |
-
else:
|
| 159 |
-
streamlit.error("β Incorrect password!")
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
# Function to hash passkey
|
| 3 |
def hash_passkey(passkey):
|
|
|
|
| 28 |
streamlit.session_state.failed_attempts += 1
|
| 29 |
streamlit.session_state.last_attempt_time = time.time()
|
| 30 |
return None
|
| 31 |
+
except Exception:
|
| 32 |
streamlit.session_state.failed_attempts += 1
|
| 33 |
streamlit.session_state.last_attempt_time = time.time()
|
| 34 |
return None
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Function to reset failed attempts
|
| 37 |
def reset_failed_attempts():
|
| 38 |
streamlit.session_state.failed_attempts = 0
|
|
|
|
| 41 |
def change_page(page):
|
| 42 |
streamlit.session_state.current_page = page
|
| 43 |
|
| 44 |
+
def main():
|
| 45 |
+
# Initialize session state variables
|
| 46 |
+
if 'failed_attempts' not in streamlit.session_state:
|
| 47 |
+
streamlit.session_state.failed_attempts = 0
|
| 48 |
+
if 'stored_data' not in streamlit.session_state:
|
| 49 |
+
streamlit.session_state.stored_data = {}
|
| 50 |
+
if 'current_page' not in streamlit.session_state:
|
| 51 |
+
streamlit.session_state.current_page = "Home"
|
| 52 |
+
if 'last_attempt_time' not in streamlit.session_state:
|
| 53 |
+
streamlit.session_state.last_attempt_time = 0
|
| 54 |
+
|
| 55 |
+
streamlit.title("π Secure Data Encryption System")
|
| 56 |
+
|
| 57 |
+
# Navigation
|
| 58 |
+
menu = ["Home", "Store Data", "Retrieve Data", "Login"]
|
| 59 |
+
choice = streamlit.sidebar.selectbox("Navigation", menu, index=menu.index(streamlit.session_state.current_page))
|
| 60 |
+
streamlit.session_state.current_page = choice
|
| 61 |
+
|
| 62 |
+
# Block if too many failed attempts
|
| 63 |
+
if streamlit.session_state.failed_attempts >= 3:
|
| 64 |
+
streamlit.session_state.current_page = "Login"
|
| 65 |
+
streamlit.warning("π Too many failed attempts! Reauthorization required.")
|
| 66 |
+
|
| 67 |
+
# HOME PAGE
|
| 68 |
+
if streamlit.session_state.current_page == "Home":
|
| 69 |
+
streamlit.subheader("π Welcome to the Secure Data System")
|
| 70 |
+
streamlit.write("Use this app to **securely store and retrieve data** using unique passkeys.")
|
| 71 |
+
|
| 72 |
+
col1, col2 = streamlit.columns(2)
|
| 73 |
+
with col1:
|
| 74 |
+
if streamlit.button("Store New Data", use_container_width=True):
|
| 75 |
+
change_page("Store Data")
|
| 76 |
+
with col2:
|
| 77 |
+
if streamlit.button("Retrieve Data", use_container_width=True):
|
| 78 |
+
change_page("Retrieve Data")
|
| 79 |
+
|
| 80 |
+
streamlit.info(f"Currently storing {len(streamlit.session_state.stored_data)} encrypted data entries.")
|
| 81 |
+
|
| 82 |
+
# STORE DATA PAGE
|
| 83 |
+
elif streamlit.session_state.current_page == "Store Data":
|
| 84 |
+
streamlit.subheader("π Store Data Securely")
|
| 85 |
+
user_data = streamlit.text_area("Enter Data:")
|
| 86 |
+
passkey = streamlit.text_input("Enter Passkey:", type="password")
|
| 87 |
+
confirm_passkey = streamlit.text_input("Confirm Passkey:", type="password")
|
| 88 |
+
|
| 89 |
+
if streamlit.button("Encrypt & Save"):
|
| 90 |
+
if user_data and passkey and confirm_passkey:
|
| 91 |
+
if passkey != confirm_passkey:
|
| 92 |
+
streamlit.error("β οΈ Passkeys do not match!")
|
| 93 |
+
else:
|
| 94 |
+
data_id = str(uuid.uuid4())
|
| 95 |
+
hashed_passkey = hash_passkey(passkey)
|
| 96 |
+
encrypted_text = encrypt_data(user_data, passkey)
|
| 97 |
+
|
| 98 |
+
streamlit.session_state.stored_data[data_id] = {
|
| 99 |
+
"encrypted_text": encrypted_text,
|
| 100 |
+
"passkey": hashed_passkey
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
streamlit.success("β
Data stored securely!")
|
| 104 |
+
streamlit.code(data_id, language="text")
|
| 105 |
+
streamlit.info("β οΈ Save this Data ID! You'll need it to retrieve your data.")
|
| 106 |
else:
|
| 107 |
+
streamlit.error("β οΈ All fields are required!")
|
| 108 |
+
|
| 109 |
+
# RETRIEVE DATA PAGE
|
| 110 |
+
elif streamlit.session_state.current_page == "Retrieve Data":
|
| 111 |
+
streamlit.subheader("π Retrieve Your Data")
|
| 112 |
+
attempts_remaining = 3 - streamlit.session_state.failed_attempts
|
| 113 |
+
streamlit.info(f"Attempts remaining: {attempts_remaining}")
|
| 114 |
+
|
| 115 |
+
data_id = streamlit.text_input("Enter Data ID:")
|
| 116 |
+
passkey = streamlit.text_input("Enter Passkey:", type="password")
|
| 117 |
+
|
| 118 |
+
if streamlit.button("Decrypt"):
|
| 119 |
+
if data_id and passkey:
|
| 120 |
+
if data_id in streamlit.session_state.stored_data:
|
| 121 |
+
encrypted_text = streamlit.session_state.stored_data[data_id]["encrypted_text"]
|
| 122 |
+
decrypted_text = decrypt_data(encrypted_text, passkey, data_id)
|
| 123 |
+
|
| 124 |
+
if decrypted_text:
|
| 125 |
+
streamlit.success("β
Decryption successful!")
|
| 126 |
+
streamlit.markdown("### Your Decrypted Data:")
|
| 127 |
+
streamlit.code(decrypted_text, language="text")
|
| 128 |
+
else:
|
| 129 |
+
streamlit.error(f"β Incorrect passkey! Attempts remaining: {3 - streamlit.session_state.failed_attempts}")
|
| 130 |
+
else:
|
| 131 |
+
streamlit.error("β Data ID not found!")
|
| 132 |
|
| 133 |
+
if streamlit.session_state.failed_attempts >= 3:
|
| 134 |
+
streamlit.warning("π Too many failed attempts! Redirecting to Login Page.")
|
| 135 |
+
streamlit.session_state.current_page = "Login"
|
| 136 |
+
streamlit.rerun()
|
| 137 |
+
else:
|
| 138 |
+
streamlit.error("β οΈ Both fields are required!")
|
| 139 |
|
| 140 |
+
# LOGIN PAGE
|
| 141 |
+
elif streamlit.session_state.current_page == "Login":
|
| 142 |
+
streamlit.subheader("π Reauthorization Required")
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
if time.time() - streamlit.session_state.last_attempt_time < 10 and streamlit.session_state.failed_attempts >= 3:
|
| 145 |
+
remaining_time = int(10 - (time.time() - streamlit.session_state.last_attempt_time))
|
| 146 |
+
streamlit.warning(f"π Please wait {remaining_time} seconds before trying again.")
|
| 147 |
+
else:
|
| 148 |
+
login_pass = streamlit.text_input("Enter Master Password:", type="password")
|
| 149 |
+
|
| 150 |
+
if streamlit.button("Login"):
|
| 151 |
+
if login_pass == "admin123":
|
| 152 |
+
reset_failed_attempts()
|
| 153 |
+
streamlit.success("β
Reauthorized successfully!")
|
| 154 |
+
streamlit.session_state.current_page = "Home"
|
| 155 |
+
streamlit.rerun()
|
| 156 |
else:
|
| 157 |
+
streamlit.error("β Incorrect password!")
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
# Footer
|
| 160 |
+
streamlit.markdown("---")
|
| 161 |
+
streamlit.markdown("π Secure Data Encryption System | Educational Project")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# Run app
|
| 164 |
+
if __name__ == "__main__":
|
| 165 |
+
main()
|