wip: rsa impl
This commit is contained in:
parent
364b196293
commit
84d6d87d98
3 changed files with 75 additions and 2 deletions
|
@ -1,3 +1,5 @@
|
|||
import { gen_RSA_keypair, RSA_enc } from "./rsa.js";
|
||||
|
||||
window.addEventListener("load", () => main());
|
||||
|
||||
const main = () => {
|
||||
|
@ -54,7 +56,12 @@ const send = (file, element) => {
|
|||
parent.appendChild(newText);
|
||||
}
|
||||
|
||||
// TODO: Encrypt the file before sending it
|
||||
// Encrypt the file
|
||||
gen_RSA_keypair(1024).then((keys) =>
|
||||
console.log(`p=${keys[0]}, s=${keys[1]}`)
|
||||
);
|
||||
|
||||
// Send it
|
||||
const data = new FormData();
|
||||
data.append("file", file);
|
||||
|
||||
|
|
53
src/public/js/rsa.js
Normal file
53
src/public/js/rsa.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
BigNumber.prototype.floor = function () {
|
||||
return this.integerValue(BigNumber.ROUND_FLOOR);
|
||||
};
|
||||
|
||||
export const gen_RSA_keypair = async (bits) => {
|
||||
const getPrime = (bits) => {
|
||||
return new Promise((ok, ko) => {
|
||||
forge.prime.generateProbablePrime(bits, (err, data) => {
|
||||
if (err) return ko(err);
|
||||
ok(new BigNumber(data));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const pq_size = bits / 2;
|
||||
|
||||
const p = await getPrime(pq_size);
|
||||
const q = await getPrime(pq_size);
|
||||
console.assert(p != q);
|
||||
|
||||
const n = p.times(q);
|
||||
|
||||
const phi_n = p.minus(1).times(q.minus(1));
|
||||
|
||||
const e = new BigNumber(65537);
|
||||
console.assert(e.isLessThan(phi_n) && phi_n.modulo(e) != 0);
|
||||
|
||||
// https://stackoverflow.com/a/51562038/15436737
|
||||
const inverse = (a, m) => {
|
||||
const s = [];
|
||||
let b = m;
|
||||
while (b) {
|
||||
[a, b] = [b, a % b];
|
||||
s.push({ a, b });
|
||||
}
|
||||
let x = 1;
|
||||
let y = 0;
|
||||
for (let i = s.length - 2; i >= 0; --i) {
|
||||
[x, y] = [y, x - y * Math.floor(s[i].a / s[i].b)];
|
||||
}
|
||||
|
||||
return ((y % m) + m) % m;
|
||||
};
|
||||
|
||||
const d = inverse(e, phi_n);
|
||||
|
||||
console.log((e, n), (d, n));
|
||||
return [(e, n), (d, n)];
|
||||
};
|
||||
|
||||
const RSA = () => {};
|
||||
|
||||
export const RSA_enc = (key, data) => {};
|
|
@ -34,6 +34,19 @@
|
|||
<p>NPNO</p>
|
||||
</footer>
|
||||
|
||||
<script src="../js/main.js"></script>
|
||||
<script
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/forge/1.3.1/forge.min.js"
|
||||
integrity="sha512-95iy0RZIbw3H/FgfAj2wnCQJlzFQ+eaSfUeV/l8WVyGHKSRMzm3M/O+85j9ba/HFphkijrCTDjcuDX0BL2lthA=="
|
||||
crossorigin="anonymous"
|
||||
referrerpolicy="no-referrer"
|
||||
></script>
|
||||
<script
|
||||
src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.1.0/bignumber.min.js"
|
||||
integrity="sha512-w9skcoMiLIpBsmoP7TBpXooZl4nnj7z354N2w+T52bBy965eXP68U6cUvq5/aXGmn8YmwNAirnUtQPPPjk1prQ=="
|
||||
crossorigin="anonymous"
|
||||
referrerpolicy="no-referrer"
|
||||
></script>
|
||||
|
||||
<script type="module" src="../js/main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Reference in a new issue