This commit is contained in:
@@ -1,140 +0,0 @@
|
|||||||
package com.crypto;
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
|
|
||||||
// ─────────────────────────────────────────────
|
|
||||||
// Menu
|
|
||||||
// ─────────────────────────────────────────────
|
|
||||||
private static int scegliDimensioneChiave(Scanner scanner) {
|
|
||||||
int bits = 0;
|
|
||||||
|
|
||||||
while (bits == 0) {
|
|
||||||
System.out.println("\nSeleziona la dimensione della chiave:");
|
|
||||||
System.out.println(" [1] 512 bit (Solo uso didattico)");
|
|
||||||
System.out.println(" [2] 1024 bit (Deprecata, solo test)");
|
|
||||||
System.out.println(" [3] 2048 bit (Standard attuale)");
|
|
||||||
System.out.println(" [4] 3072 bit (Ottimo compromesso)");
|
|
||||||
System.out.println(" [5] 4096 bit (Alta sicurezza)");
|
|
||||||
System.out.print("Scelta (1-5): ");
|
|
||||||
|
|
||||||
try {
|
|
||||||
int scelta = Integer.parseInt(scanner.nextLine().trim());
|
|
||||||
|
|
||||||
// Assegniamo i bit in base alla scelta
|
|
||||||
switch (scelta) {
|
|
||||||
case 1 -> bits = 512;
|
|
||||||
case 2 -> bits = 1024;
|
|
||||||
case 3 -> bits = 2048;
|
|
||||||
case 4 -> bits = 3072;
|
|
||||||
case 5 -> bits = 4096;
|
|
||||||
default -> System.out.println("❌ Scelta non valida. Inserisci un numero da 1 a 5.");
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
System.out.println("❌ Errore: Inserisci un numero valido.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Selezionata chiave da " + bits + " bit.");
|
|
||||||
return bits;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Main
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
|
|
||||||
System.out.println("==========================================");
|
|
||||||
System.out.println(" RSA CRYPTO ENGINE ");
|
|
||||||
System.out.println("==========================================");
|
|
||||||
|
|
||||||
// --- 1. SETUP DELLE CHIAVI ---
|
|
||||||
int dimChiave = scegliDimensioneChiave(scanner);
|
|
||||||
|
|
||||||
System.out.println("\n⏳ Generazione delle chiavi in corso...");
|
|
||||||
// Passiamo dimChiave / 2 come facevi prima per p e q
|
|
||||||
RSA.RSAkeys chiavi = RSA.defkeys(dimChiave / 2);
|
|
||||||
System.out.println("> Chiavi generate con successo!");
|
|
||||||
|
|
||||||
boolean continua = true;
|
|
||||||
|
|
||||||
// --- CICLO PRINCIPALE ---
|
|
||||||
while (continua) {
|
|
||||||
|
|
||||||
System.out.println("\n------------------------------------------");
|
|
||||||
System.out.println("Cosa vuoi fare?");
|
|
||||||
System.out.println(" [1] Cifra un numero");
|
|
||||||
System.out.println(" [2] Cifra una stringa");
|
|
||||||
System.out.println(" [3] Rigenera le chiavi");
|
|
||||||
System.out.println(" [0] Esci");
|
|
||||||
System.out.println("------------------------------------------");
|
|
||||||
System.out.print("Scelta: ");
|
|
||||||
|
|
||||||
int scelta = -1;
|
|
||||||
try {
|
|
||||||
scelta = Integer.parseInt(scanner.nextLine().trim());
|
|
||||||
} catch (NumberFormatException ignored) {
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
switch (scelta) {
|
|
||||||
|
|
||||||
case 1 -> {
|
|
||||||
// MODALITÀ NUMERO
|
|
||||||
System.out.print("Inserisci il numero da cifrare: ");
|
|
||||||
try {
|
|
||||||
BigInteger m = new BigInteger(scanner.nextLine().trim());
|
|
||||||
|
|
||||||
System.out.println("> Cifratura in corso...");
|
|
||||||
BigInteger c = RSA.encrypt(m, chiavi.e(), chiavi.n());
|
|
||||||
System.out.println("Cifrato:\n" + c);
|
|
||||||
|
|
||||||
System.out.println("\n> Decifratura in corso...");
|
|
||||||
BigInteger dec = RSA.decrypt(c, chiavi.d(), chiavi.n());
|
|
||||||
System.out.println("Decifrato:\n" + dec);
|
|
||||||
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
System.out.println("> Errore: Assicurati di inserire un numero valido.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case 2 -> {
|
|
||||||
// MODALITÀ STRINGA
|
|
||||||
System.out.print("Inserisci il testo da cifrare: ");
|
|
||||||
String input = scanner.nextLine();
|
|
||||||
|
|
||||||
System.out.println("> Cifratura in corso...");
|
|
||||||
String cifrato = RSA.encrypt(input, chiavi.e(), chiavi.n());
|
|
||||||
if (cifrato != null && !cifrato.isEmpty()) {
|
|
||||||
// 2. SOLO PER LA STAMPA: nascondiamo il binario convertendolo in Base64
|
|
||||||
String base64 = java.util.Base64.getEncoder().encodeToString(cifrato.getBytes());
|
|
||||||
System.out.println("Cifrato (Base64):\n" + base64);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("\n> Decifratura in corso...");
|
|
||||||
String dec = RSA.decrypt(cifrato, chiavi.d(), chiavi.n());
|
|
||||||
System.out.println("Decifrato:\n" + dec);
|
|
||||||
}
|
|
||||||
|
|
||||||
case 3 -> {
|
|
||||||
// RIGENERA CHIAVI
|
|
||||||
dimChiave = scegliDimensioneChiave(scanner);
|
|
||||||
System.out.println("\n> Generazione delle chiavi in corso...");
|
|
||||||
chiavi = RSA.defkeys(dimChiave / 2);
|
|
||||||
System.out.println("> Chiavi rigenerate con successo!");
|
|
||||||
}
|
|
||||||
|
|
||||||
case 0 -> {
|
|
||||||
continua = false;
|
|
||||||
System.out.println("Chiusura del motore RSA. Alla prossima!");
|
|
||||||
}
|
|
||||||
|
|
||||||
default -> System.out.println("> Scelta non valida, riprova.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
scanner.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ public class Seal {
|
|||||||
|
|
||||||
System.out.println("Generazione chiavi RSA per il server web in corso...");
|
System.out.println("Generazione chiavi RSA per il server web in corso...");
|
||||||
|
|
||||||
// Cifratura con Cifrario di Vigenere
|
// Cifratura con Cifrario di Vigenere (TODO)
|
||||||
app.post("/seal/vigenere/encrypt", ctx -> {
|
app.post("/seal/vigenere/encrypt", ctx -> {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,15 @@ public class Seal {
|
|||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Decifratura con Cifrario di Cesare (Attacco Bruteforce !!)
|
||||||
|
app.post("/seal/caesar/decrypt", ctx -> {
|
||||||
|
RichiestaCesare req = ctx.bodyAsClass(RichiestaCesare.class);
|
||||||
|
String[] ciphertext = Caesar.decode(req.ciphertext);
|
||||||
|
ctx.json(ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
// Cifratura con RSA
|
// Cifratura con RSA
|
||||||
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
|
||||||
@@ -60,8 +69,8 @@ public class Seal {
|
|||||||
class RichiestaCesare {
|
class RichiestaCesare {
|
||||||
public int key;
|
public int key;
|
||||||
public String plaintext;
|
public String plaintext;
|
||||||
|
public String ciphertext;
|
||||||
// Costruttore della richiesta
|
public RichiestaCesare() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RichiestaRSA {
|
class RichiestaRSA {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@
|
|||||||
<button class="btn-green" onclick="decrypt()">🔓 Decifra in chiaro</button>
|
<button class="btn-green" onclick="decrypt()">🔓 Decifra in chiaro</button>
|
||||||
|
|
||||||
<label style="display:block; margin-top: 25px;">Risultato decifrato:</label>
|
<label style="display:block; margin-top: 25px;">Risultato decifrato:</label>
|
||||||
<textarea id="testoDecifrato" readonly placeholder="Il messaggio originale tornerà qui..."></textarea>
|
<textarea id="decodedtext" readonly placeholder="Il messaggio originale tornerà qui..."></textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<footer>
|
<footer>
|
||||||
|
|||||||
@@ -16,16 +16,14 @@ async function encrypt() {
|
|||||||
|
|
||||||
async function decrypt() {
|
async function decrypt() {
|
||||||
const ciphertext = document.getElementById("ciphertext").value;
|
const ciphertext = document.getElementById("ciphertext").value;
|
||||||
const key = document.getElementById("key").value;
|
|
||||||
|
|
||||||
if (!ciphertext) return alert("Nessun testo da decifrare!");
|
if (!ciphertext) return alert("Nessun testo da decifrare!");
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/seal/caesar/decrypt", {
|
const res = await fetch("/seal/caesar/decrypt", {
|
||||||
method: "POST", headers: { "Content-Type": "application/json" },
|
method: "POST", headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ testo: ciphertext, chiave: parseInt(key) })
|
body: JSON.stringify({ ciphertext: ciphertext })
|
||||||
});
|
});
|
||||||
// TODO: SISTEMARE IL SISTEMA DI DECIFRATURA !!
|
document.getElementById("decodedtext").value = await res.text();
|
||||||
document.getElementById("testoDecifrato").value = await res.text();
|
|
||||||
} catch (e) { alert("Errore di connessione!"); }
|
} catch (e) { alert("Errore di connessione!"); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user