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()); window.addEventListener("load", () => main());
@ -57,9 +57,10 @@ const send = (file, element) => {
} }
// Encrypt the file // Encrypt the file
gen_RSA_keypair(1024).then((keys) => file.text().then((content) => {
console.log(`p=${keys[0]}, s=${keys[1]}`) gen_RSA_keypair(1024).then((keys) => {
); let x = RSA_enc(content, keys[1]);
console.log(RSA_dec(x, keys[0]));
// Send it // Send it
const data = new FormData(); const data = new FormData();
@ -68,4 +69,6 @@ const send = (file, element) => {
const req = new XMLHttpRequest(); const req = new XMLHttpRequest();
req.open("POST", "upload"); req.open("POST", "upload");
req.send(data); req.send(data);
});
});
}; };

View file

@ -1,13 +1,11 @@
BigNumber.prototype.floor = function () { import { modInv, modPow } from "./libs/bigint-mod.js";
return this.integerValue(BigNumber.ROUND_FLOOR);
};
export const gen_RSA_keypair = async (bits) => { export const gen_RSA_keypair = async (bits) => {
const getPrime = (bits) => { const getPrime = (bits) => {
return new Promise((ok, ko) => { return new Promise((ok, ko) => {
forge.prime.generateProbablePrime(bits, (err, data) => { forge.prime.generateProbablePrime(bits, (err, data) => {
if (err) return ko(err); 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); const q = await getPrime(pq_size);
console.assert(p != q); 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); const e = 65537n;
console.assert(e.isLessThan(phi_n) && phi_n.modulo(e) != 0); console.assert(e < phi_n && phi_n % e != 0n);
// https://stackoverflow.com/a/51562038/15436737 const d = modInv(e, phi_n);
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; return [
[e, n],
[d, n],
];
}; };
const d = inverse(e, phi_n); const RSA = (msg, key) => {
return modPow(msg, key[0], key[1]);
console.log((e, n), (d, n));
return [(e, n), (d, n)];
}; };
const RSA = () => {}; export const RSA_enc = (data, key) => {
return RSA(str_to_int(data)[0], key);
};
export const RSA_enc = (key, data) => {}; 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" crossorigin="anonymous"
referrerpolicy="no-referrer" referrerpolicy="no-referrer"
></script> ></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> <script type="module" src="../js/main.js"></script>
</body> </body>