141 lines
5.7 KiB
Java
141 lines
5.7 KiB
Java
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();
|
|
}
|
|
}
|
|
|