forked from AhmadJamal01/cipheRSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
153 lines (116 loc) · 3.13 KB
/
helpers.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
import sys
import threading
import numpy as np
import random
import math
from ast import literal_eval
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 getPrivateKey(e, p, q):
phi_n = (p - 1) * (q - 1)
d = modularInverse(e, phi_n)
return d
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 Encrypt(m, e, n):
msg_blocks = divideMsg(m, n)
msg_blocks_in_int = [ConvertToInt(block) for block in msg_blocks]
c = [modularExponentiate(block, e, n) for block in msg_blocks_in_int]
return c
def Decrypt(c, d, p, q):
decrypted_blocks = [modularExponentiate(block, d, p * q) for block in c]
m = [ConvertToStr(block) for block in decrypted_blocks]
m = "".join(m)
return m
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 testexponent(e, p, q):
phi_n = (p - 1) * (q - 1)
if e >= phi_n or e <= 1:
return False
if GCD(e, phi_n) != 1:
return False
return True
# c = Encrypt("hello",7,367*373)
# print(Decrypt(c , 19556 , 367 , 373))
def use_encrypt(msg, exponent, n):
msg_chunks = divideMsg(msg, n)
cipher = [Encrypt(msg, exponent, n) for msg in msg_chunks]
return cipher
def use_decrypt(c, d, p, q):
message = [Decrypt(chunk, d, p, q) for chunk in c]
message = "".join(message)
return message
def convrt(inp):
lis = literal_eval(inp)
print(lis)
convrt("[[80532, 46558], [77326, 46558]]")
def generatePrime(n):
if n == 1:
return -1
number = 1
while not fermatPrimalityTest(number):
number = nBitRandom(n)
return number