forked from terencejackson8000/encrypt_decrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_decrypt.sh
More file actions
executable file
·55 lines (51 loc) · 1.96 KB
/
encrypt_decrypt.sh
File metadata and controls
executable file
·55 lines (51 loc) · 1.96 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
#!/usr/bin/env bash
#Get the parameteres of the script and assign them
while getopts m:s:p: flag
do
case "${flag}" in
m) mechanism=${OPTARG};;
s) string=${OPTARG};;
p) password=${OPTARG};;
esac
done
#Check if all parameters are set, if not show an error message and exit the script
if [ -z "$mechanism" ] || [ -z "$string" ] || [ -z "$password" ]
then echo "You need to set all variables to run the script: -m enc for encryption or dec for decryption, -s The string to encrypt/decrypt, -p The password for the encryption/decryption"
exit 0
fi
#if the mechanism is encryption => encrypt the string, if the mechanism is decryption => decrypt the string
if [ $mechanism == 'enc' ]
then
#Check if input string is a directory
if [ -d "$string" ]
then
#Get the last folder of the provided path
dir=$(basename $string)
#Compress the folder
tar -czvf "${dir}.tar.gz" $string
#Encrypt the tar file
openssl enc -e -a -in "${dir}.tar.gz" -aes-256-cbc -salt -pass pass:$password -pbkdf2 -base64 -out "${dir}.enc"
#Delete the tar file
rm "${dir}.tar.gz"
echo "Folder encryption done"
#Check if input string is a file
elif [ -f "$string" ]
then
openssl enc -e -a -in $string -aes-256-cbc -salt -pass pass:$password -pbkdf2 -base64 -out "${string}.enc"
echo "File encryption done"
else
echo $string | openssl enc -base64 -e -aes-256-cbc -salt -pass pass:$password -pbkdf2
fi
elif [ $mechanism == 'dec' ]
then
if [ -f "$string" ]
then
new_str=$(echo $string | sed 's/.enc//')
openssl enc -d -a -in $string -aes-256-cbc -salt -pass pass:$password -pbkdf2 -base64 -out $new_str
echo "File decryption done"
else
echo $string | openssl enc -base64 -d -aes-256-cbc -salt -pass pass:$password -pbkdf2
fi
else
echo "Mechanism (-m) must be enc for encryption or dec for decryption"
fi