This commit is contained in:
@@ -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,9 +32,9 @@ 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
|
||||
@@ -43,11 +42,14 @@ public class Seal {
|
||||
// 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() {}
|
||||
|
||||
@@ -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."); }
|
||||
|
||||
Reference in New Issue
Block a user