From 9ee4b94040380d35caf25a43b82574d63b50777f Mon Sep 17 00:00:00 2001 From: elisabetta Date: Sat, 28 Mar 2026 12:42:02 +0100 Subject: [PATCH] hey hey hey --- backend/src/main/java/com/crypto/Seal.java | 20 ++++++++++++-------- backend/src/main/resources/get/rsa.js | 10 ++++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/com/crypto/Seal.java b/backend/src/main/java/com/crypto/Seal.java index 419ad81..1d9d128 100644 --- a/backend/src/main/java/com/crypto/Seal.java +++ b/backend/src/main/java/com/crypto/Seal.java @@ -1,10 +1,9 @@ package com.crypto; +import java.math.BigInteger; + import io.javalin.Javalin; public class Seal { - - // Il mazzo di chiavi !! - public static RSA.RSAkeys keys; public static void main(String[] args) { // Avvio del server web @@ -33,21 +32,24 @@ public class Seal { app.post("/seal/rsa/encrypt", ctx -> { // Legge i dati nel body dalla richiesta Richiesta req = ctx.bodyAsClass(Richiesta.class); - keys = RSA.defkeys(req.keysize / 2); + RSA.RSAkeys keys = RSA.defkeys(req.keysize / 2); String ciphertext = RSA.encrypt(req.text,keys.e(), keys.n()); - ctx.result(ciphertext); + ctx.result("{\"ciphertext\":"+ciphertext+",\"keys\":{\"d\":" + keys.d().toString() + ",\"n\":" + keys.n().toString() + "}}"); }); // Decifratura con RSA app.post("/seal/rsa/decrypt", ctx -> { // Legge i dati nel body dalla richiesta Richiesta req = ctx.bodyAsClass(Richiesta.class); - + + BigInteger d = new BigInteger(req.d); + BigInteger n = new BigInteger(req.n); + // Usa il metodo RSA per cifrare - String plaintext = RSA.decrypt(req.text, keys.d(), keys.n()); + String plaintext = RSA.decrypt(req.text, d, n); // Restituisce il risultato della cifratura - ctx.result(plaintext.replaceAll("\\\\u0000", "")); + ctx.result(plaintext.replaceAll("\\\\u0000/g", "")); }); } } @@ -56,6 +58,8 @@ public class Seal { class Richiesta { public int keysize; public String text; + public String d; + public String n; // Costruttore della richiesta public Richiesta() {} diff --git a/backend/src/main/resources/get/rsa.js b/backend/src/main/resources/get/rsa.js index 283b758..ebb692e 100644 --- a/backend/src/main/resources/get/rsa.js +++ b/backend/src/main/resources/get/rsa.js @@ -1,3 +1,6 @@ +let d = ""; +let n = ""; + async function encrypt() { const plaintext = document.getElementById("plaintext").value; const size = document.getElementById("keysize").value; @@ -11,7 +14,10 @@ async function encrypt() { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text: plaintext, keysize: parseInt(size) }) }); - document.getElementById("ciphertext").value = await res.text(); + const answer = await res.json(); + document.getElementById("ciphertext").value = answer.ciphertext; + d = answer.d; + n = answer.n; } catch (e) { alert("Impossibile connettersi al server Javalin."); } } @@ -24,7 +30,7 @@ async function decrypt() { try { const res = await fetch("/seal/rsa/decrypt", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ text: input }) + body: JSON.stringify({ text: input, n: n, d: d }) }); document.getElementById("decodedtext").value = await res.text(); } catch (e) { alert("Impossibile connettersi al server."); }