-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.py
More file actions
265 lines (221 loc) Β· 10.4 KB
/
encrypt.py
File metadata and controls
265 lines (221 loc) Β· 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import streamlit as st
from encryption_utils import encryptionfile as utils
from encryption_utils.hashing_utils import check_password_strength
# Configure page settings with blue-black theme
st.set_page_config(
page_title="File Encryption Tool",
page_icon="π",
layout="wide",
initial_sidebar_state="collapsed"
)
# Custom CSS for blue-black theme
st.markdown("""
<style>
.stApp {
background: linear-gradient(135deg, #0f1419 0%, #1a2332 50%, #0a0e13 100%);
color: #e6f3ff;
}
.stFileUploader > div {
background: rgba(30, 58, 95, 0.3);
border: 2px dashed #2d5aa0;
border-radius: 10px;
padding: 2rem;
}
.stTextInput > div > div > input {
background: rgba(30, 58, 95, 0.5);
color: #e6f3ff;
border: 1px solid #2d5aa0;
border-radius: 5px;
}
.stButton > button {
background: linear-gradient(45deg, #2d5aa0 0%, #1e3a5f 100%);
color: white;
border: none;
border-radius: 25px;
padding: 0.5rem 2rem;
font-weight: bold;
transition: all 0.3s ease;
}
.stButton > button:hover {
background: linear-gradient(45deg, #3d6ab0 0%, #2e4a6f 100%);
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(45, 90, 160, 0.4);
}
.stDownloadButton > button {
background: linear-gradient(45deg, #0d7377 0%, #14a085 100%);
color: white;
border: none;
border-radius: 25px;
padding: 0.5rem 2rem;
font-weight: bold;
}
.page-content {
padding: 1rem;
margin-top: 1rem;
}
.success-message {
background: linear-gradient(45deg, #0d7377 0%, #14a085 100%);
color: white;
padding: 1rem;
border-radius: 10px;
margin: 1rem 0;
}
.error-message {
background: linear-gradient(45deg, #d73502 0%, #f44336 100%);
color: white;
padding: 1rem;
border-radius: 10px;
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state for navigation
if 'current_page' not in st.session_state:
st.session_state.current_page = 'encrypt'
# Navigation header
col1, col2 = st.columns(2)
with col1:
if st.button("π Encrypt", key="encrypt_nav"):
st.session_state.current_page = 'encrypt'
with col2:
if st.button("π Decrypt", key="decrypt_nav"):
st.session_state.current_page = 'decrypt'
# Add this function for better file validation
def validate_file_upload(uploaded_file, max_size_mb=10240): # 10GB = 10240MB
"""Validate uploaded file size and type"""
if uploaded_file is None:
return False, "No file uploaded"
file_size_mb = uploaded_file.size / (1024 * 1024)
file_size_gb = file_size_mb / 1024
if file_size_mb > max_size_mb:
return False, f"File too large ({file_size_gb:.2f}GB). Maximum allowed: {max_size_mb/1024}GB"
# Display size in appropriate units
if file_size_gb >= 1:
return True, f"File validated successfully ({file_size_gb:.2f}GB)"
else:
return True, f"File validated successfully ({file_size_mb:.1f}MB)"
def chunked_file_processing(uploaded_file, chunk_size=100*1024*1024): # 100MB chunks for better performance
"""Process large files in chunks to avoid memory issues"""
file_size_gb = uploaded_file.size / (1024 * 1024 * 1024)
if uploaded_file.size <= chunk_size:
return uploaded_file
st.info(f"π Large file detected ({file_size_gb:.2f}GB). Optimizing processing...")
return uploaded_file # For now, return as-is, but you can implement chunking logic
# Page content based on current selection
if st.session_state.current_page == 'encrypt':
st.markdown("# π File Encryption")
st.markdown("---")
uploaded_file = st.file_uploader(
"Choose a file to encrypt",
type=None,
key="encrypt_file_uploader",
help="Select any file to encrypt. Maximum size: 10GB"
)
if uploaded_file is not None:
# Validate file upload
is_valid, validation_message = validate_file_upload(uploaded_file)
if not is_valid:
st.markdown(f'<div class="error-message">β {validation_message}</div>', unsafe_allow_html=True)
else:
try:
# Print the file name and validation info
st.info(f"π **File selected:** {uploaded_file.name}")
st.success(f"β
{validation_message}")
# Get password input
password_encr = st.text_input(
"π Enter password to encrypt the file",
type="password",
key="encrypt_password_input"
)
if password_encr:
strength = check_password_strength(password_encr)
if strength < 3:
st.markdown('<div class="error-message">β Password is too weak. Please use a stronger password with at least 8 characters, including uppercase, lowercase, numbers, and special characters.</div>', unsafe_allow_html=True)
else:
st.markdown('<div class="success-message">β
Password is strong enough.</div>', unsafe_allow_html=True)
if password_encr and st.button("π Encrypt File", type="primary", key="encrypt_button"):
if check_password_strength(password_encr) >= 3:
try:
with st.spinner("π Encrypting file..."):
encrypt = utils.file_encrypt(file=uploaded_file, password=password_encr)
st.download_button(
label="π₯ Download Encrypted File",
data=encrypt,
file_name="encrypted_file.zip",
mime="application/zip",
key="download_encrypted_file"
)
st.markdown('<div class="success-message">π File encrypted successfully!</div>', unsafe_allow_html=True)
except Exception as e:
st.markdown(f'<div class="error-message">β Encryption failed: {e}</div>', unsafe_allow_html=True)
else:
st.markdown('<div class="error-message">β Please use a stronger password before encrypting.</div>', unsafe_allow_html=True)
except Exception as e:
st.error(f"Error processing file: {e}")
st.markdown('<div class="error-message">β File processing failed.</div>', unsafe_allow_html=True)
else:
st.markdown("### π Instructions:")
st.markdown("""
1. **Upload a file** using the file uploader above (up to 10GB)
2. **Enter a strong password** (at least 8 characters with mixed case, numbers, and symbols)
3. **Click Encrypt** to secure your file
4. **Download** the encrypted ZIP file
π‘ **Note**: Large files (>1GB) may take longer to process.
""")
elif st.session_state.current_page == 'decrypt':
st.markdown("# π File Decryption")
st.markdown("---")
uploaded_file = st.file_uploader(
"Choose an encrypted file to decrypt",
type=['zip'],
key="decrypt_file_uploader",
help="Select an encrypted ZIP file to decrypt. Maximum size: 10GB"
)
if uploaded_file is not None:
# Validate file upload
is_valid, validation_message = validate_file_upload(uploaded_file)
if not is_valid:
st.markdown(f'<div class="error-message">β {validation_message}</div>', unsafe_allow_html=True)
else:
try:
# Print the file name and validation info
st.info(f"π **File selected:** {uploaded_file.name}")
st.success(f"β
{validation_message}")
# Get password input
password_decr = st.text_input(
"π Enter password to decrypt the file",
type="password",
key="decrypt_password_input"
)
if password_decr and st.button("π Decrypt File", type="primary", key="decrypt_button"):
try:
with st.spinner("π Decrypting file..."):
decrypt, filename, text = utils.file_decrypt(uploaded_file, password=password_decr)
st.markdown(f"π **Decrypted file name:** {filename}")
with st.expander("π View Decryption Details"):
st.text(text)
try:
decrypt = bytes(decrypt)
st.download_button(
label="π₯ Download Decrypted File",
data=decrypt,
file_name=filename,
key="download_decrypted_file"
)
st.markdown('<div class="success-message">π File decrypted successfully!</div>', unsafe_allow_html=True)
except Exception as e:
st.markdown(f'<div class="error-message">β Download failed: {e}</div>', unsafe_allow_html=True)
except Exception as e:
st.markdown(f'<div class="error-message">β Decryption failed: {e}</div>', unsafe_allow_html=True)
except Exception as e:
st.error(f"Error processing file: {e}")
st.markdown('<div class="error-message">β File processing failed.</div>', unsafe_allow_html=True)
else:
st.markdown("### π Instructions:")
st.markdown("""
1. **Upload an encrypted ZIP file** using the file uploader above (up to 10GB)
2. **Enter the password** used during encryption
3. **Click Decrypt** to recover your file
4. **Download** the decrypted file
π‘ **Note**: Large files (>1GB) may take longer to process.
""")