代码我直接粘贴上来了,放在最后。
Decoding 所用公示:
L( u ) = ( u - 1 ) / n
m = D( c ) ≡ ( L( c^( λ( n )) ( mod n^2 ))) / ( L( g^( λ( n )) ( mod n^2 ))) (mod n)
问题在于:
在 m 中,被除数和除数都是属于 Znn ,也就是属于 Integers(n^2)
后面 modulo n 范围自然就成了 Zn
这里就会出现 inverse dose not existe
如何顺利转换 Znn 到 Zn 呢?
def getRandom ():
tmp = 0;
while (tmp == 0):
r = ZZ.random_element(2^(512 - 1), 2^512)
# random number 512 bits from 2^(512 - 1) to 2^215 - 1
if is_prime(r):
tmp = 1
return r
def getKeyList ():
LKey = []
# initialize prime number p and q
p = getRandom()
q = getRandom()
while (p == q):
p = getRandom()
LKey.append(p) #Lkey[0]
LKey.append(q) #Lkey[1]
lambdan = lcm(p - 1, q - 1)
LKey.append(lambdan) #Lkey[2]
n = p * q
LKey.append(n) #Lkey[3]
if (gcd(n, lambdan) != 1):
return false
g = n + 1
LKey.append(g) #Lkey[4]
# how it works with return listKey1, listKey2 ?
return LKey
def getPubKey (LKey):
KPub = []
KPub.extend(LKey[3:5])
return KPub
def getPriKey (LKey):
KPri = []
KPri.extend(LKey[0:3])
return KPri
def Encoding (m, KPub):
n = KPub[0]
Zn = Integers(n)
Znn = Integers(n^2)
g = Znn(KPub[1])
r = Znn(abs(ZZ.random_element()))
c = Znn(g^m * r^n)
return c
def L(u, KPub):
n = KPub[0]
Zn = Integers(n)
Znn = Integers(n^2)
return Zn((u - 1)/n)
def Decoding (c, KPub, KPri):
n = KPub[0]
Zn = Integers(n)
Znn = Integers(n^2)
g = Znn(KPub[1])
lambdan = Znn(KPri[2])
Pup = L(pow(c, lambdan, n^2), KPub)
Pdown = L(pow(g, lambdan, n^2), KPub)
m = Zn(Pup / Pdown) # bug here
#up = pow(c, lambdan, n^2) - 1
#down = pow(g, lambdan, n^2) - 1
#m = Zn(up / down) # bug here
return m
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.