加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 关于Diffie-Hellman密钥交换

发布时间:2020-12-20 12:20:19 所属栏目:Python 来源:网络整理
导读:我正在阅读的这本书解释了算法如下: 2人认为2个公共“n和g”数字都知道. 2人认为他们保密的2个私人“x和”y“数字. 交换发生如图所示 我把以下python代码放在??一起,看看它是如何工作的……它没有.请帮我理解我缺少的东西: #!/usr/bin/python n=22 # publi
我正在阅读的这本书解释了算法如下:

> 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

解决方法

你忘了两个模数:

>>> 5556302616191343498765890791686005349041729624255239232159744 % 22
16L
>>> 793714773254144 % 22
16

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读