-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecretSharing.py
280 lines (261 loc) · 9.33 KB
/
SecretSharing.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import sys
from sys import argv
# Импортируем наш интерфейс
from GEN import *
from SEC import *
from mainform import*
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
from sympy import Matrix
import random
from functools import partial
# проверка на простое число
def is_prime(n):
if n< 2:
return False
if n == 2:
return True
for i in range(2, int(n/2)+1):
if n % i == 0:
return False
return True
rejim=''
class Generat(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui=GEN()
self.ui.setupUi(self)
if rejim=='BL':
self.ui.pushButton.clicked.connect(self.doliBL)
elif rejim=='SHA':
self.ui.pushButton.clicked.connect(self.doliSHA)
else:
self.ui.textEdit.setText('Введите K,N через пробел')
self.ui.pushButton.clicked.connect(self.doliKTO)
def doliBL(self):
try:
sistem=list(map(int,self.ui.textEdit.toPlainText().split()))
secret=int(self.ui.textEdit_2.toPlainText())
except :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
x=[]
x.append(secret)
prv=is_prime(sistem[2])
if sistem[0]>sistem[1] or prv!=True or sistem[2]<=secret:
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
for i in range(1,sistem[0]):
x.append(random.randint(0,1000))
mas=[]
for i in range(sistem[1]):
listA=[]
for j in range(sistem[0]):
listA.append(random.randint(0,1000))
listA.append(sum(np.array(listA)*np.array(x))%sistem[2])
mas.append(listA)
self.ui.textEdit_3.setText("Ваши данные:")
for i in range(len(mas)):
vivod=''
for j in range(len(mas[i])):
if j!=len(mas[i])-1:
vivod+=(f"A{i,j} = {mas[i][j]} ")
else:
vivod+=(f"B{i} = {mas[i][j]} ")
self.ui.textEdit_3.append(vivod +'\n')
def doliSHA(self):
try:
sistem=list(map(int,self.ui.textEdit.toPlainText().split()))
secret=int(self.ui.textEdit_2.toPlainText())
except :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
prv=is_prime(sistem[2])
if sistem[0]>sistem[1] or prv!=True or sistem[2]<=secret:
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
a =[]
k=sistem[0]
n=sistem[1]
for i in range(k-1):
a.append(random.randint(2,1000))
x=[i for i in range(1,n+1)]
ki = []
for i in range(n):
sm = secret
for j in range(k-1):
sm += a[j]*(x[i]**(k-j-1))
ki.append(sm%sistem[2])
vivod=''
self.ui.textEdit_3.setText("Ваши данные:")
for i in range(len(ki)):
vivod+=f'A{i}={x[i],ki[i]}\n'
self.ui.textEdit_3.append(vivod)
def doliKTO(self):
try:
sistem=list(map(int,self.ui.textEdit.toPlainText().split()))
secret=int(self.ui.textEdit_2.toPlainText())
except :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
x=[]
mas=[]
k=sistem[0]
n=sistem[1]
primes = [i for i in range(int(secret**(1/k)),int(secret**(1/(k-1)))) if is_prime(i)]
#print(primes)
#print(primes,int(secret**-k),int(secret**-(k-1)))
if sistem[0]>sistem[1] or primes==[] :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
for i in range(0,n):
p=random.choice(primes)
x.append(p)
mas.append(secret%p)
self.ui.textEdit_3.setText("Ваши данные:")
vivod=''
for i in range(len(mas)):
vivod+=(f"A{i} = {mas[i]} P{i} = {x[i]} \n")
self.ui.textEdit_3.append(vivod +'\n')
#Вкладка о программе
class Secret(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui=SEC()
self.ui.setupUi(self)
if rejim=='BL':
self.ui.pushButton.clicked.connect(self.doliBL)
elif rejim=='SHA':
self.ui.pushButton.clicked.connect(self.doliSHA)
else:
self.ui.textEdit.setText('Введите K,N через пробел')
self.ui.pushButton.clicked.connect(self.doliKTO)
def doliKTO(self):
try:
sistem=list(map(int,self.ui.textEdit.toPlainText().split()))
dannie=list(map(int,self.ui.textEdit_3.toPlainText().split()))
except :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
k=sistem[0]
n=sistem[1]
if k>n or len(dannie)/2!=k :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
Mob=1
masM=[]
masY=[]
masP=[]
for i in range(1,k+1):
Mob*=dannie[i]
masP.append(dannie.pop(i))
for i in range(k):
m=Mob//masP[i]
masM.append(m)
masY.append(pow(m,-1,masP[i]))
N=0
for i in range(len(dannie)):
N+=dannie[i]*masM[i]*masY[i]
self.ui.textEdit_2.setText(f"Секрет = {N%Mob}")
def doliBL(self):
try:
sistem=list(map(int,self.ui.textEdit.toPlainText().split()))
dannie=list(map(int,self.ui.textEdit_3.toPlainText().split()))
except :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
k=sistem[0]
n=sistem[1]
p=sistem[2]
if k>n or len(dannie)/k!=k+1 :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
masA=[]
masB=[]
for i in range(k):
masA.append(list(dannie[j+i*(k+1)] for j in range(k)))
masB.append(dannie[k+i*(k+1)])
A=Matrix(masA)
D=np.array((A.inv_mod(p))).T
masB = np.array(masB).T
resh=masB.dot(D)
resh=resh%p
self.ui.textEdit_2.setText(f"Секрет = {resh[0]}")
def doliSHA(self):
try:
sistem=list(map(int,self.ui.textEdit.toPlainText().split()))
dannie=list(map(int,self.ui.textEdit_3.toPlainText().split()))
except :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
k=sistem[0]
n=sistem[1]
p=sistem[2]
if k>n or len(dannie)/2!=k :
em = QtWidgets.QErrorMessage(self)
em.showMessage("Не те данные")
return
Y=[]
X=[]
for i in range(0,k*2,2):
X.append(dannie[i])
Y.append(dannie[i+1])
sm = 0
for j in range(len(Y)):
p1=1
p2=1
for i in range(len(X)):
if j != i:
p1=p1*(0 - X[i])
p2=p2*(X[j] - X[i])
else:
p1=p1*1
p2=p2*1
sm = sm+Y[j]*p1/p2
sm=int(sm%p)
self.ui.textEdit_2.setText(f"Секрет = {sm}")
#основный класс программы
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.action.triggered.connect(partial(self.doli, "SHA"))
self.ui.action_2.triggered.connect(partial(self.secr, "SHA"))
self.ui.action_3.triggered.connect(partial(self.doli, "KTO"))
self.ui.action_4.triggered.connect(partial(self.secr, "KTO"))
self.ui.action_5.triggered.connect(partial(self.doli, "BL"))
self.ui.action_6.triggered.connect(partial(self.secr, "BL"))
self.widget_2=QtWidgets.QWidget(self)
self.widget_2.setGeometry(QtCore.QRect(0, 20, 801, 531))
def doli(self,a):
global rejim
rejim=a
self.widget_2.hide()
self.widget_2=Generat(self)
self.widget_2.setGeometry(QtCore.QRect(0, 15, 801, 531))
self.widget_2.show()
def secr(self,a):
global rejim
rejim=a
self.widget_2.hide()
self.widget_2=Secret(self)
self.widget_2.setGeometry(QtCore.QRect(0,15, 801, 531))
self.widget_2.show()
#Инициализация программы
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())