4

我正在阅读的书对算法的解释如下:

  • 2 人想到 2 个公开的“n 和 g”数字都知道。
  • 2 个人想到了他们保密的 2 个私人“x”和“y”数字。

交换发生如图所示

在此处输入图像描述

我将以下 python 代码放在一起,看看它是如何工作的,而......它没有。请帮助我了解我缺少什么:

 #!/usr/bin/python

 n=22 # publicly known 
 g=42 # publicly known

 x=13 # only Alice knows this 
 y=53 # only Bob knows this

 aliceSends = (g**x)%n 
 bobComputes = aliceSends**y 
 bobSends = (g**y)%n
 aliceComputes = bobSends**x


 print "Alice sends    ", aliceSends 
 print "Bob computes   ", bobComputes 
 print "Bob sends      ", bobSends 
 print "Alice computes ", aliceComputes

 print "In theory both should have ", (g**(x*y))%n

 ---

 Alice sends     14  
 Bob computes    5556302616191343498765890791686005349041729624255239232159744 
 Bob sends       14 
 Alice computes  793714773254144 

 In theory both should have  16
4

3 回答 3

8

您忘记了另外两个模数:

>>> 5556302616191343498765890791686005349041729624255239232159744 % 22
16L
>>> 793714773254144 % 22
16
于 2012-05-31T15:13:50.163 回答
7

罗曼是对的。但是,您最好看一下 pow() 三参数函数。更快,第三个参数是模数

于 2012-05-31T15:29:11.603 回答
0

两个人

#!/usr/bin/python
p=141301# publicly known 
g=5728435 # publicly known
x=76435 # only Alice knows this 
y=37846 # only Bob knows this
aliceSends = (g**x)%p 
aliceComputes = (bobSends**x)%p
bobSends = (g**y)%p
bobComputes = (aliceSends**y) %p
bobSends = (g**y)%p
bobComputes = (aliceSends**y) %p
print ("Alice sends    ", aliceSends )
print ("Bob computes   ", bobComputes )
print ("Bob sends      ", bobSends)
print ("Alice computes ", aliceComputes)

三人以上

#!/usr/bin/python
p=141301# publicly known 
g=5728435 # publicly known
x=76435 # only Alice knows this 
y=37846 # only Bob knows this
z=23# only carol knows this
aliceSends = (g**x)%p 
bobSends = (aliceSends**y)%p
carolComputes=(bobSends**z)%p
bobSends2=(g**y)%p
carolSends=(bobSends2**z)%p
aliceComputes=(carolSends**x)%p
carolSends2=(g**z)%p
aliceSends2=(carolSends2**x)%p
bobComputes=(aliceSends2**y)%p
print ("Alice computes ga and sends it to Bob.",aliceSends)
print ("Bob computes (ga)b = gab and sends it to Carol.",bobSends)
print ("Carol computes (gab)c = gabc and uses it as her secret.",carolComputes)
print ("Bob computes gb and sends it to Carol.",bobSends2)
print ("Carol computes (gb)c = gbc and sends it to Alice.",carolSends)
print ("Alice computes (gbc)a = gbca = gabc and uses it as her  secret.",aliceComputes)
print ("Carol computes gc and sends it to Alice.",carolSends2)
print ("Alice computes (gc)a = gca and sends it to Bob.",aliceSends2)
print ("Bob computes (gca)b = gcab = gabc and uses it as his 
secret.",bobComputes)
于 2017-11-16T12:45:53.100 回答