-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenssl-helper
174 lines (149 loc) · 4.52 KB
/
openssl-helper
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
#!/usr/bin/bash
RED='\033[0;31m'
NC='\033[0m'
if [[ -x $(command -v openssl) ]]; then
:
else
echo -e "$RED Openssl not found $NC";
exit 0;
fi
openssl=$(head -n 0 | which openssl);
function choice1(){
read -rp "Encrypt/Decrypt File or Exit (Y/N): " choice1;
if [[ $choice1 == "Y" || $choice1 == "y" ]]; then
choice2;
elif [[ $choice1 == "N" || $choice1 == "n" || $choice1 == "Q" || $choice1 == "q" ]]; then
exit 0;
else
choice1;
fi
}
function choice2(){
read -rp "Encrypt/Decrypt a new File or does it already exit (A/B): " choice2;
if [[ $choice2 == "A" || $choice2 == "a" ]]; then
read -rp "Enter the name for the file: " file_name;
read -rp "Enter the contents for the file: " file_content;
if [[ -z "$file_name" ]]; then
echo -e "$RED File name cannot be blank $NC";
choice2;
elif [[ -f "$file_name" ]]; then
echo -e "$RED File name is already present $NC";
choice2;
elif [[ -z "$file_content" ]]; then
echo -e "$RED Nothing is written to the file $NC";
choice2;
else
touch "$file_name" && echo -e "$file_content" > "$file_name";
encdec_choice;
fi
elif [[ $choice2 == "B" || $choice2 == "b" ]]; then
read -rp "Enter the name for the file: " file_name;
if [ -f "$file_name" ]; then
encdec_choice;
else
echo "No such file found.";
choice2;
fi
elif [[ $choice2 == "Q" || $choice2 == "q" ]]; then
exit 0;
else
choice2;
fi
}
function encdec_choice(){
echo "$file_name";
cat -n "$file_name";
echo "";
read -rp "Encrypt or Decrypt the file (E/D): " encdec;
read -rp "Enter the name for the output file: " encdec_out;
if [[ $encdec == "E" || $encdec == "e" ]];then
encrypt_choice;
elif [[ $encdec == "D" || $encdec == "d" ]]; then
decrypt_choice;
elif [[ $encdec == "Q" || $encdec == "q" ]]; then
exit 0;
else
encdec_choice;
fi
}
function encrypt_choice(){
read -rp "Encrypt the FIle with Asymertric key encryption or Symmetric Key encyption (A/B): " enc_choice;
if [[ $enc_choice == "A" || $enc_choice == "a" ]]; then
asymmetric_encryption;
elif [[ $enc_choice == "B" || $enc_choice == "b" ]]; then
symmetric_encryption;
elif [[ $enc_choice == "Q" || $enc_choice == "q" ]]; then
exit 0;
else
encrypt_choice;
fi
}
function decrypt_choice(){
read -rp "Decrypt the File with Asymertric key encryption or Symmetric Key encyption (A/B): " dec_choice;
if [[ $dec_choice == "A" || $dec_choice == "a" ]]; then
asymmetric_decryption;
elif [[ $dec_choice == "B" || $dec_choice == "b" ]]; then
symmetric_decryption;
elif [[ $dec_choice == "Q" || $dec_choice == "q" ]]; then
exit 0;
else
decrypt_choice;
fi
}
function symmetric_encryption(){
$openssl enc -ciphers;
read -rp "Enter the algorithm: " symm_algo;
read -rsp "Enter password: " sym_pass;
echo "$sym_pass" | $openssl enc -$symm_algo -md sha512 -pbkdf2 -iter 10000 -salt -in "$file_name" -out "$encdec_out" -pass stdin 2>/dev/null || {
echo -e "\n$RED [!]Bad enc algo $NC";
sleep 1;
symmetric_encryption;
};
rerun_choice;
}
function symmetric_decryption(){
$openssl enc -ciphers;
read -rp "Enter the algorithm: " symm_algo;
read -rsp "Enter the password: " symm_pass;
echo "$symm_pass" | $openssl enc -$symm_algo -md sha512 -pbkdf2 -in "$file_name" -out "$encdec_out" -d -pass stdin 2>/dev/null || {
echo -e "\n$RED [!]Bad enc algo or bad password $NC";
sleep 1;
symmetric_decryption;
};
}
function asymmetric_encryption(){
$openssl enc -ciphers;
read -rp "Enter the algorithm: " asymm_algo;
read -rsp "Enter password for private key: " asymm_pass;
echo "";
$openssl genrsa -$asymm_algo -out my_priv.key -passout pass:"$asymm_pass" 4096 2>/dev/null || {
echo -e "\n$RED [!]Bad enc algo $NC\nChoose one from above";
sleep 1.5;
asymmetric_encryption;
};
echo "$asymm_pass" | $openssl rsa -in my_priv.key -pubout > my_pub.key -passin stdin;
$openssl rsautl --encrypt -inkey my_pub.key -pubin -in "$file_name" -out "$encdec_out";
rerun_choice;
}
function asymmetric_decryption(){
read -rsp "Enter password: " asymm_pass;
echo "$asymm_pass" | $openssl rsautl --decrypt -inkey my_priv.key -in "$file_name" > "$encdec_out" -passin stdin 2>/dev/null || {
echo -e "\n$RED [!]Bad password $NC";
sleep 1.5;
asymmetric_decryption;
};
echo "$encdec_out";
cat -n "$encdec_out";
}
function rerun_choice(){
echo "";
read -rp "Encrypt/Decrypt another file? (Y/N): " rerun;
if [[ $rerun == "N" || $rerun == "n" || $rerun == "Q" || $rerun == "q" ]]; then
exit 0;
elif [[ $rerun == "Y" || $rerun == "y" ]]; then
choice2;
else
rerun_choice;
fi
}
choice1;