Advertisement
HPWebcamAble

Key exchange proof of concept

Aug 30th, 2015
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.26 KB | None | 0 0
  1. local channel = 1
  2. local prime = 625210769
  3. local primeRoot = 11
  4.  
  5. m = peripheral.wrap("top")
  6. m.open(1)
  7.  
  8. function baseToPowerMod(base,power,modulus)
  9.   --This function was taken from Anavrins' Diffie-Hellman proof of concept
  10.   --Full code here:http://pastebin.com/H3kZHZBA
  11.   local remainder = base
  12.   for i = 1, power-1 do
  13.     remainder = remainder * remainder
  14.     if remainder >= modulus then
  15.       remainder = remainder % modulus
  16.     end
  17.   end
  18.   return remainder
  19. end
  20.  
  21. print("Calculating secret number...")
  22. local secretNum = math.random(100,999)
  23. print("Done")
  24.  
  25. print("Calculating public key...")
  26. local public = baseToPowerMod(primeRoot,secretNum,prime)
  27. print("Done")
  28.  
  29. print()
  30. print("Secret Number:"..secretNum)
  31. print("Public Calc:"..public)
  32.  
  33. print()
  34. print("Waiting for other computer...")
  35. m.transmit(1,os.getComputerID(),{"sec","publicNum",public})
  36. while true do
  37.   local event,p1,p2,p3,p4,p5 = os.pullEvent("modem_message")
  38.   if type(p4) == "table" and p4[1] == "sec" and p4[2] == "publicNum" then
  39.     m.transmit(1,os.getComputerID(),{"sec","publicNum",public})
  40.     print("Recieved public key from Computer ID "..p3)
  41.     local sharedNum = baseToPowerMod(p4[3],secretNum,prime)
  42.     print("Shared number:"..sharedNum)
  43.     break
  44.   end
  45. end
  46.  
  47. m.closeAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement