-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrt.py
131 lines (92 loc) · 2.76 KB
/
crt.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
import helper_functions
class Message:
p=0
q=0
def __init__(self,M):
self.M = M
def generateKeys():
# print("generating key 1 - p ....")
p = helper_functions.generate_prime_number(10)
# print("p:/n",p)
# print("generating key 2 - q ....")
q = helper_functions.generate_prime_number(10)
while(p==q):
# print("p and q were equal regenrating")
q=helper_functions.generate_prime_number(10)
# print("q:/n",q)
# generating key
n = p * q
# print("your private key = ", n)
# print("generating z ...")
z= helper_functions.generate_prime_number(10)
while(z==p or z==q or helper_functions.gcd(n,z)!=1):
# print("z failed conditions, generating again")
z = helper_functions.generate_prime_number(10)
# print("z:/n",z)
# print("calculating mu ...")
mu = (helper_functions.modInverse(p,q)*p)+(helper_functions.modInverse(q,p)*q)
# print("mu:/n",mu)
# print("n:/n",p*q)
x = n*z
# print(p,q,z,mu)
return p,q,z,mu,x,n
def generating_random_numbers():
k1=3
k2=4
return k1,k2
# keys = generateKeys()
# def encrypt_crt(M1,k1):
# # generating keys and additional calculations
# global p
# global q
# p, q, z, mu, x,n = generateKeys()
# print("generating keys finished!")
# print("your private key = ",n)
# # phi(n)
# phin = helper_functions.phi(p*q)
# # base = M1**((k1*phin)+1)
# now = ((M1 % x) ** (k1 * phin + 1)) % x
# print("your encrypted message: ",now)
# return now
def encrypt_crt(M1,k1):
# generating keys and additional calculations
global p
global q
p, q, z, mu, x,n = generateKeys()
print("generating keys finished!")
print("your private key = ",n)
# phi(n)
phin = helper_functions.phi(p*q)
# base = M1**((k1*phin)+1)
# now = pow(M1,k1*phin+1,x)
l = helper_functions.convert_to_int(M1)
enc=[]
for i in l:
# now = ((M1 % x) ** (k1 * phin + 1)) % x
now = pow(i,k1*phin+1,x)
enc.append(str(now))
newenc = ' '.join(enc)
print(newenc)
return newenc
# C1 = encrypt_crt(99899,3)
# print("encrypted text=",C1)
# def decrypt_crt(C1,key):
# #key = int(input("Enter key: "))
#
# x = C1%(key)
# print("decypted message: ",x)
# return x
def decrypt_crt(C1,key):
C1 = C1.split(' ')
dec = []
text = ""
for i in C1:
i = int(i)
dec.append(i%(key))
# return C1%(p*q)
for i in dec:
text = text + chr((i + 96))
print("decrypted text = ",text)
return text
# newM1 = decrypt_crt(C1,key)
# print("decrypted text= ",newM1)