-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdes.sh
128 lines (128 loc) · 4.07 KB
/
sdes.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
#!/bin/sh
p10=(2 4 1 6 3 9 0 8 7 5)
p8=(5 2 6 3 7 4 9 8)
ip1=(3 0 2 4 6 1 7 5)
p4=(1 3 2 0)
ip=(1 5 2 0 3 7 4 6)
e=(3 0 1 2 1 2 3 0)
function permutation {
perm=($1); shift
bin=($1)
for j in ${perm[@]}; do
echo ${bin[$j]}
done
}
function xor {
bin1=($1); shift
bin2=($1)
binLength=($((${#bin1[@]} - 1)))
for ((j=0; j<=$binLength; j++)); do
echo $(( ${bin1[j]} ^ ${bin2[j]} ))
done
}
function sBox {
txtSBox=($1); shift
sBoxS=($1)
case "$sBoxS" in
sBoxS0)
declare -A sBoxSVersion=([0,0]=1 [0,1]=0 [0,2]=3 [0,3]=2
[1,0]=3 [1,1]=2 [1,2]=1 [1,3]=0
[2,0]=0 [2,1]=2 [2,2]=1 [2,3]=3
[3,0]=3 [3,1]=1 [3,2]=3 [3,3]=1);;
sBoxS1)
declare -A sBoxSVersion=([0,0]=1 [0,1]=1 [0,2]=2 [0,3]=3
[1,0]=2 [1,1]=0 [1,2]=1 [1,3]=3
[2,0]=3 [2,1]=0 [2,2]=1 [2,3]=0
[3,0]=2 [3,1]=1 [3,2]=0 [3,3]=3);;
esac
txtSBoxS01=($((${txtSBox[0]} * 2 ** 1 + ${txtSBox[3]} * 2 ** 0)))
txtSBoxS02=($((${txtSBox[1]} * 2 ** 1 + ${txtSBox[2]} * 2 ** 0)))
if [ ${sBoxSVersion[$txtSBoxS01,$txtSBoxS02]} = 0 ]; then
echo 0 0
fi
if [ ${sBoxSVersion[$txtSBoxS01,$txtSBoxS02]} = 1 ]; then
echo 0 1
fi
if [ ${sBoxSVersion[$txtSBoxS01,$txtSBoxS02]} = 2 ]; then
echo 1 0
fi
if [ ${sBoxSVersion[$txtSBoxS01,$txtSBoxS02]} = 3 ]; then
echo 1 1
fi
}
function keyGeneration1 {
keyIn=($1)
keyP10=($(permutation "${p10[*]}" "${keyIn[*]}")) #p10
keyL=(${keyP10[@]:0:5}); keyR=(${keyP10[@]:5:10}) #Division
ls1L=(${keyL[@]:1:4}); ls1R+=(${keyR[@]:1:4}) #Offset <
ls1L[5]=${keyL[0]}; ls1R[5]=${keyR[0]}
ls1=(${ls1L[@]}); ls1+=(${ls1R[@]})
echo $(permutation "${p8[*]}" "${ls1[*]}") #p8
}
function keyGeneration2 {
keyIn=($1)
keyP10=($(permutation "${p10[*]}" "${keyIn[*]}")) #p10
keyL=(${keyP10[@]:0:5}); keyR=(${keyP10[@]:5:10}) #Division
ls1L=(${keyL[@]:1:4}); ls1R+=(${keyR[@]:1:4}) #Offset <
ls1L[5]=${keyL[0]}; ls1R[5]=${keyR[0]}
ls1=(${ls1L[@]}); ls1+=(${ls1R[@]})
ls2L=(${ls1L[@]:2:4}); ls2R=(${ls1R[@]:2:4}) #Offset <<
ls2L+=(${ls1L[@]:0:2}); ls2R+=(${ls1R[@]:0:2})
ls2=(${ls2L[@]}); ls2+=(${ls2R[@]})
echo $(permutation "${p8[*]}" "${ls2[*]}") #p8
}
function round {
txtRoundInput=($1); shift
keyRound=($1)
txtL=(${txtRoundInput[@]:0:4}); txtR=(${txtRoundInput[@]:4:8}) #Division
txtE=($(permutation "${e[*]}" "${txtR[*]}")) #E/P
txtXor=($(xor "${txtE[*]}" "${keyRound[*]}")) #xor
txtXorL=(${txtXor[@]:0:4}); txtXorR=(${txtXor[@]:4:8}) #Division
txtSBox=($(sBox "${txtXorL[*]}" "sBoxS0")) #SBoxS0
txtSBox+=($(sBox "${txtXorR[*]}" "sBoxS1")) #SBoxS1
txtP4=($(permutation "${p4[*]}" "${txtSBox[*]}")) #P4
txtRoundL=($(xor "${txtP4[*]}" "${txtL[*]}")) #Xor
echo ${txtRoundL[@]} ${txtR[@]}
}
function encryption {
txtInput=($1); shift
keyInput=($1)
key1=($(keyGeneration1 "${keyInput[*]}")) #KeyGen 1
key2=($(keyGeneration2 "${keyInput[*]}")) #KeyGen 2
txtIp=($(permutation "${ip[*]}" "${txtInput[*]}")) #Ip
txtRound=($(round "${txtIp[*]}" "${key1[*]}")) #fK1
txtRoundRotate=(${txtRound[@]:4:8}); txtRoundRotate+=(${txtRound[@]:0:4}) #P
txtRound=($(round "${txtRoundRotate[*]}" "${key2[*]}")) #fK2
echo $(permutation "${ip1[*]}" "${txtRound[*]}") #Ip-1
}
function decryption {
txtInput=($1); shift
keyInput=($1)
key1=($(keyGeneration1 "${keyInput[*]}")) #KeyGen 1
key2=($(keyGeneration2 "${keyInput[*]}")) #KeyGen 2
txtIp=($(permutation "${ip[*]}" "${txtInput[*]}")) #Ip
txtRound=($(round "${txtIp[*]}" "${key2[*]}")) #fK1
txtRoundRotate=(${txtRound[@]:4:8}); txtRoundRotate+=(${txtRound[@]:0:4}) #P
txtRound=($(round "${txtRoundRotate[*]}" "${key1[*]}")) #fK2
echo $(permutation "${ip1[*]}" "${txtRound[*]}") #Ip-1
}
if [[ $# < 3 ]] || [[ "$1" != "-e" && "$1" != "-d" ]]; then
echo "Usage: -e|-d \"txt[8 bits]\" \"key[10 bits]\""
exit 0
fi
func=("$1"); shift
txt=("$1"); shift
key=("$1")
case "${func}" in
"-e")
txtCipher=$(encryption "${txt[*]}" "${key[*]}")
echo Cipher txt: ${txtCipher[@]};;
"-d")
txtDecrypted=$(decryption "${txt[*]}" "${key[*]}")
echo Decrypted txt: ${txtDecrypted[@]};;
esac
# sdes.sh
#
#
# Created by danil-koltsov
#