'This Powerbasic program is based on an example from a book called

'THE CODE BOOK, by Simon Singh. I got my copy at Amazon, and I

'recommend you get your copy now! It's the single most enjoyable

'book I've read in 2001/2002. It combines amazing stories of the

'history of cryptography (the codemakers) and cryptanalysis

'(the codebreakers) with amazing algorithms and is a must read

'for anyone who has even the slightest fascination with ciphers! :-)

'ported to PB from textual descriptions by Wayne Diamond, January 2002

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

Enjoy!

------------------

[This message has been edited by Wayne Diamond (edited January 21, 2002).]

'THE CODE BOOK, by Simon Singh. I got my copy at Amazon, and I

'recommend you get your copy now! It's the single most enjoyable

'book I've read in 2001/2002. It combines amazing stories of the

'history of cryptography (the codemakers) and cryptanalysis

'(the codebreakers) with amazing algorithms and is a must read

'for anyone who has even the slightest fascination with ciphers! :-)

'ported to PB from textual descriptions by Wayne Diamond, January 2002

Code:

#COMPILE EXE #INCLUDE "win32api.inc" FUNCTION OneWayFunc(LongIn AS LONG) AS LONG ON ERROR RESUME NEXT '// The general one-way function is Y^x (mod P). '// Alive and Bob have chosen values for Y and P, and hence '// have agreed on the one-way function 7^x (mod 11) FUNCTION = (((7 ^ LongIn) MOD 11) MOD 11) END FUNCTION FUNCTION PBMAIN() AS LONG ON ERROR RESUME NEXT LOCAL A AS LONG, B AS LONG LOCAL A2 AS LONG, B2 AS LONG LOCAL DecryptKeyA AS LONG, DecryptKeyB AS LONG LOCAL Function1Way AS LONG 'A = Alice, B = Bob 'STAGE 1 - Alice and Bob choose a secret number. A = 3 B = 6 'STAGE 2 - Alice and Bob put their secret numbers into ' the one-way function. A2 = OneWayFunc(A) ' = 2 B2 = OneWayFunc(B) ' = 4 'STAGE 3 - Alice and Bob tell each other their STAGE2 numbers. '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 takes Bob's STAGE2 number (B2) and works out the result using the one-way function DecryptKeyB = (((B2 ^ A) MOD 11) MOD 11) 'Bob takes Alice's STAGE2 number (A2) and works out the result using the one-way function DecryptKeyA = (((A2 ^ B) MOD 11) MOD 11) 'Display the results... STDOUT "Alice has ended up with: " & STR$(DecryptKeyB) STDOUT " Bob has ended up with: " & STR$(DecryptKeyA) 'Miraculously, both Alice and Bob have ended up with the same key - 9. 'They can now proceed with secure encryption, knowing that their key (9) is safe. WAITKEY$ END FUNCTION

Enjoy!

------------------

[This message has been edited by Wayne Diamond (edited January 21, 2002).]

## Comment