2024-04-29

Navigation

Skip Navigation Links

Hash algorithms

Asymmetric Algorithms

Symmetric Cipher Algorithms

Encoding Algorithms

Compression Algorithms

Pseudo Random Number Algorithms

Steganography

Library Wrappers

String Comparison

Others

Syntax highlighting by Prism
PBCrypto.com Mirror

Diffie-Hellman-Merkle Secure Key Exchange

Algorithm creator(s)

Whitfield Diffie, Martin Hellman, Ralph Merkle


PB author(s)

Wayne Diamond, Eddy Van Esch


Description

The worlds first secure public key exchange system.


Note

In the spring of 1976 Martin Hellman discovered a way for Alice and Bob to agree on a key without meeting, thereby disposing of an axiom that had lasted for centuries.


Source

https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/23945-simple-example-of-diffie-hellman-merkle-secure-key-exchange?t=23320


See also

n/a


Source Code

Download source code file keyex.bas (Right-click -> "Save as ...")

#COMPILE EXE
#INCLUDE "win32api.inc"
 
DECLARE SUB DEBUG (st$)
$ConClose = "$CON:CLOSE"
 
FUNCTION PBMAIN() AS LONG
LOCAL A AS LONG, B AS LONG
LOCAL A2 AS LONG, B2 AS LONG
LOCAL DecryptKeyA AS LONG, DecryptKeyB AS LONG
LOCAL mess AS STRING
LOCAL ModulusPrime AS LONG, Generator AS LONG
'====================================================
'Do not choose values too high!! Otherwise you will get unpredictable results
'====================================================
'ModulusPrime: must be a large prime (not too large in this program...)
ModulusPrime = 23
'Generator   : a random number, must be less than 'ModulusPrime'
Generator    = 16
'A = Alice, B = Bob
'STAGE 1 - Alice and Bob choose a secret number.
A = 10   'random secret number, smaller than ModulusPrime-1
B = 3    'random secret number, smaller than ModulusPrime-1
'STAGE 2 - Alice and Bob calculate their public key
A2 = (Generator ^ A) MOD ModulusPrime  'Alice's public key
B2 = (Generator ^ B) MOD ModulusPrime  'Bob's public key
debug STR$(Generator ^ A) & "<-- if this gets exponential, values are too high.."
debug STR$(Generator ^ B) & "<-- if this gets exponential, values are too high.."
debug "Alice's public key:" & STR$(A2)
debug "  Bob's public key:" & STR$(B2)
'STAGE 3 - Alice and Bob tell each other their public keys.
'Ordinarily, this would be a crucial moment, because Alice and Bob are
'exchanging information, and therefore this is an opportunity for Eve
'to eavesdrop and find out the details of the information. However, it
'turns out that Eve can listen in without it affecting the ultimate
'security of the system. Alice and Bob could use the same telephone line
'that they used to agree the values for Y and P, and Eve could intercept
'the two numbers that are being exchanged, 2 and 4. However, these numbers
'are not the key, which is why it doesn't matter if Eve knows them.
'[no code required - imagine Alice and Bob simply telling each other their STAGE2 numbers.
'STAGE 4 - Alice and Bob each calculate a common (secret) communication key using each others public keys
DecryptKeyA = (B2 ^ A) MOD ModulusPrime 'Alice's communication key, calculated using her own private key and Bob's public key
DecryptKeyB = (A2 ^ B) MOD ModulusPrime 'Bob's communication key, calculated using his own private key and Alice's public key
debug "Common key:" & STR$(DecryptKeyA)
debug "Common key:" & STR$(DecryptKeyB)
'Display the results...
mess =  "Alice has calculated this common key:" & STR$(DecryptKeyA) & $CRLF
mess =  mess & "  Bob has calculated this common key:" & STR$(DecryptKeyB) & $CRLF
IF DecryptKeyA <> DecryptKeyB THEN
    mess = mess & "DOOOHHH!! Keys didn't match! Error in program!"
ELSE
    mess = mess & "These must be the same."
END IF
MSGBOX mess
END FUNCTION

'For obvious reasons this demo uses small numbers. When implementing such a key exchange, all numbers should be made extremely high so that brute-force isn't viable
'==============================================================================
'This routine is only used for debugging. Has no real function in the program, other than to display results.
SUB DEBUG (st$)
    STATIC Hwnd&
    LOCAL szConsole AS ASCIIZ * 255
    IF Hwnd& = 0 THEN
       AllocConsole
       SetConsoleTitle "PBDLL Console"
       Hwnd& = GetStdHandle(%STD_OUTPUT_HANDLE)
       SetConsoleTextAttribute Hwnd&, %FOREGROUND_RED OR _
                                      %FOREGROUND_GREEN OR _
                                      %FOREGROUND_BLUE
    END IF
    IF Hwnd& > 0 THEN
       ' a magic word that close console
       IF st$=$ConClose THEN
          FreeConsole
          Hwnd& = 0
          EXIT SUB
       END IF
       szConsole = st$ & $CRLF
       WriteConsole Hwnd&,szConsole,LEN(szConsole),%NULL,%NULL
    END IF
END SUB

Mirror provided by Knuth Konrad