-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodepw.sh
136 lines (120 loc) · 3.6 KB
/
encodepw.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
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
#!/bin/bash
# URL encode function
urlencode() {
# Usage: urlencode "string"
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos = 0 ; pos < strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
# URL decode function
urldecode() {
# Usage: urldecode "string"
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# Function to encode the passphrase from file
encode_passphrase() {
echo "Enter the file path with the passphrase you want to encode: "
read -r file_path
if [ -f "$file_path" ]; then
phrase=$(cat "$file_path")
newph="$phrase"
for i in $(seq 1 10)
do
newph=$(echo "$newph" | base64)
for j in $(seq 1 10)
do
newph=$(urlencode "$newph")
done
done
echo "Encoded passphrase: $newph"
else
echo "File not found. Please enter a valid file path."
fi
}
# Function to decode the passphrase
decode_passphrase() {
echo "Enter the encoded passphrase file path: "
read -r file_path
if [ -f "$file_path" ]; then
encoded_passphrase=$(cat "$file_path" )
decoded_passphrase="$encoded_passphrase"
for j in $(seq 1 10)
do
decoded_passphrase=$(urldecode "$decoded_passphrase")
done
decoded_passphrase=$(echo "$decoded_passphrase" | base64 --decode)
echo "Decoded passphrase: $decoded_passphrase"
else
echo "File not found. Please enter a valid file path."
fi
}
# Function to encode the contents of a file
encode_file() {
echo "Enter the file path you want to encode: "
read -r file_path
if [ -f "$file_path" ]; then
# Read content from the file
content=$(cat "$file_path")
# Ask for confirmation
echo "Are you sure you want to encode the file? (yes/no)"
read -r confirmation
if [ "$confirmation" = "yes" ]; then
# Encode the content
encoded_content=$(echo "$content" | base64)
# Update the file with the encoded content
echo "$encoded_content" > "$file_path"
echo "File encoded successfully."
else
echo "Operation canceled."
fi
else
echo "File not found. Please enter a valid file path."
fi
}
# Function to decode the contents of a file
decode_file() {
echo "Enter the file path you want to decode: "
read -r file_path
if [ -f "$file_path" ]; then
# Read content from the file
encoded_content=$(cat "$file_path")
# Decode the content
decoded_content=$(echo "$encoded_content" | base64 --decode)
# Update the file with the decoded content
echo "$decoded_content" > "$file_path"
echo "File decoded successfully."
else
echo "File not found. Please enter a valid file path."
fi
}
# Ask user for operation
echo "Choose an operation: encode or decode"
read -r operation
if [ "$operation" = "encode" ]; then
encode_file
elif [ "$operation" = "decode" ]; then
decode_file
else
echo "Invalid operation. Please choose 'encode' or 'decode'."
fi
# Ask user for operation
echo "Choose an operation: encode or decode"
read -r operation
if [ "$operation" = "encode" ]; then
encode_passphrase
elif [ "$operation" = "decode" ]; then
decode_passphrase
else
echo "Invalid operation. Please choose 'encode' or 'decode'."
fi