@SureshPatil/

# RSA

## No description

Files
• main.py
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
```
```import math
import random

def prepare_string(s, alphabet):
newS = ""
for char in s:
if char in alphabet:
newS += char
elif char in alphabet.upper() or char in alphabet.lower():
newS += char.upper()
return newS

def trial(n, d, r):
a = random.randint(2, n-1)
check = pow(a, d, n) - 1
if check%n == 0:
return True
for s in range(0, r):
check = pow(a, d * pow(2, s), n) + 1
if check%n == 0:
return True
return False

def isPrime(p):
if p%2 == 0:
return False
r = 0
d = p - 1
while True:
if d % 2 != 0:
break
else:
r += 1
d = d//2
for i in range(10):
if trial(p, d, r) == False:
return False
return True

def findPrime(number):
if number % 2 == 0:
number += 1
while isPrime(number) == False:
number += 2
return number

def generate_primes(n):
# n = 512
print('Your choice of low bound is two to the following power:', n - 1)
print('Your choice of upper bound is two to the following power:', n)
primes = []
for x in range(2):
rando = random.randint(2**(n-1) + 1, 2**n)
primes.append(findPrime(rando))
return primes

def mod_inverse(e, m):
gNums = []
gNums.append(m//e)
newt = e
remainder = m%e
while remainder > 1:
gNums.append(newt//remainder)
temp = newt
newt = remainder
remainder = temp%remainder

blue = 1
red = -1 * gNums[-1]
reverse = gNums[::-1]
for index in range(1, len(reverse)):
temp = red
red = blue + red*-1*reverse[index]
blue = temp
return red%m

def generateSubstringLength(alphabet, m):
length = 0
n = len(alphabet)
while n < m:
n = n*len(alphabet)
length += 1
return length

def generateSubstrings(message, n):
numberOfIters = 1 + len(message)//n
substrings = []
substring = ''
for index in range(0, n*numberOfIters):
if index >= len(message):
substring += 'Z'
if index == n*numberOfIters - 1:
substrings.append(substring)
elif index%n == 0 and len(substring) != 0:
substrings.append(substring)
substring = message[index]
else:
substring += message[index]
return substrings

def convertToNumbers(substrings, alphabet):
numSubs = []
for sub in substrings:
num = 0
for index, char in enumerate(sub[::-1]):
num += (pow(len(alphabet), index) * alphabet.index(char))
numSubs.append(num)
return numSubs

def encryptNums(numSubs, e, m):
encryptedNums = []
for numSub in numSubs:
encryptedNums.append(pow(numSub, e, m))
return encryptedNums

def decryptNums(encSubs, d, m):
decryptedNums = []
for encSub in encSubs:
decryptedNums.append(pow(encSub, d, m))
return decryptedNums

def backToText(numSubs, alphabet, n):
text = ''
for numSub in numSubs:
subString = ''
num = numSub
for x in range(n):
subString += alphabet[(num%len(alphabet))]
num = num//len(alphabet)
text += subString[::-1]
return text

def RSAencrypt(message, alphabet, m, e):
message = prepare_string(message, alphabet)
print('Prepared input is:', message)
n = generateSubstringLength(alphabet, m)
print('Alphabet length is', len(alphabet), 'and the highest power is', n)
substrings = generateSubstrings(message, n)
print('Broken into substrings:', substrings)
numSubstrings = convertToNumbers(substrings, alphabet)
print('Become numbers:', numSubstrings)
encryptedNums = encryptNums(numSubstrings, e, m)
return encryptedNums, n

decryptedNums = decryptNums(encryptedNums, d, m)
print('Decoded to:', decryptedNums)
text = backToText(decryptedNums, alphabet, n)
return text

# primes = generate_primes(512)
# p = primes[0]
# q = primes[1]

p = 12146506381645834038938637694315560145605522481099001678525961130513503330983463286913894837422547560452046476688310536561557005074571465528726771144222297
q = 6841181471228144056720961233011919562929461148315947269150349289240990263876119140774193696991680295609812474830147505476097737728291793488537259523362993
# m = 118456637130996291897701363514649223742730255290953274497573340489819788458809841625513215849730650994620957302637587276442320965838347877245637657219366670879219650786029986227509829596496141634803043613733661026530022954365284467996401208401516321999492317933852513583641307593524044828329609295781595900489
# print('Your two primes are:', p, q)
m = p*q
#  m = 2494279923802253606287472092668310204875649561581672691813431640440219048972006082731965879
# # m = 115712558842134048205836272587056285480849629024677293541124880937705363858182343422554859445264360845873174501056273523921951220839005618612364262396504674060761497947465562923755042278171369880531283078967072297006652846358720770194248892054534111830448794138268830444592232064373366348538613829914681686329
# [786455771834685379588950282669850118588542062416352994035757229411495455300397962388489222]
# m = 48270922816026598154405983813556401404019974982778163090941399490970552656182570083633733757248228735008677104809287802044685027005977076591483908293176709641654912262747801602561438391534465854949071877669299431251310886819410334698249356300151998546621506144019003019172020663596910738612128874579241555391
#eckel
# m = 147690143426417720543026043890448472042375285573365468767128662033037446713246419100204917883446300400801225405192554010954725249626347314234029446068634707387105420955618605845881594400970904496218962357799602434736918859856803813645781043520129383769019885018939343481635017846708963422603112278784470086259
totm = (p-1) * (q-1)
print('The mod is:', m, 'and the totient is:', totm)
e = 65537
while math.gcd(e, totm) != 1:
e += 2
d = mod_inverse(e, totm)
print('e is:', e, 'and d is:',d, 'and if this worked, this is 1:', (e*d)%totm );

message = "I REVEAL TO YOU A SECRET TRUTH. SALT AND VINEGAR CHIPS ARE THE WORST FOOD EVERCREATED. SIGNATURE"
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ., []0123456789"

# message = prepare_string(message, alphabet)
# print('Prepared input is:', message)
n = generateSubstringLength(alphabet, m)
# print('Alphabet length is', len(alphabet), 'and the highest power is', n)
# substrings = generateSubstrings(message, n)
# print('Broken into substrings:', substrings)
# numSubstrings = convertToNumbers(substrings, alphabet)
# print('Become numbers:', numSubstrings)
# encryptedNums = encryptNums(numSubstrings, e, m)
# encryptedNums, n = RSAencrypt(message, alphabet, m, e)
# encryptedNums = [67547512390119212362317356205157957343537636631783200507171132129774996393206951944617285987994472100063727248702567127881706364241348186886760226921300668493429459027201579370185507017518555561432628313625635228418323972766011950754905403157277005774143560844773553093109092969904909792622864463554261245963]
# encryptedNums = [96925331730787106206916889776473268859473570971930867654981869430948114750987276730906578667968134757237819721745875139347384392136628070139051415447304885910981753690527960559094958060411445158123361562241995367084512422343562278211624566195244969036198424636459024406424218173680812075715028340603646536252]
# encryptedNums = [122641508378263313854344520400539066752471250488133550658292961161158606489793763636369099829624626697981916648353451219745741227910107308261261782798962251091883617477844167961778150465074566360243608961332311333942307134254245421617185460495696059214260058307892259298188317850076670369744191669116302686482]
# encryptedNums = [98985360556488020256831538272575601941394029597346071336057526005313684039005275745665027315781170775724315242636437217588683784868222474932153991825323636259668183479599369982777864684985767762240909348889239067620881550951805338632428511336825169328077092766493769713243542661933998196357663338151388411359]
# print('Encoded to:', encryptedNums)

encryptedNums = [27529222289130396982441972178167156131188390270854605803088482820364727777356806293149110424664547444829506048862681707047361447940307458528855188797640951349314273871265869613531475173199205446725064541554710160734703958937428787702631909934402384936987117450673463038491985907253183292938459387572382618320]

decryptedNums = decryptNums(encryptedNums, d, m)
print('Decoded to:', decryptedNums)
text = backToText(decryptedNums, alphabet, n)

# text = RSAdecrypt(encryptedNums, d, m, n)
# print('Back to text:', text)

#nathan message: Lord help the people who watch animeZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

#eckel encode: [89093387552906584825583363154107217997673969100464322264363567591711199958140149934063632551642998145172176707198655988173086105053136900284679130408045754254287160481525895317091973315163418252875440492732645814965393346600132877076985545964174119641498943237921620140543742889882444396103539277609580745293]

#Output
# a. m = 115712558842134048205836272587056285480849629024677293541124880937705363858182343422554859445264360845873174501056273523921951220839005618612364262396504674060761497947465562923755042278171369880531283078967072297006652846358720770194248892054534111830448794138268830444592232064373366348538613829914681686329
# e = 65537
# d = 34971070009468182181618413740046587133593923728113339868761905363337248685491444768152794608550721507605265451446237066460259487725107263514481855926885666417334167276379165528375870087551593834090835302664753694646312466077739445723433707624523290287334590236840349400460984087862263324825924608100496791953

# b. [96925331730787106206916889776473268859473570971930867654981869430948114750987276730906578667968134757237819721745875139347384392136628070139051415447304885910981753690527960559094958060411445158123361562241995367084512422343562278211624566195244969036198424636459024406424218173680812075715028340603646536252], Nathan Xu
# c. Lord help the people who watch animeZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
# d. [89093387552906584825583363154107217997673969100464322264363567591711199958140149934063632551642998145172176707198655988173086105053136900284679130408045754254287160481525895317091973315163418252875440492732645814965393346600132877076985545964174119641498943237921620140543742889882444396103539277609580745293]```
Fetching token