fix rsa implementation

This commit is contained in:
Mylloon 2022-10-26 15:37:35 +02:00
parent 361078e25c
commit a4dfb37a7d
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 64 additions and 46 deletions

View file

@ -1,4 +1,4 @@
import { gen_RSA_keypair, RSA_enc } from "./rsa.js";
import { gen_RSA_keypair, RSA_dec, RSA_enc } from "./rsa.js";
window.addEventListener("load", () => main());
@ -57,15 +57,18 @@ const send = (file, element) => {
}
// Encrypt the file
gen_RSA_keypair(1024).then((keys) =>
console.log(`p=${keys[0]}, s=${keys[1]}`)
);
file.text().then((content) => {
gen_RSA_keypair(1024).then((keys) => {
let x = RSA_enc(content, keys[1]);
console.log(RSA_dec(x, keys[0]));
// Send it
const data = new FormData();
data.append("file", file);
// Send it
const data = new FormData();
data.append("file", file);
const req = new XMLHttpRequest();
req.open("POST", "upload");
req.send(data);
const req = new XMLHttpRequest();
req.open("POST", "upload");
req.send(data);
});
});
};

View file

@ -1,13 +1,11 @@
BigNumber.prototype.floor = function () {
return this.integerValue(BigNumber.ROUND_FLOOR);
};
import { modInv, modPow } from "./libs/bigint-mod.js";
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));
ok(BigInt(data));
});
});
};
@ -18,36 +16,59 @@ export const gen_RSA_keypair = async (bits) => {
const q = await getPrime(pq_size);
console.assert(p != q);
const n = p.times(q);
const n = p * q;
const phi_n = p.minus(1).times(q.minus(1));
const phi_n = (p - 1n) * (q - 1n);
const e = new BigNumber(65537);
console.assert(e.isLessThan(phi_n) && phi_n.modulo(e) != 0);
const e = 65537n;
console.assert(e < phi_n && phi_n % e != 0n);
// 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)];
}
const d = modInv(e, phi_n);
return ((y % m) + m) % m;
};
const d = inverse(e, phi_n);
console.log((e, n), (d, n));
return [(e, n), (d, n)];
return [
[e, n],
[d, n],
];
};
const RSA = () => {};
const RSA = (msg, key) => {
return modPow(msg, key[0], key[1]);
};
export const RSA_enc = (key, data) => {};
export const RSA_enc = (data, key) => {
return RSA(str_to_int(data)[0], key);
};
export const RSA_dec = (data, key) => {
return int_to_str([RSA(data, key)]);
};
const str_to_int = (msg) => {
let result = "";
let table = [];
for (let i = 0; i < msg.length; i++) {
result += String(msg[i].charCodeAt(0)).padStart(3, "0");
if (result.length == 100) {
table.push(BigInt(`1${result}`));
}
}
table.push(BigInt(`1${result}`));
return table;
};
const int_to_str = (msg) => {
let result = "";
for (let k = 0; k < msg.length; k++) {
let full_msg = msg[k].toString().slice(1);
for (let i = 0, step = 3; i < full_msg.length; i += step) {
let txt = "";
for (let j = 0; j < step; j++) {
txt += full_msg.charAt(i + j);
}
result += String.fromCharCode(parseInt(txt));
}
}
return result;
};

View file

@ -40,12 +40,6 @@
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>