基于中国剩余定理的秘密共享方案

密码学

本文最后更新于 <span id="expire-date"></span> 天前,文中部分描述可能已经过时。

基于中国剩余定理的秘密共享方案

如题



题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from random import randint
import sympy
import sys
with open('C:\\Users\\吴文之\\Desktop\\验收数据\\plaintext4.txt') as file_object:
lines = file_object.readlines()
m = int(lines[0])
def fast(a,e,m): #快速计算模幂
a = a % m
r = 1
while e != 0:
if e&1:
r= (r*a)%m
e >>= 1 #右移一位
a = (a * a)%m
return r
def primitive(p, q): #调用快速取模幂求原根
while True:
g = randint(2,p-2)
if fast(g,2,p) != 1 and fast(g,q,p) != 1:
return g
def e_gcd(a,b):
if b == 0:
return a,1,0
g,x,y = e_gcd(b,a%b)
return g,y,x-a//b*y
while True: #生成一个大素数p
q = sympy.randprime(pow(10,149),pow(10,150)/2-1) #生成范围内的随机数
if sympy.isprime(q): #判断q是否是素数
p = 2 * q + 1 #生成强素数
if sympy.isprime(p) and len(str(p)) == 150:
break
print("q = " + str(q))
print("p = " + str(p))
g = primitive(p, q) #生成模𝑝的一个原根g
print("g = "+str(g))
a = randint(1,11) #随机选取整a
print("公钥对: (p,g,g^a) = "+"\n"+str(p)+"\n"+str(g)+"\n"+str(pow(g,a)%p))
y = fast(g,a,p)
#加密过程
k = randint(2, p-2)
c1 = fast(g, k, p)
c2 = (m*fast(y,k,p))%p
print("c1 = " + str(c1))
print("c2 = " + str(c2))
#解密过程
v = fast(c1,a,p)
v_ = e_gcd(v,p)[1]
m_ = c2*v_%p
if(m == m_):
print("相同")
else:
print("不同")

本文作者:Mosquito

本文链接: http://example.com/2022/01/26/%E5%9F%BA%E4%BA%8E%E4%B8%AD%E5%9B%BD%E5%89%A9%E4%BD%99%E5%AE%9A%E7%90%86%E7%9A%84%E7%A7%98%E5%AF%86%E5%85%B1%E4%BA%AB%E6%96%B9%E6%A1%88/

Mosquito 天使之吻手打

文章默认使用 CC BY-NC-SA 4.0 协议进行许可,使用时请注意遵守协议。