-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesarNrot.sh
60 lines (45 loc) · 1.35 KB
/
caesarNrot.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
#!/usr/bin/bash
read -p "Please Enter the Plaintext: " plaintext
read -p "Please enter number of shift: " shiftN
#plaintext="$1"
#shiftN="$2"
# for executing the script in a single command by passing plaintext and shift
# as arguments, uncomment line 6 & 7 and comment line 3 & 4
# bash caesarNrot.sh "your plaintext/cipherText" "shift number"
# example: bash caesarNrot.sh "abcdef hello!" 13
abc="abcdefghijklmnopqrstuvwxyz"
plaintext=$(echo $plaintext | tr '[:upper:]' '[:lower:]')
function encrypt(){
for ((i=0;i<${#plaintext};i++)); do
character=${plaintext:i:1}
findIndex=${abc%%$character*}
value=$((${#findIndex}+shiftN))
if [[ ${#findIndex} -eq 26 ]]; then
cipherText+=$character
elif [[ $value -gt 25 ]]; then
let value-=1
cipherText+=${abc:$((${value}%25)):1}
else
cipherText+=${abc:value:1}
fi
done
}
function decrypt(){
for ((i=0;i<${#plaintext};i++)); do
character=${plaintext:i:1}
findIndex=${abc%%$character*}
value=$((${#findIndex}-shiftN))
if [[ ${#findIndex} -eq 26 ]]; then
plainText+=$character
elif [[ $value -lt 0 ]]; then
let value+=1
plainText+=${abc:$((25+${value})):1}
else
plainText+=${abc:value:1}
fi
done
}
encrypt; decrypt
echo "The plaintext (shift-left by "$shiftN") is: " $plainText
echo "The ciphertext (shift-right by "$shiftN") is: " $cipherText
#echo $cipherText