這本書我讀,如下解釋算法:在Diffie-Hellman密鑰交換
- 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
正如塞爾吉奧說了'POW()'三個參數的功能是遠遠快於冪然後mod。 – zaph
就速度達成一致,我剛纔注意到應該對未來看代碼的人的代碼進行更正。 – drixjoker