Skip to content

Instantly share code, notes, and snippets.

@rangercyh
Created September 23, 2014 01:32
Show Gist options
  • Save rangercyh/eca307aae9bfab6af051 to your computer and use it in GitHub Desktop.
Save rangercyh/eca307aae9bfab6af051 to your computer and use it in GitHub Desktop.
diffe-hellman algorithm simple code
--[[
diffie-hellman算法简单代码
]]
local low = 10 --约定的随机数集合
local high = 20
local Alice = {}
function Alice:CreateSecret()
self.g = 2
self.p = 1019 --素数
self.a = math.random(low, high)
print("a = ", self.a)
return self.g, self.p, math.fmod(self.g^self.a, self.p)
end
function Alice:ShakeHand(B)
self.K = math.fmod(B^self.a, self.p)
print("B ^ a = ", B^self.a)
end
local Bob = {}
function Bob:ShakeHand(g, p, A)
self.b = math.random(low, high)
print("b = ", self.b)
self.K = math.fmod(A^self.b, p)
print("A ^ b = ", A^self.b)
return math.fmod(g^self.b, p)
end
local g, p, A = Alice:CreateSecret()
local B = Bob:ShakeHand(g, p, A)
Alice:ShakeHand(B)
print("g = ", g)
print("p = ", p)
print("A = ", A)
print("B = ", B)
print("Alice.K = ", Alice.K)
print("Bob.K = ", Bob.K)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment