-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcypher_v2.py
More file actions
90 lines (73 loc) · 2.39 KB
/
cypher_v2.py
File metadata and controls
90 lines (73 loc) · 2.39 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
import random
from _math import is_prime
import secrets
import time
import tqdm
import os
import sys
class Parameter():
def __init__(self) -> None:
self.Q = self.generate_prime(160)
self.R = random.getrandbits(20)
self.P = self.cal_P()
self.P_path = f"./thamso/P.txt"
def generate_prime(self, bit):
Q = random.getrandbits(bit)
while not is_prime(Q):
Q = random.getrandbits(bit)
return Q
def cal_P(self):
while True:
P = self.Q * self.R + 1
if is_prime(P):
print("P = ", P)
return P
else:
self.Q = self.generate_prime(160)
self.R = random.getrandbits(20)
pass
def write_P(self):
with open(self.P_path, "w") as f:
f.write(str(self.P))
def generate_mathching_pair(self):
Q = self.generate_prime(160)
with open(self.P_path, "w") as f:
for R in range(1, 100000):
# calculate the value of P
P =Q * R + 1
# check if P is prime
if is_prime(P):
# write the matching pair to file
f.write(f"({P}, {R})\n")
def select_g(self):
input_path = f"./thamso/PR.txt"
out_path = f"./thamso/tapG.txt"
with open(input_path, "r") as f:
pairs = [eval(line.strip()) for line in f]
valid_g = False
while not valid_g:
P, R = random.choice(pairs)
h = random.randint(1, P-1)
g = pow(h, R, P)
if pow(g, R ,P) != 1:
valid_g = True
with open(out_path, "w") as f:
f.write(f"{g}\n")
x = g
with open(f"./thamso/giatrix.txt", "w") as f:
f.write(f"{x}\n")
print("x = ", x)
print("x saved successfully")
def select_a(self):
a = random.randint(2, 200)
print("a = ", a)
with open(f"./thamso/a.txt", "w") as f:
f.write(str(a))
if __name__ == "__main__":
app = Parameter()
app.write_P()
print("Q = ", app.Q)
print("R = ", app.R)
app.generate_mathching_pair()
app.select_g()
app.select_a()