Repository navigation
Expand file tree
/
Copy pathkid-rsa.js
More file actions
144 lines (118 loc) · 3.49 KB
/
Copy pathkid-rsa.js
File metadata and controls
144 lines (118 loc) · 3.49 KB
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
const generate = function (inputs) {
console.log(`--- Generate Public x Private ---`);
// compute - M, e, d, n
const { a, b, aa, bb } = inputs;
let math_print = `
let M = a * b - 1;
let e = aa * M + a;
let d = bb * M + b;
let n = (e * d - 1) / M;
`;
console.log(`maths ${math_print}`);
let M = a * b - 1;
let e = aa * M + a;
let d = bb * M + b;
let n = (e * d - 1) / M;
let public = new Map();
public.set("e", e);
public.set("n", n);
let private = new Map();
private.set("d", d);
private.set("n", n);
let generated = {
inputs: inputs,
M: M,
e: e,
d: d,
n: n,
public: public,
private: private,
};
return generated;
};
const factors = function (num) {
// calculate factors of i, an Integer
console.log(`--- Calculate Factors for ${num} ---`);
let factors = new Set();
if (Number.isInteger(num)) {
let half = Math.floor(num / 2); // bc half * 2 = num
for (let i = half; i > 0; i--) {
if (num % i == 0) {
factors.add(i);
}
}
return factors;
}
};
const encrypt = function (m, public) {
console.log(`--- Encrypting message using generated public key ---`);
const encrypt_message = (e, m, n) => {
return (e * m) % n;
};
// let message be an integer m that is less than n, and share no factors with n
console.log(`public: `);
console.log(public);
if (m < public.get("n")) {
console.log(`m < n is True`);
let n = public.get("n");
let e = public.get("e");
let m_factors = factors(m);
let n_factors = factors(n);
// console.log(`m: ${m}`);
console.log(`m factors`);
console.log(m_factors);
// console.log(`n: ${n}`);
console.log(`n factors`);
console.log(n_factors);
const intersection = new Set(
[...m_factors].filter((factor) => n_factors.has(factor))
);
if (intersection.size > 1) {
console.log(`m and n share factors other than 1`);
console.log(intersection);
} else {
console.log(`m and n do not share factors other than 1`);
console.log(
`m is valid to be encrypted: m is less than n, and they do not share factors`
);
}
return encrypt_message(e, m, n);
}
};
const decrypt = function (encrypted, private) {
console.log(`--- Decrypting message using generated private key`);
return (encrypted * private.get("d")) % private.get("n");
};
// interesting, i tried using input numbers random 0-512 and decryption ended up being off by one,
// example of that output included in kid-rsa-off-by-1.md
let inputs = {
a: Math.floor(Math.random() * 64),
b: Math.floor(Math.random() * 64),
aa: Math.floor(Math.random() * 64),
bb: Math.floor(Math.random() * 64),
};
console.log("inputs: ");
console.log(inputs);
console.log("\n");
let generated = generate(inputs);
console.log("generated:");
console.log(generated);
console.log("\n");
let m = Math.floor(Math.random() * 512); // this is the msg to encrypt
console.log(`message to encrypt: ${m}`);
console.log(`hex: ${m.toString(16)}`);
console.log("\n");
let encrypted = encrypt(m, generated.public);
console.log("\n");
let decrypted = decrypt(encrypted, generated.private);
console.log(`m = ${m} ---encrypt---> ${encrypted} ---decrypt---> ${decrypted}`);
console.log("\n");
console.log(
`hex: ${m.toString(16)} ---encrypt---> ${encrypted.toString(
16
)} ---decrypt---> ${decrypted.toString(16)}`
);
console.log(
`${m.toString(2)} -> ${encrypted.toString(2)} -> ${decrypted.toString(2)}`
);
console.log(`done!`);