This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
package com.crypto;
|
package com.crypto;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
import io.javalin.Javalin;
|
import io.javalin.Javalin;
|
||||||
|
|
||||||
public class Seal {
|
public class Seal {
|
||||||
|
|
||||||
// Il mazzo di chiavi !!
|
|
||||||
public static RSA.RSAkeys keys;
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// Avvio del server web
|
// Avvio del server web
|
||||||
@@ -33,21 +32,24 @@ public class Seal {
|
|||||||
app.post("/seal/rsa/encrypt", ctx -> {
|
app.post("/seal/rsa/encrypt", ctx -> {
|
||||||
// Legge i dati nel body dalla richiesta
|
// Legge i dati nel body dalla richiesta
|
||||||
Richiesta req = ctx.bodyAsClass(Richiesta.class);
|
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());
|
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
|
// Decifratura con RSA
|
||||||
app.post("/seal/rsa/decrypt", ctx -> {
|
app.post("/seal/rsa/decrypt", ctx -> {
|
||||||
// Legge i dati nel body dalla richiesta
|
// Legge i dati nel body dalla richiesta
|
||||||
Richiesta req = ctx.bodyAsClass(Richiesta.class);
|
Richiesta req = ctx.bodyAsClass(Richiesta.class);
|
||||||
|
|
||||||
|
BigInteger d = new BigInteger(req.d);
|
||||||
|
BigInteger n = new BigInteger(req.n);
|
||||||
|
|
||||||
// Usa il metodo RSA per cifrare
|
// 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
|
// 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 {
|
class Richiesta {
|
||||||
public int keysize;
|
public int keysize;
|
||||||
public String text;
|
public String text;
|
||||||
|
public String d;
|
||||||
|
public String n;
|
||||||
|
|
||||||
// Costruttore della richiesta
|
// Costruttore della richiesta
|
||||||
public Richiesta() {}
|
public Richiesta() {}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
let d = "";
|
||||||
|
let n = "";
|
||||||
|
|
||||||
async function encrypt() {
|
async function encrypt() {
|
||||||
const plaintext = document.getElementById("plaintext").value;
|
const plaintext = document.getElementById("plaintext").value;
|
||||||
const size = document.getElementById("keysize").value;
|
const size = document.getElementById("keysize").value;
|
||||||
@@ -11,7 +14,10 @@ async function encrypt() {
|
|||||||
method: "POST", headers: { "Content-Type": "application/json" },
|
method: "POST", headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ text: plaintext, keysize: parseInt(size) })
|
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."); }
|
} catch (e) { alert("Impossibile connettersi al server Javalin."); }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,7 +30,7 @@ async function decrypt() {
|
|||||||
try {
|
try {
|
||||||
const res = await fetch("/seal/rsa/decrypt", {
|
const res = await fetch("/seal/rsa/decrypt", {
|
||||||
method: "POST", headers: { "Content-Type": "application/json" },
|
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();
|
document.getElementById("decodedtext").value = await res.text();
|
||||||
} catch (e) { alert("Impossibile connettersi al server."); }
|
} catch (e) { alert("Impossibile connettersi al server."); }
|
||||||
|
|||||||
Reference in New Issue
Block a user