-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.sh
126 lines (111 loc) · 2.48 KB
/
rsa.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
#!/bin/sh
# rsa encryption algorithm implementation as shell scripts by PKG
# generate random number
function generate {
local random=$(awk -v min=3 -v max=25 'BEGIN{srand(); print int(min+rand()*(max-min+1))}')
echo $random
}
# check for prime number
function isPrime () {
n="$1" # n = first argument
if [ "$n" -le 1 ]; then # n <= 1
echo 0 && return 0
elif [ "$n" -le 3 ]; then # n <= 3
echo 1 && return 1
# n mod 2 = 0 or n mod 3 = 0
elif [[ $(( n % 2 )) == 0 ]] || [[ $(( n % 3 )) == 0 ]]; then
echo 0 && return 0
fi
let i=5
while (( $i * $i <= $n )) # i * i <= n
do
# n mod i = 0 or n mod (i + 2) = 0
if (( $n % $i == 0 )) || (( $n % (( $i + 2)) == 0 )) ; then
echo 0 && return 0
fi
i=i+6
done
echo 1 && return 1
}
# generate numbers until a prime is generated
function randomPrime() {
randPri=`generate`
while [ `isPrime $randPri` -ne 1 ]
do
randPri=`generate`
sleep 1
done
echo $randPri
}
p=`randomPrime`; echo prime p = "$p"
q=`randomPrime`; echo prime q = "$q"
n=$(( $p * $q )); echo n is used as the modulus for both the public and private keys = "$n"
totient=$(( ($p-1) * ($q-1) )); echo totient = "$totient"
# find greatest common denominator of two numbers
function gcd () {
dividend=$1
divisor=$2
remainder=1
if [ $divisor -eq 0 ]; then
echo "GCD of $dividend and $divisor = $dividend"
exit 0
fi
until [ "$remainder" -eq 0 ]
do
let "remainder = $dividend % $divisor"
dividend=$divisor
divisor=$remainder
done
echo The GCD is: "$dividend"
}
gcd 5435 3141
# generate e
function genE() {
e=`randomPrime`
while [ $e -gt $totient ]
do
e=`genPrime`
done
echo "$e"
}
e=`genE`; echo The public key e = $e
# compute the multiplicative inverse
function multInv() {
e=$1
m=$2
e=$((e%$m))
for ((i = 1 ; i < $m ; i++ ));
do
if (( ($e * $i) % $m == 1 )); then
echo "$i" # final i = d
fi
done
}
d=`multInv $e $totient`; echo The private key d = $d
# encrypt
#echo Enter plain text:
#read plainText
#cipherText=$(($plainText**$e))
#cipherText=$(($cipherText%$n))
#echo cipher text = $cipherText
# decrypt
#echo Enter cipher text:
#read cipherTexti
#plainTexti=$(($cipherTexti**$d))
#plainTexti=$(($plainTexti%$n))
#echo plain text = $plainTexti
max=max=$((p * q)) # calc max for flattening
read msg
#i=$msg
#for x in $(seq 2 $e); do
# msg=$((msg*i)) # multiply
# msg=$((msg%max)) # flatten
#done
#echo "encr msg: $msg"
# decrypt message
#i=$msg2
#for x in $(seq 2 $d); do
# msg2=$((msg2*i)) # multiply
# msg2=$((msg2%max)) # flatten
#done
#echo "decr msg: $msg2