-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
129 lines (109 loc) · 3.38 KB
/
cli.py
File metadata and controls
129 lines (109 loc) · 3.38 KB
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
import random
from decimal import Decimal
FIELD_SIZE = 10 ** 3
def reconstruct_secret(shares):
sums = 0
for j, share_j in enumerate(shares):
xj, yj = share_j
prod = Decimal(1)
for i, share_i in enumerate(shares):
xi, _ = share_i
if i != j:
prod *= Decimal(Decimal(xi) / (xi - xj))
prod *= yj
sums += Decimal(prod)
return int(round(Decimal(sums), 0))
def polynom(x, coefficients):
point = 0
for coefficient_index, coefficient_value in enumerate(coefficients[::-1]):
point += x ** coefficient_index * coefficient_value
return point
def coeff(t, secret):
coeff = [random.randrange(0, FIELD_SIZE) for _ in range(t - 1)]
coeff.append(secret)
return coeff
def generate_shares(n, m, secret):
coefficients = coeff(m, secret)
shares = []
for i in range(1, n + 1):
x = i
shares.append((x, polynom(x, coefficients)))
with open("shares.txt", "w", encoding='utf8') as fout:
for i in shares:
fout.write(str(i) + "\n")
return shares
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def encrypt():
p = 13
q = 31
r = 157
s = 173
n = p * q * r
totient = (p - 1) * (q - 1) * (r - 1)*(s-1)
e = d = -1
ct = 0
for i in range(2, totient):
if ct == 1:
break
if gcd(i, totient) == 1:
e = i
ct += 1
ed = 1
while True:
ed = ed + totient
if ed % e == 0:
d = int(ed / e)
break
num = int(input("Enter the total number of shares you wish to make : "))
threshold = int(input("Enter the threshold number of shares required to reconstruct the key : "))
shares = generate_shares(num, threshold, d)
print(f"\nThe shares are as follows : \n{shares}\nPlease store them securely.\n")
with open("input.txt", "r", encoding='utf8') as fin:
message = fin.read()
print(f"The message in the file is :\n{message}")
msg = [ord(i) for i in message]
enc = [pow(i, e, n) for i in msg]
print("\nThe encrypted message is :")
with open("encrypted.txt", "w", encoding='utf8') as fout:
for i in enc:
fout.write(chr(i))
print(chr(i), end="")
def decrypt():
p = 13
q = 31
r = 157
s = 173
n = p * q * r
totient = (p - 1) * (q - 1) * (r - 1)*(s-1)
e = d = -1
ct = 0
t = int(input('Enter the number of shares you have :'))
shares = []
for i in range(t):
ind = int(input("Enter the index number of the share : "))
val = int(input("Enter the value of the share : "))
shares.append((ind, val))
# print(reconstruct_secret(shares))
d = reconstruct_secret(shares)
with open("encrypted.txt", "r", encoding='utf8') as fin:
encmsg = fin.read()
print(f"The encrypted message is :\n{encmsg}")
enc = [ord(i) for i in encmsg]
try:
dec = [pow(i, d, n) for i in enc]
print("\nThe decrypted message is :")
with open("decrypted.txt", "w", encoding='utf8') as fout:
for i in dec:
fout.write(chr(i))
print(chr(i), end="")
except:
print("Minimum shares not reached")
choice = int(input("MENU\n1.Encryption\n2.Decryption\n\nEnter your choice : "))
if choice == 1:
encrypt()
elif choice == 2:
decrypt()