-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFTP.sh
More file actions
237 lines (211 loc) · 7.28 KB
/
SFTP.sh
File metadata and controls
237 lines (211 loc) · 7.28 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
#!/bin/bash
# Function to choose operation: upload or download
choose_operation() {
operation=$(zenity --list --title="Secure File Sharing System" \
--text="Choose operation:" \
--column="Key" --column="Operation" --hide-column=1 \
1 "Upload file" 2 "Download file")
if [ -z "$operation" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
case "$operation" in
1) choose_encryption "upload" ;;
2) download_file ;;
*) zenity --error --text="Invalid option."; exit 1 ;;
esac
}
# Function to choose encryption/decryption type
choose_encryption() {
operation=$1
enc_option=$(zenity --list --title="Encryption Type" \
--text="Choose encryption type:" \
--column="Key" --column="Type" --hide-column=1 \
1 "Symmetric (AES-256-CBC with OpenSSL)" 2 "Asymmetric (GPG)")
if [ -z "$enc_option" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
case "$enc_option" in
1) if [ "$operation" == "upload" ]; then
encrypt_symmetric
else
decrypt_symmetric "$file_to_decrypt"
fi ;;
2) if [ "$operation" == "upload" ]; then
encrypt_asymmetric
else
decrypt_asymmetric "$file_to_decrypt"
fi ;;
*) zenity --error --text="Invalid encryption option."; exit 1 ;;
esac
}
# Function to encrypt file symmetrically
encrypt_symmetric() {
infile=$(zenity --file-selection --title="Select file to encrypt")
if [ -z "$infile" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
if [ ! -f "$infile" ]; then
zenity --error --text="Error: File does not exist."
exit 1
fi
outfile=$(zenity --file-selection --save --title="Save encrypted file as" \
--filename="$infile.enc")
if [ -z "$outfile" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
password=$(zenity --password --title="Enter password for encryption")
if [ -z "$password" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
tmpfile=$(mktemp)
echo "$password" > "$tmpfile"
openssl enc -aes-256-cbc -salt -in "$infile" -out "$outfile" -pass file:"$tmpfile"
if [ $? -eq 0 ]; then
zenity --info --text="Encryption successful: $outfile created."
rm "$tmpfile"
choose_transfer "$outfile"
else
zenity --error --text="Encryption failed."
rm "$tmpfile"
exit 1
fi
}
# Function to encrypt file asymmetrically
encrypt_asymmetric() {
infile=$(zenity --file-selection --title="Select file to encrypt")
if [ -z "$infile" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
if [ ! -f "$infile" ]; then
zenity --error --text="Error: File does not exist."
exit 1
fi
recipient=$(zenity --entry --title="GPG Recipient" \
--text="Enter GPG recipient (e.g., [email protected] or key ID):")
if [ -z "$recipient" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
outfile="$infile.gpg"
gpg --output "$outfile" --encrypt --recipient "$recipient" "$infile"
if [ $? -eq 0 ]; then
zenity --info --text="Encryption successful: $outfile created."
choose_transfer "$outfile"
else
zenity --error --text="Encryption failed. Ensure recipient's key is available."
exit 1
fi
}
# Function to choose transfer method
choose_transfer() {
filename=$1
transfer_option=$(zenity --list --title="Transfer Method" \
--text="Choose transfer method:" \
--column="Key" --column="Method" --hide-column=1 \
1 "SCP (Secure Copy)" 2 "RSYNC (Remote Sync)")
if [ -z "$transfer_option" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
destination=$(zenity --entry --title="Remote Destination" \
--text="Enter remote destination (user@host:/path):")
if [ -z "$destination" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
case "$transfer_option" in
1) scp "$filename" "$destination" ;;
2) rsync -av "$filename" "$destination" ;;
*) zenity --error --text="Invalid transfer option."; exit 1 ;;
esac
if [ $? -eq 0 ]; then
zenity --info --text="File transferred successfully to $destination."
else
zenity --error --text="File transfer failed. Check SSH configuration."
exit 1
fi
}
# Function to download file
download_file() {
remote_file=$(zenity --entry --title="Remote File" \
--text="Enter remote file (e.g., user@host:/path/to/file):")
if [ -z "$remote_file" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
local_file=$(zenity --file-selection --save --title="Save downloaded file as")
if [ -z "$local_file" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
scp "$remote_file" "$local_file"
if [ $? -eq 0 ]; then
zenity --info --text="File downloaded successfully as $local_file."
if zenity --question --text="Do you want to decrypt the downloaded file?"; then
file_to_decrypt="$local_file"
choose_encryption "download"
fi
else
zenity --error --text="Download failed. Check remote path or SSH access."
exit 1
fi
}
# Function to decrypt file symmetrically
decrypt_symmetric() {
encf=$1
if [ ! -f "$encf" ]; then
zenity --error --text="Error: File does not exist."
exit 1
fi
outf=$(zenity --file-selection --save --title="Save decrypted file as" \
--filename="${encf%.enc}")
if [ -z "$outf" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
password=$(zenity --password --title="Enter password for decryption")
if [ -z "$password" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
tmpfile=$(mktemp)
echo "$password" > "$tmpfile"
openssl enc -d -aes-256-cbc -in "$encf" -out "$outf" -pass file:"$tmpfile"
if [ $? -eq 0 ]; then
zenity --info --text="Decryption successful: $outf created."
rm "$tmpfile"
else
zenity --error --text="Decryption failed. Wrong password or corrupted file."
rm "$tmpfile"
exit 1
fi
}
# Function to decrypt file asymmetrically
decrypt_asymmetric() {
gpgf=$1
if [ ! -f "$gpgf" ]; then
zenity --error --text="Error: File does not exist."
exit 1
fi
outf=$(zenity --file-selection --save --title="Save decrypted file as" \
--filename="${gpgf%.gpg}")
if [ -z "$outf" ]; then
zenity --error --text="Operation cancelled."
exit 1
fi
gpg --output "$outf" --decrypt "$gpgf"
if [ $? -eq 0 ]; then
zenity --info --text="Decryption successful: $outf created."
else
zenity --error --text="Decryption failed. Check GPG key or passphrase."
exit 1
fi
}
# Start the script
choose_operation