Created
June 7, 2024 11:30
-
-
Save kepocnhh/3cd3163e591968add80deb9304b0dbd1 to your computer and use it in GitHub Desktop.
Diffie–Hellman key exchange cryptographic explanation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
# d=256 | |
d=512 | |
g="$(((RANDOM % d) + 1))" | |
p="$(((RANDOM % d) + 1))" | |
echo '' | |
echo 'public:' | |
echo "g = $g" | |
echo "p = $p" | |
# d=8 | |
a="$(((RANDOM % d) + 1))" | |
b="$(((RANDOM % d) + 1))" | |
for ((;;)); do | |
if test "$b" != "$a"; then break; fi | |
b="$(((RANDOM % d) + 1))" | |
done | |
echo '' | |
echo 'private:' | |
echo "a = $a" | |
echo "b = $b" | |
export BC_LINE_LENGTH=2048 | |
gA="$(bc -e "$g^$a")" | |
gB="$(bc -e "$g^$b")" | |
echo '' | |
echo -e "g^a($g^$a):\n$gA" | |
echo -e "g^b($g^$b):\n$gB" | |
A="$(bc -e "$gA%$p")" | |
B="$(bc -e "$gB%$p")" | |
echo '' | |
echo 'public:' | |
echo "A = $A" | |
echo "B = $B" | |
Ba="$(bc -e "$B^$a")" | |
Ab="$(bc -e "$A^$b")" | |
echo '' | |
echo -e "B^a:\n$Ba" | |
echo -e "A^b:\n$Ab" | |
aK="$(bc -e "$Ba%$p")" | |
bK="$(bc -e "$Ab%$p")" | |
echo '' | |
echo 'private:' | |
echo "aK = $aK" | |
echo "bK = $bK" | |
echo '' | |
if test "$aK" == "$bK"; then | |
echo 'ok'; exit 0; fi | |
echo 'Not implemented!'; exit 1 # todo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment