Compare commits
3 commits
84d6d87d98
...
a4dfb37a7d
Author | SHA1 | Date | |
---|---|---|---|
a4dfb37a7d | |||
361078e25c | |||
1cc02f89e3 |
9 changed files with 99 additions and 51 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -8,3 +8,4 @@ __pycache__/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
src/public/fonts/
|
src/public/fonts/
|
||||||
|
src/public/js/libs/
|
||||||
|
|
|
@ -3,12 +3,14 @@ from flask import Flask, redirect
|
||||||
from routes.index import router as index
|
from routes.index import router as index
|
||||||
from routes.upload import router as upload
|
from routes.upload import router as upload
|
||||||
from utils.font import init as init_font
|
from utils.font import init as init_font
|
||||||
|
from utils.libjs import init as init_libjs
|
||||||
|
|
||||||
app = Flask(__name__, static_url_path="/", static_folder="public")
|
app = Flask(__name__, static_url_path="/", static_folder="public")
|
||||||
app.register_blueprint(index, url_prefix="/index")
|
app.register_blueprint(index, url_prefix="/index")
|
||||||
app.register_blueprint(upload, url_prefix="/upload")
|
app.register_blueprint(upload, url_prefix="/upload")
|
||||||
|
|
||||||
init_font("1.3.0")
|
init_font("1.3.0")
|
||||||
|
init_libjs("fc5e3c53e41490e24ca7f67cb24e7ab389b770f9")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
|
};
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
from config import Config
|
from flask import Blueprint, redirect, request
|
||||||
from flask import Blueprint, redirect, render_template, request
|
|
||||||
|
|
||||||
router = Blueprint("upload", __name__)
|
router = Blueprint("upload", __name__)
|
||||||
|
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from os import mkdir
|
||||||
|
|
||||||
from requests import get
|
from requests import get
|
||||||
|
|
||||||
from utils.misc import exist
|
from utils.misc import exist
|
||||||
|
|
29
src/utils/libjs.py
Normal file
29
src/utils/libjs.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from os import mkdir
|
||||||
|
|
||||||
|
from requests import get
|
||||||
|
|
||||||
|
from utils.misc import exist
|
||||||
|
|
||||||
|
|
||||||
|
def init(commit_hash):
|
||||||
|
""" Download JS libraries"""
|
||||||
|
path = "./src/public/js/libs"
|
||||||
|
filename = "bigint-mod.js"
|
||||||
|
# If there is filename changes, you should change
|
||||||
|
# the name in the js imports too (./src/public/js/rsa.js)
|
||||||
|
|
||||||
|
# Check if js/lib directory exists
|
||||||
|
if not exist(path):
|
||||||
|
mkdir(path)
|
||||||
|
|
||||||
|
# TODO: Store the version used and redownload on version changes
|
||||||
|
|
||||||
|
# Download the js file if needed
|
||||||
|
if not exist(f"{path}/{filename}"):
|
||||||
|
# Download the font file
|
||||||
|
file_url = f"https://github.com/juanelas/bigint-mod-arith/blob/{commit_hash}/dist/esm/index.browser.js"
|
||||||
|
data = get(file_url).content
|
||||||
|
|
||||||
|
# Save the file
|
||||||
|
with open(f"{path}/{filename}", "wb") as file:
|
||||||
|
file.write(data)
|
|
@ -1,6 +1,3 @@
|
||||||
from os import mkdir
|
|
||||||
|
|
||||||
|
|
||||||
def exist(path):
|
def exist(path):
|
||||||
"""Check if file or directory exists"""
|
"""Check if file or directory exists"""
|
||||||
try:
|
try:
|
||||||
|
|
Reference in a new issue