· 7 years ago · Sep 08, 2018, 11:10 AM
1#!/bin/bash
2#Made by Patrick Kobberup-Jensen
3#ZBC H2 - Scriptprogramering - Bash
4
5op="" #This is a operator to help encoding / decoding, if you are decoding it will change to '-' if you are encoding it will change to '+'
6
7#############START MENU TO DECIDE IF WE ARE ENCODING OR DECODING ##########
8continue="0" #Set the variable continue to 0
9PS3='Please enter your choice: ' #Prompt selection
10options=("Encryption" "Decryption" "Decryption with gzip" "Quit") #Options for the menu
11select opt in "${options[@]}" #Select list from variable Options
12do
13 case $opt in
14 "Encryption") #If this option is selected variable continue is set to 1
15 echo "You chose encryption"
16 continue="1" #Set the variable continue to 1
17 op="+" #Set the variable op to +
18 break #Break out of the loop/selection to continue to next step
19 ;; #Extra break just in case first break dosen't work, this is to support mulitple bash versions
20 "Decryption") #If this option is selected variable continue is set to 2
21 echo "You chose decryption"
22 continue="2" #Set variable continue to 2
23 op="-" #Set variable op to -
24 break #Break out of the loop/selection to continue to next step
25 ;; #Extra break just in case first break dosen't work, this is to support multiple bash version
26 "Decryption with gzip") #If this option is selected variable continue is set to 3
27 echo "You chose Decryption with gzip"
28 continue="3" #Set variable continue to 3
29 op="-" #Set variable op to -
30 break #Break out of the loop/selection to continue to next step
31 ;; #Extra break just in case first break dosen't work, this is to support mulitple bash version
32 "Quit")
33 exit 1 #Will exit the script
34 ;; #Extra break just in case exit dosen't work correct
35 *) echo invalid option;; #Print this if something outside of the available options are selected
36 esac
37done
38###########################END OF MENU#####################################
39if [ "$continue" -eq "1" ] #If variable continue is equal to 1, then do encoding section
40###########################START TO GET MESSAGE TO ENCODE##################
41then
42 messageloop="0" #Set the variable messageloop to 0, this is to keep the message menu open until the messageloop is set to 1
43 reg='^[A-VX-Za-vx-z]+$' #My regular expression
44 echo "Please insert the message you want to have encrypted, no spaces and no special characters."
45 while [ $messageloop = 0 ] #While my messageloop is 0 continue to do this
46 do
47 read -p 'Insert your message: ' message #Ask the user to insert the message that needs to be encrypted
48 if [[ $message =~ $reg ]]; #If the message matches the allowed characters in regular expression do this
49 then
50 messageloop="1" #Set the variable messageloop to 1 to get out of loop
51 message=$(echo "$message" | tr [A-VX-Z] [a-vx-z] ) #Set the variable message to the result from message and translate all upper-case to lower-case, this is used for the encryption to understand the letters.
52 else #Give the follow error if the input has none allowed characters
53 echo "Spaces and speceial characters are not allowed, please type your key again or press 'ctrl + c' to cancel"
54 echo "The message you typed: $message"
55 fi
56 done
57######################END OF MESSAGE TO ENCODE############################
58######################START OF KEY TO USE FOR ENCODING####################
59 keyloop="0" #Set the variable keyloop to 0, this is to keep the key menu open until the keyloop is set to 1
60 echo "Please insert the key you want to use for the encryption, no spaces and no special characters."
61 while [ $keyloop = 0 ] #While my keyloop is 0 continue to do this
62 do
63 read -p 'Insert your key: ' secretkey #Ask the user to insert the key that will be used for encryption
64 if [ ${#secretkey} -lt 24 ] && [[ $secretkey =~ $reg ]]; #If the secretkey is less than 24 characters and matches the allowed characters in regular expression do this:
65 then
66 keyloop="1" #Set the variable keyloop to 1 to get out of loop
67 secretkey=$(echo "$secretkey" | tr [A-VX-Z] [a-vx-z] ) #Set the variable secretkey to the result from message and tranlsate all lower-case to upper-case
68 else #Give the following error if input is greater then 24, or if the input has none allowed characters
69 echo "The key can maximum be 24 characters long. Spaces and speceial characters are not allowed, please type your key again or press 'ctrl + c' to cancel"
70 echo "The key you typed: $secretkey"
71 fi
72 done
73######################END OF KEY TO USE FOR ENCODING#######################
74fi #End of if with continue equal to 1
75######################START OF MESSAGE TO DECODE###########################
76if [ "$continue" -eq "2" ] #If variable continue is equal to 2, then do decoding section
77then
78 messageloop="0" #Define the messageloop to 0 for my while loop
79 reg='^[A-VX-Za-vx-z]+$' #My regular expression
80 echo "Please insert the message you want to have decrypted, no spaces and no special characters."
81 while [ $messageloop = 0 ] #As long as the messageloop is 0, continue to do this
82 do
83 read -p 'Insert your message: ' message #Ask the user to insert the message that needs to be decoded
84 if [[ $message =~ $reg ]]; #If the allowed characters from the regex are used, then this will continue
85 then
86 messageloop="1" #Set the messageloop to 1 to break out of the loop
87 message=$(echo "$message" | tr [A-VX-Z] [a-vx-z] ) #Echo message into the same variable message, and after that translate all text that is upper-case to lower-case, this is used for the encryption to understand the letters.
88 else #Give the following error if the input has none allowed characters
89 echo "Spaces and speceial characters are not allowed, please type your message again or press 'ctrl + c' to cancel"
90 echo "The message you typed: $message"
91 fi
92 done
93######################END OF MESSAGE TO DECODE#############################
94######################START OF KEY TO DECODE###############################
95 keyloop="0" #Define the keyloop to 0 for my while loop
96 echo "Please insert the key you want to use for the decryption, no spaces and no special characters."
97 while [ $keyloop = 0 ] #As long as the keyloop is 0, continue to do this
98 do
99 read -p 'Insert your key: ' secretkey #Ask the user to insert the secret key that needs to be used to decode
100 if [ ${#secretkey} -lt 24 ] && [[ $secretkey =~ $reg ]]; #If secretkey is less then 24 and the allowed characters from the regex are used, then this will continue
101 then
102 keyloop="1" #Set the keyloop to 1 to break out of the loop
103 secretkey=$(echo "$secretkey" | tr [A-VX-Z] [a-vx-z] ) #Echo secretkey into the same variable secretkey, and after that translate all text that is upper-case to lower-case, this is used for the encryption to understand the letters.
104 else #Give the following error if input is greater then 24, or if the input has none allowed characters
105 echo "The secret key can maximum be 24 characters long. Spaces and speceial characters are not allowed, please type your secret key again or press 'ctrl + c' to cancel"
106 echo "The key you typed: $secretkey"
107 fi
108 done
109######################END OF KEY TO DECODE#################################
110fi #End of if with continue equal to 2
111######################START OF MESSAGE TO DECODE GZIP######################
112if [ "$continue" -eq "3" ] #If variable continue is equal to 3, then do decoding with gzip section
113then
114 messageloop="0" #Define the messageloop to 0 for while loop
115 reg='^[A-VX-Za-vx-z]+$' #My regular expression
116 echo "Please insert the file name of the gzip file, do not include extension."
117 while [ $messageloop = 0 ] #As long as the messageloop is 0, continue to do this
118 do
119 read -p 'Insert file name: ' message #Ask the user to insert the message that needs to be decoded
120 message=$(zless "$message" | tr [A-VX-Z] [a-vx-z] )
121 if [[ $message =~ $reg ]]; #If the allowed characters from the regex are used, then this will continue
122 then
123 messageloop="1" #Set the messageloop to 1 to break out of the loop
124 else #Give the follow error if some none allowed characters are used
125 echo "Spaces and speceial characters are not allowed, please type your message again or press 'ctrl + c' to cancel"
126 echo "The message extracted from gzip file says the message is "$message""
127 fi
128 done
129######################END OF MESSAGE TO DECODE GZIP########################
130######################START OF SECRETKEY TO DECODE GZIP####################
131 keyloop="0" #Define the keyloop to 0 for my while loop
132 echo "Please insert the key you want to use for the decryption, no spaces and no special characters."
133 while [ $keyloop = 0 ] #As long as the keyloop is 0, continue to do this
134 do
135 read -p 'Insert your key: ' secretkey
136 if [ ${#secretkey} -lt 24 ] && [[ $secretkey =~ $reg ]]; #If secretkey is less then 24 and the allowed characters from the regex are used, then this will continue
137 then
138 keyloop="1" #Set the keyloop to 1 to break out of the loop
139 secretkey=$(echo "$secretkey" | tr [A-VX-Z] [a-vx-z] ) #Echo secretkey into the same variable secretkey, and after that translate all text that is upper-case to lower-case, this is used for the encryption to understand the letters.
140 else #Give the following error if input is greater then 24, or if the input has none allowed characters
141 echo "The secret key can maximum be 24 characters long. Spaces and speceial characters are not allowed, please type your secret key again or press 'ctrl + c' tocancel"
142 echo "The key you typed: $secretkey"
143 fi
144 done
145fi
146######################END OF SECRETKEY TO DECODE GZIP######################
147
148######################START OF ENCRYPTION/DECRYPTION#######################
149length=${#secretkey} #Take the length of the secretkey and save it in length
150step=0 #Preset step to value 0, this step is used further down in the encryption/decryption to determine how far we are in the encryption process of the message
151move=1 #Declare move to 1, this is used to add 1 to the code value later on to make sure we are on the right position for the alphabet, this was added used due to a bug in the code I found which did not give the correct cipher code.
152while test -n "$message"
153do
154 char=${message:0:1} #define char as the first character in position 0 to position 1 in the message variable, a string starts with the first character in position 0
155 loop=25 #Set/reset the loop value to 25
156######################LOOP THROUGH MESSAGE LETTER BY LETTER################
157array=(a b c d e f g h i j k l m n o p q r s t u v x y z) #The allowed characters in the alphabet made in an array.
158 for letter in "${array[@]}" #Define for loop with name letter, where it loops through the defined array.
159 do
160 char=$(echo $char | sed s/$letter/$loop/) #Define char to the numeric varible that comes out from the sed, it will loop through all the letters which are defined z-a one by one, until it finds the required letter that matches the message variable on that letter, when it finds the letter it will define char to the loop value.
161 loop=$((loop-1)) #This will decrease the loop value with 1 every time that we have looped through a letter.
162 done
163 loop=25 #set/reset the loop value to 25
164######################LOOP THROUGH SECRETKEY LETTER BY LETTER##############
165 shift=${secretkey:$step:1} #Define variable shift from the secretkey's variable taken from what step that the encoding / decoding process is at, $step 0 = 1st character, $step 1 = 2nd character, etc.
166 for letter in "${array[@]}" #Define for loop with name letter, where it loops through the defined array.
167 do
168 shift=$(echo $shift | sed s/$letter/$loop/) #Define shift to the numeric variable that comes from the sed, it will loop through all the letters which are defined z-a one by one, until it finds the required letter that matches the secretkey variable on that step, when it finds the letter it will define sshift to the loop value
169 loop=$((loop-1)) #This will decrease the loop value with 1 every time that we have looped through a word
170 done
171 step=$(($(($step+1))%$length)) #Will increase the step by 1 after the loop process is done with the letter, and modify the length of the key so that it will loop around.
172######################CALCULATE THE TOTAL VALUE############################
173 code=$(($char$op$shift$op$move)) #This will take the numeric value that we found in char, then use the op variable which either plus or minus depending on if we are encoding or decoding,(this was defined in our first menu step where we chose what to do), it will when take the shift value and again use the op variable to either plus or minus and here on add the variable move which are defined at the top and give us the result saved in code variable
174 if [[ $code -lt 0 ]] #If the value of code is less then 0 do this:
175 then
176 code=$((code+26)) #Takes the code variable value and adds 26 to it.
177 fi
178 if [[ $code -gt 25 ]] #If the value of code is greater then 25 do this, this is because the whole alphabet is only 25 characters long:
179 then
180 code=$((code-26)) #Takes the code variable value and minus 26 to it.
181 fi
182 loop=25 #set/reset the loop value to 25
183#####################LOOP THROGUH THE CODE#################################
184 for letter in "${array[@]}" #Define for loop with name letter, where it loops through the defined array.
185 do
186 code=$(echo $code | sed s/$loop/$letter/) #This is where it finds the letter that are used in the fullmessage, it goes through the loop numbers and all the letter until it finds the loop with the corresponding code value and gives us the letter and saves it in the code which are saved in fullmessage
187 loop=$((loop-1)) #This will decrease the loop value with 1 every time that we have looped over a letter
188 done
189 fullmessage=$fullmessage$code #This is building the message as it comes in and saves the message in the fullmessage variable, it will set it as a preexisting message, and as the message gets decoded, the code variable will take over and the fullmessage will have the message.
190 message=${message:1} #Removes the first character from the message
191done
192if [ "$continue" -eq "1" ]
193then
194 read -p "Please define what you want to call your gzip file, do not include .gz: " filename #Ask the user to put in a filename wanted for the gzip file
195fi
196echo "The message is: "
197echo $(echo -n "$fullmessage"; printf "%`echo $(((5 - (${#fullmessage} % 5)) % 5))`s" | tr ' ' '%') | sed 's/.\{5\}/&%/g' | tee >(gzip --stdout > "$filename".gz ) #This will echo out the encoded message in 5 character groups, sepeareted with a '%', it goes through the message and counts the fullmessage variable to find out how long it is, and as I understand it it takes all spaces and translates that to '%', I am not completly sure how it works. at the end it will make a gzip file and save it as the filename defined and add .gz to the end.
198######################END OF ENCRYPTION/DECRYPTION#########################