-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
155 lines (121 loc) · 3.21 KB
/
utils.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
import numpy as np
import random
import time
import matplotlib.pyplot as plt
import math
def GCD(a, b):
if b == 0:
return a
return GCD(b, a % b)
def extendedEuclid(a, b):
if b == 0:
return (1, 0)
(x, y) = extendedEuclid(b, a % b)
k = a // b
return (y, x - k * y)
def modularExponentiate(a, n, mod):
if n == 0:
return 1 % mod
elif n == 1:
return a % mod
f = 1
binaryB = bin(n)[2:]
for i in range(len(binaryB)):
f = (f*f) % mod
if binaryB[i] == '1':
f = (f * a) % mod
return f
def modularInverse(a, n):
(b, x) = extendedEuclid(a, n)
if b < 0:
b = (b % n + n) % n
return b
def ConvertToInt(message_str):
res = 0
for i in range(len(message_str)):
res = res * 256 + ord(message_str[i])
return res
def ConvertToStr(n):
res = ""
while n > 0:
res += chr(n % 256)
n //= 256
return res[::-1]
def Encrypt(m, e, n):
m = ConvertToInt(m)
c = modularExponentiate(m, e, n)
return c
def Decrypt(c, d, p, q):
m = modularExponentiate(c, d, p * q)
m = ConvertToStr(m)
return m
def divideMsg(msg, n):
msg_blocks = []
begin = 0
msg_len = len(msg)
step = math.floor(math.log(n, 256))
if(msg_len > math.log(n, 256)): # need to divide
for start in range(begin, len(msg), step):
if(start + step > len(msg)-1):
msg_blocks.append(msg[start:])
else:
msg_blocks.append(msg[start:start+step])
else:
msg_blocks = msg
return msg_blocks
def encryptEncompass(msg, exponent, modulo):
msg_blocks = divideMsg(msg, modulo)
cipher_blocks = [Encrypt(chunk, exponent, modulo) for chunk in msg_blocks]
return cipher_blocks
def decryptEncompass(cipher_blocks, d, p, q):
message = [Decrypt(chunk, d, p, q) for chunk in cipher_blocks]
message = "".join(message)
return message
def getPublicKey(phi_n):
e = random.randrange(1, phi_n)
while e < 1 or GCD(e, phi_n) != 1:
e = random.randrange(1, phi_n)
return e
def getPrivateKey(e, p, q):
phi_n = (p - 1) * (q - 1)
d = modularInverse(e, phi_n)
return d
def nBitRandom(n):
return random.getrandbits(n) + (2**(n-1)+1)
def fermatPrimalityTest(p):
"""
a:random integer
p:the number to test if prime or not
"""
if p <= 1:
return False
for _ in range(1, 102):
# a=np.random.randint(1,p,dtype=np.int64)
a = random.randint(1, p+1)
aPowP = modularExponentiate(a, p, p)
if (aPowP - a) % p != 0:
return False
return True
def generatePrime(n):
if n == 1:
return -1
number = 1
while not fermatPrimalityTest(number):
number = nBitRandom(n)
return number
def generatePrimeModuli(n):
p = 1
q = 1
nArray = []
for i in range(2, int(n/2)+1):
# for _ in range(2):
# while (not fermatPrimalityTest(p)):
# p = nBitRandom(i)
# while (not fermatPrimalityTest(q)):
# q = nBitRandom(i)
p = generatePrime(i)
q = generatePrime(i)
while p == q:
q = generatePrime(i)
nArray.append(p*q)
return nArray