-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypt_decrypt_txt.sh
executable file
·109 lines (75 loc) · 2.54 KB
/
crypt_decrypt_txt.sh
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
#!/bin/bash
# Authors: Alex Freitag, Sahra Flôhr, Willyam Castro
#
# Date: 23/03/2020
#
# Descryption: Crypt and decrypt files (only txt) with openssl in rsa key (asymmetric);
#
ext='.crypt'
[ "$1" == 1 ] && echo && echo || clear
echo -en "\033[1;34m\n[0] Gerar chaves \n[1] Crypt or Decrypt \n[2] Sair: \033[0m"
read ANSWER
clear
# Checking
case $ANSWER in
# Generate public and private keys
0) echo -en "\033[1;32m\n\tDefina uma chave de segurança a seguir e confirme-a: \n\n\033[0m"
openssl genrsa -des3 -out ~/private.pem 2048
[ $? == 0 ] && (echo -en "\033[1;32m\n\n\tEntre com a chave definida para gerar a chave pública:\n\n\033[0m" &&
openssl rsa -in ~/private.pem -outform PEM -pubout -out ~/public.pem)
if [ $? == 0 ]; then
clear
echo -en "\033[1;32m\n\t Chave privada gerada $HOME/private.pem e \n\t\tpública em $HOME/public.pem\033[0m\n\n"
$0 1
fi
;;
1) echo -en "\033[1;34m\n[0] Crypto \n[1] Decrypt : \033[0m "
read ANSWER
echo
case $ANSWER in
# Crypt;
0)
clear
echo -en "\033[1;34m Full path from archive TXT for crypt: \033[0m"
read path
if [ -e "$path" ]; then
cd $(dirname $path)
# get filename only
fileName=$(basename $path)
# change extension name to "$ext"
fileNameDst=$(echo $fileName | sed -e "s/.txt/${ext}/g")
if [ $? = 0 ]; then
openssl rsautl -encrypt -inkey ~/public.pem -pubin -in $fileName -out $fileNameDst
echo -en "\033[1;32m\n\t Arquivo encriptado com sucesso utilizando a chave pública. \n\n\033[0m"
else
echo -e "\033[1;31m Erro ao encriptar \"$path\" \033[0m"
exit 10
fi
else
echo -e "\033[1;31m\n\n Arquivo \"$path\" não localizado... \033[0m"
echo && echo
fi
;;
# Decrypt;
1)
echo
echo -en "\033[1;34m Informe o caminho completo do arquivo .crypt: \033[0m "
read path
# $OUTPUT vazio;
if [ -n $path ]; then
outputFile="$(echo $path | cut -d. -f1).txt" #"$fileName.txt"
echo -en "\033[1;32m \n\tInforme a chave de segurança da chave privada: \n \033[0m"
# Decrypt;
openssl rsautl -decrypt -inkey ~/private.pem -in $path -out $outputFile >> /dev/null
[ $? == 0 ] && echo -en "\033[1;32m \n\t Arquivo \"$path\" decriptado com sucesso! \n\t\tDisponível em \"$outputFile\" \n\n\033[0m"
else
echo -en "\033[1;31m \n\t\tCaminho vazio...\n\n \033[0m"
fi
;;
*) printf "\033[1;31m \t\tValor inválido...\n \033[0m"
esac
;;
2) exit;;
*) printf "\033[1;31m \n\t\tValor inválido... \033[0m"
$0 1
esac