[diffie.py]{..\src\ciphers\diffie.py}
"""
Prepare
1. sys.path 中增加 TheAlgorithms\src 子模块
"""
import sys
sys.path.append(‘E:\dev\AI\TheAlgorithms\src‘)
def find_primitive(n: int) -> Optional[int]:
for r in range(1, n):
li = []
for x in range(n - 1):
val = pow(r, x, n)
# pow(x, y[, z]), 函数是计算x的y次方,如果z在存在,则再对结果进行取模,
# 其结果等效于pow(x,y) %z
if val in li:
break
li.append(val)
else:
return r
return None
from ciphers.diffie import find_primitive
"""
"""
print(find_primitive(7))
print(find_primitive(563))
# print(find_primitive(838207))
3
2
- 1、依据一个素数,寻找一个另一可用素数
# q = int(input("Enter a prime number q: "))
q = 563
a = find_primitive(q)
if a is None:
print(f"Cannot find the primitive for the value: {a!r}")
else:
# alice_private = int(input("Enter private key of A: "))
alice_private = 89
alice_public = pow(a, alice_private, q)
# bob_private = int(input("Enter private key of B: "))
bob_private = 23
bob_public = pow(a, bob_private, q)
alice_secret = pow(bob_public, alice_private, q)
bob_secret = pow(alice_public, bob_private, q)
print("The key value generated by Alice is: ", alice_secret)
print("The key value generated by Bob is: ", bob_secret)
print(f"The key generated by Alice,Bob is equal ?: {alice_secret == bob_secret} ")
The key value generated by Alice is: 350
The key value generated by Bob is: 350
The key generated by Alice,Bob is equal ?: True
原文:https://www.cnblogs.com/it88-laobing/p/14912579.html