-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_key1.js
74 lines (60 loc) · 1.5 KB
/
rsa_key1.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
// person A
const p1 = 53;
const p2 = 59;
const n = p1 * p2;
const phi = (p1 - 1) * (p2 - 1);
console.log(n, phi);
/* formula:
m^ed mod n = m^c*phi+1 mod n
*/
const e = 3; // encryption key
const k = 2; // constant
const d = (k * phi + 1) / e; // decryption key
console.log(d);
// to send as public key
const lock = {
num: n,
ekey: e,
};
//-------------------
// person B
//sending messages
const message = 'HI Hello ray';
const charToAscii = function (str) {
let msg = [];
Array.from(str).forEach(function (e) {
msg.push(e.charCodeAt());
});
console.log(msg.join('*'));
return msg.join('*');
};
const msg_send = charToAscii(message);
console.log(msg_send);
// locking
const locking = function (msg) {
const crypt = [];
msg_send.split('*').forEach(function (m) {
crypt.push(m ** lock.ekey % lock.num);
});
return crypt.join('*');
};
const cryptic_msg = locking(msg_send);
console.log('cryptic_msg: ', cryptic_msg);
//----------------------------
// sending back to A with locking
// receiving msg by A and decrypting the msg
/* formula:
*/
const unlocking = function (msg) {
let decrypt = [];
let real_message = [];
msg.split('*').forEach(function (c) {
decrypt.push(Number(BigInt(c) ** BigInt(d) % BigInt(n)));
});
decrypt.forEach(function (m) {
real_message.push(String.fromCharCode(m));
});
return real_message.join('');
};
const realMessage = unlocking(cryptic_msg);
console.log(realMessage);