-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.js
144 lines (120 loc) · 3.02 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
'use strict';
const primes = require('./prime-numbers')();
const offset = -64;
/**
* More efficient way of calculating: Math.pow(code, power) % mod
* @param code - char code
* @param power - d or e
* @param mod - n
* @returns reminder (encrypted or decrypted value)
*/
const transform = (code, power, mod) => {
let value = code;
for (let j = 0; j < power - 1; j++) {
value = (value * code) % mod;
}
return value;
};
/**
* Creates object that allows encrypting data
* @param d
* @param n
* @returns {Object} PrivateKey
* @constructor
*/
const PrivateKey = (d, n) => {
const encrypt = text => {
const encrypted = [];
// iterates each character and applies Math.pow(code, d) % n
for (let i = 0; i < text.length; i++) {
const code = text.charCodeAt(i) + offset;
const encryptedText = transform(code, d, n);
encrypted.push(encryptedText);
}
// returns an array of encrypted characters
return encrypted;
};
const getD = () => {
return d;
};
const getN = () => {
return n;
};
return {
encrypt: encrypt,
getD: getD,
getN: getN
};
};
/**
* Creates object that allows decrypting data
* @param e
* @param n
* @returns {Object} PublicKey
* @constructor
*/
const PublicKey = (e, n) => {
const decrypt = encrypted => {
// iterates through array of encoded char values
return encrypted.map(code => {
// Applies Math.pow(code, e) % n
return transform(code, e, n);
}).reduce((prev, current) => {
// concat a list of characters in one string
return prev + String.fromCharCode(current - offset);
}, '');
};
const getE = () => {
return e;
};
const getN = () => {
return n;
};
return {
decrypt: decrypt,
getE: getE,
getN: getN
};
};
/**
*
* @param primeIndexOne {Integer} (Optional) Index of first prime number for generating key
* @param primeIndexTwo {Integer} (Optional) Index of second prime number for generating key
* @returns {Object} public and private RSA keys
* @constructor
*/
const GenerateKey = (primeIndexOne = -1, primeIndexTwo = -1) => {
if (primeIndexOne < 0) {
// randomly selects an prime index (10 - 39)
// small primes seems to fail
// for example 3 and 7
primeIndexOne = Math.floor(Math.random() * 20) + 10;
}
if (primeIndexTwo < 0) {
primeIndexTwo = primeIndexOne;
// finds a different unique prime index
while (primeIndexOne === primeIndexTwo) {
primeIndexTwo = Math.floor(Math.random() * 20) + 10;
}
}
// finds primes with corresponding indexes
const primeOne = primes.find(primeIndexOne);
const primeTwo = primes.find(primeIndexTwo);
const n = primeOne * primeTwo;
const z = (primeOne - 1) * (primeTwo - 1);
// finds a prime that is relative to z
const d = primes.coprime(z);
let e = 0;
while ((e * d) % z !== 1) {
e++;
}
return {
publicKey: PublicKey(e, n),
privateKey: PrivateKey(d, n)
};
};
module.exports = {
GenerateKey: GenerateKey,
PrivateKey: PrivateKey,
PublicKey: PublicKey
};