-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.js
277 lines (246 loc) · 7.47 KB
/
rsa.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (c) 2020 MICHAEL LOGAN GARRETT.
* This file is licensed under the MIT License.
* A copy of the license may be found at:
* https://cefns.nau.edu/~mlg238/rsa/license.txt
*/
/*
* This file drives an implementation of the RSA cryptographic algorithm
* in Javascript. It interacts with inputs and outputs in the interface file
* "index.html", and performs expensive computations asynchronously with
* Web Workers via "doCrypto.js".
*
* Because RSA involves rather extreme exponentiation, this implementation
* uses Javascript's "BigInt" datatype. Javascript represents BigInts with
* Strings behind the scenes, and while this (allegedly) allows for
* operations on arbitrarily large numbers, it's slow as hell. Furthermore,
* some browsers are configured by default to disallow large Javascript memory
* allocations, so if your decryption isn't working and your secret exponent
* 'd' is more than about 50000, your browser might be shutting it down.
* Modifications to browser configuration settings may enable you to do some
* pretty big numbers, but obviously I cannot endorse this sort of thing. Use
* at your own risk.
*/
/* ========== BEGIN ========== */
/* some primes for testing */
var largePrimes =
[149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
419, 421, 431, 433, 439, 839, 853, 857, 859, 863,
1523, 1531, 1543, 1549, 1553, 2311, 2333, 2339, 2341, 2347]
/* elements of RSA encryption */
var p // the p
var q // the q
var m // the message
var n // rsa modulus
var keyBits // number of bits in the key
var phi // the phi
var e // the e
var d // the d
/* textboxes */
var pBox = document.getElementById("p")
var qBox = document.getElementById("q")
var nBox = document.getElementById("n")
var phiBox = document.getElementById("phi")
var eBox = document.getElementById("e")
var dBox = document.getElementById("d")
var mBox = document.getElementById("m")
var cBox = document.getElementById("c")
var encTextBox = document.getElementById("encryptedText")
var decTextBox = document.getElementById("decryptedText")
var primesTable = document.getElementById("primesTable")
var btnCalculateN = document.getElementById("btn_calculateN")
var keyBitsTextBox = document.getElementById("keyBits")
var btnCalculatePhi = document.getElementById("btn_calculatePhi")
var btnCalculateE = document.getElementById("btn_calculateE")
var btnCalculateD = document.getElementById("btn_calculateD")
var encTimeBox = document.getElementById("encTime")
var encTimerTextBox = document.getElementById("encTimerTextBox")
var decTimeBox = document.getElementById("decTime")
var decTimerTextBox = document.getElementById("decTimerTextBox")
var encErrBox = document.getElementById("encErrBox")
/* image to show while async processing */
var loadingImage = document.getElementById("loading")
/* webworker for doing asynchronous computation of d, encryption,
* decryption
*/
var webWorker = new Worker('doCrypto.js')
/* computes N from the input values */
function calculateN()
{
p = BigInt(pBox.value)
q = BigInt(qBox.value)
n = p * q
keyBits = parseInt(Math.log2(n.toString()))+1
nBox.innerHTML = n
keyBitsTextBox.innerHTML = "("+keyBits+"-bit key)"
}
/* computes phi from p, q, and n */
function calculatePhi()
{
phi = (p-1n)*(q-1n)
phiBox.innerHTML = phi
}
/* chooses an E with the chooseE function */
function getE()
{
e = chooseE(p, q, phi)
eBox.innerHTML = e
}
/* calculates the secret exponent d asynchronously via web worker */
function calculateD()
{
dBox.innerHTML = " "
webWorker.onmessage = function(event) {
result = event.data
swapButtonClass('btn_calculateD', 'buttons')
dBox.innerHTML = result
d = result
}
swapButtonClass('btn_calculateD', 'buttonDLoading')
webWorker.postMessage([e, phi])
}
/* chooses an e that meets the RSA criteria by brute force */
function chooseE(p, q, phi)
{
var e = 2n
while(e < phi)
{
if(gcd(e, phi)==1)
break;
else
e++
}
return e
}
/* encrypts the input message asynchronously */
function doEncrypt()
{
encErrBox.innerHTML = ""
var result
m = BigInt(document.getElementById("m").value)
if(BigInt(mBox.value) >= (n-1n))
{
encErrBox.innerHTML += "must be < " + (n-1n) + "<br>"
return
}
if(pBox.value == qBox.value)
{
encErrBox.innerHTML += "p and q must be different<br>"
dBox.innerHTML = " "
eBox.innerHTML = " "
phiBox.innerHTML = " "
nBox.innerHTML = " "
return;
}
encErrBox.innerHTML = ""
webWorker.onmessage = function(event) {
result = event.data
encTextBox.innerHTML = result[0]
encTimeBox.innerHTML = result[1]
setLoadingImageVisible('loadingEnc', false)
}
encTextBox.innerHTML = " "
setLoadingImageVisible('loadingEnc', true)
webWorker.postMessage([m, e, n, 'encrypt'])
encTimeBox.innerHTML = " "
}
/* decrypts the input ciphertext asynchronously */
function doDecrypt()
{
var result
c = BigInt(document.getElementById("c").value)
webWorker.onmessage = function(event) {
result = event.data
decTextBox.innerHTML = result[0]
decTimeBox.innerHTML = result[1]
setLoadingImageVisible('loadingDec', false)
}
decTextBox.innerHTML = " "
setLoadingImageVisible('loadingDec', true)
webWorker.postMessage([c, d, n, 'decrypt'])
decTimeBox.innerHTML = " "
}
/* computes the gcd of two values by recursion */
function gcd(a, b) {
if (b == 0)
return a
else
return gcd(b, (a % b))
}
/* function called when form fields are modified
* can be used to validate input in real time
*/
function validateForm()
{
// does nothing at this time
}
/* generates the table of primes at the bottom of the page from the
* list in this file
*/
function generateTable()
{
var pIndex = 0
var table = document.getElementById("primesTable")
for(var i=0; i<3; i++)
{
var row = document.createElement("tr")
row.setAttribute("class", "primes")
for(var j=0; j<10; j++)
{
var col = document.createElement("td")
col.setAttribute("class", "primes")
col.innerHTML = largePrimes[pIndex]
pIndex++
row.appendChild(col)
}
table.appendChild(row)
}
}
/* helper function to toggle visibility of loading animations */
function setLoadingImageVisible(id, visible)
{
var img = document.getElementById(id);
img.style.visibility = (visible ? 'visible' : 'hidden')
}
/* helper function to toggle visibility of timer textboxes */
function setTimerTextBoxVisible(id, visible)
{
var element = document.getElementById(id)
id.style.visibility = (visible ? 'visible' : 'hidden')
}
/* helper function to swap the class of a button
* used for "calculate d" button to show loading image
* during asynchronous computation
*/
function swapButtonClass(id, cls)
{
var button = document.getElementById(id)
button.className = cls
if(cls === "buttonDLoading")
{
button.value = ""
}
else if(cls === "buttons")
{
button.value = "calculate d"
}
}
/* resets the form fields and calculated textboxes */
function resetFields()
{
nBox.innerHTML = " "
keyBitsTextBox.innerHTML = " "
phiBox.innerHTML = " "
eBox.innerHTML = " "
dBox.innerHTML = " "
pBox.value = ""
qBox.value = ""
encTextBox.innerHTML = " "
decTextBox.innerHTML = " "
cBox.value = ""
encErrBox.innerHTML = ""
mBox.value = ""
encTimeBox.innerHTML = " "
decTimeBox.innerHTML = " "
}
/* ========== END ========== */