from crypto_func import def main generate_prime_numbe 128 Miller-Rabin

 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
from crypto_func import *
e = 2
def main():
p = generate_prime_number(128) # Miller-Rabin
q = generate_prime_number(128) # Miller-Rabin
# encrypt
n = p * q
print("Module[n]:\n\t{}".format(n))
m = id_generator()
print("Message:\n\t{}".format(m))
c = pow(int_from_bytes(m.encode()), e, n)
print("Encrypt[message]:\n\t{}".format(hex(c)))
# decrypt
y_p, y_q, _ = euclidean(p, q)
m_p = isqrt(c)[0] % q
m_q = isqrt(c)[0] % p
r = int(round((y_p * p * m_q + y_q * q * m_p) % n))
r_ = n - r
s = int(round((y_p * p * m_q - y_q * q * m_p) % n))
s_ = n - s
printable_chars = bytes(string.printable, 'ascii')
if all(char in printable_chars for char in int_to_bytes(r)):
m_dec = int_to_bytes(r)
if all(char in printable_chars for char in int_to_bytes(r_)):
m_dec = int_to_bytes(r_)
if all(char in printable_chars for char in int_to_bytes(s)):
m_dec = int_to_bytes(s)
if all(char in printable_chars for char in int_to_bytes(s_)):
m_dec = int_to_bytes(s_)
print("Decrypt[message]:\n\t{}".format(m_dec.decode()))
if __name__ in '__main__':
main()