-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.sh
88 lines (74 loc) · 1.84 KB
/
encrypt.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
function compress_and_encode() {
echo -n "$1" | gzip | base64 | tr -d '='
}
function decode_and_decompress() {
echo -n "$1" | base64 --decode | gunzip
}
function encrypt_code() {
local code="$1"
local key="$2"
local compressed_code=$(compress_and_encode "$code")
local encoded_key=$(compress_and_encode "$key")
echo "${encoded_key}${compressed_code}"
}
function decrypt_code() {
local encoded_code="$1"
local key="$2"
local encoded_key=$(compress_and_encode "$key")
if [[ "$encoded_code" != "${encoded_key}"* ]]; then
echo "Invalid key" >&2
exit 1
fi
local compressed_code="${encoded_code:${#encoded_key}}"
decode_and_decompress "$compressed_code"
}
function menu() {
echo "Pilih operasi:"
echo "1. Encrypt"
echo "2. Decrypt"
echo "0. Keluar"
}
if [ $# -eq 0 ]; then
menu
echo -n "Pilihan Anda: "
read -r choice
echo -n "Masukkan kunci (key): "
read -r key
else
choice=$1
key=$2
fi
case "$choice" in
1)
if [ $# -eq 0 ]; then
echo -n "Masukkan kode yang akan dienkripsi: "
read -r original_code
else
original_code=$3
fi
encrypted_code=$(encrypt_code "$original_code" "$key")
echo "$encrypted_code"
;;
2)
if [ $# -eq 0 ]; then
echo -n "Masukkan kode yang akan didekripsi: "
read -r encoded_code
else
encoded_code=$3
fi
decrypted_code=$(decrypt_code "$encoded_code" "$key")
if [ $? -eq 0 ]; then
echo "$decrypted_code"
else
echo "Decryption failed"
fi
;;
0)
echo "Keluar dari program."
exit 0
;;
*)
echo "Pilihan tidak valid. Keluar dari program."
exit 1
;;
esac