Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Diffie-Hellman Key Exchange

Name: Anonymous 2012-07-18 10:03

#!/bin/bash
set -e
set -o pipefail
DEFAULT_MODULUS=F5D3849D2092FD427B4EBD838EA4830397A55F80B644626320DBBE51E8F63ED88148D787C94E7E67E4F393F26C565E1992B0CFF8A47A953439462A4D0FFA5763EF60FF908F8EE6C4F6EF9F32B9BA50F01AD56FE7EBE90876A5CF61813A4AD4BA7EC0704303C9BF887D36ABBD6C2AA9545FC2263232927E731060F5C701C96DC34016636DF438CE30973715F121D767CFB98B5D09AE7B86FA36A051AD3C2941A295A68E2F583A56BC69913EC9D25ABEF4FDF1E31EDE827A02620DB058B9F041DA051C8C0F13B132C17CEB893FA7C4CD8D8FEEBD82C5F9120CB221B8E88C5FE4DC17CA020A535484C92C7D4BEE69C7703E1FA9A652D444C80065342C6EC0FAC23C24DE246E3DEE72CA8BC8BECCDADE2B36771EFCC350558268F5352AE53F2F71DB62249AD9AC4FABDD6DFB099C6CFF8C05BDEA894390F9860F011CCA046DFEB2F6EF81094E7980BE526742706D1F3DB920DB107409291BB4C11F9A7DCBFAF26D808E6F9FE636B26B939DE419129E86B1E632C60EC23B65C815723C5D861AF068FD0AC8B37F4C06ECBD5CB2EF069CA8DAAC5CBD67C6182A65FED656D0DFBBB8A430B1DBAC7BD6303BEC8DE078FE69F443A7BC8131A284D25DC2844F096240BFC61B62E91A87802987659B884C094C68741D29AA5CA19B9457E1F9DF61C7DBBB13A61A79E4670B086027F20DA2AF4F5B020725F8828726379F429178926A3625C04B
DEFAULT_GENERATOR=2
DHKX_DIR="$HOME/.dhkx"
mkdir -p "$DHKX_DIR"

fail(){ echo "error: $1" >/dev/stderr;exit 1;}
trhex(){ tr -cd '[:xdigit:]';}
pow(){ echo "$1,$2,$3" | python -c 'import sys;sys.stdout.write(hex(apply(pow,map(lambda s:int(s,16),raw_input().split(",")))).lstrip("0"))' | trhex;}
hfunc(){ printf '%s' "$1" | sha256sum | head -c 64;}
hex-to-b64(){
    s="$(echo "$1" | trhex)"
    { # account for odd number of hexdigits...
        if [ "$(printf '%s' "$s"|wc -c|dc -e '?2%p')" -eq 1 ];then printf 0;fi
        printf '%s' "$s"
    } | python -c 'from sys import *
while 1:
 s=stdin.read(2)
 if len(s)<2:break
 stdout.write(chr(int(s,16)))' | base64 -w0;}
b64-to-hex(){ echo "$1" | base64 -di | hexdump -ve '/1 "%02x"';}
gen_key(){ openssl rand -hex "$(printf '%s' "$1" | wc -c | dc -e '?1-2/p')";}

case "$1" in
   "")
        echo "Hello. I will now guide you through this manual Diffie-Hellman key exchange."
        g="$DEFAULT_GENERATOR"
        m="$DEFAULT_MODULUS"
        a="$(gen_key "$m")"
        g_a="$(pow "$g" "$a" "$m")"
        hash_g_a="$(hfunc "$g_a")"
        storage="${DHKX_DIR}/${hash_g_a}"
        printf '%s\n%s\n%s\n' "$m" "$g" "$a" > "$storage"
        echo "The parameters and key will be stored in '${storage}', but you don't have to write that down."
        echo "Give this command to the person you wish to communicate with:"
        echo "./dhkx.sh" "a" "$(hex-to-b64 "$m")" "$(hex-to-b64 "$g")" "$(hex-to-b64 "$g_a")"
        ;;
    "a")
        shift; m="$(b64-to-hex "$1")"; g="$(b64-to-hex "$2")"; g_a="$(b64-to-hex "$3")"
        b="$(gen_key "$m")"
        g_b="$(pow "$g" "$b" "$m")"
        g_ab="$(pow "$g_a" "$b" "$m")"
        echo "Congratulations, your shared key is: $(hfunc "$g_ab")"
        echo "Give this command to the person you wish to communicate with:"
        hash_g_a="$(hfunc "$g_a")"
        echo "./dhkx.sh" "b" "$(hex-to-b64 "$hash_g_a")" "$(hex-to-b64 "$g_b")"
        ;;
    "b")
        shift; hash_g_a="$(b64-to-hex "$1")"; g_b="$(b64-to-hex "$2")"
        storage="${DHKX_DIR}/${hash_g_a}"
        [ -f "$storage" ] || fail "can't find parameters in storage"
        { read m;read g;read a;} < "$storage"
        g_ab="$(pow "$g_b" "$a" "$m")"
        echo "Congratulations, your shared key is: $(hfunc "$g_ab")"
        ;;
esac

Name: Anonymous 2012-07-25 13:06

My my, >>1-san, this is cute

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List