import javax.crypto.
Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
boolean repetir;
do {
System.out.println("\nSeleccione el tipo de cifrado:");
System.out.println("1 - Cifrado César");
System.out.println("2 - Cifrado DES");
System.out.println("3 - Cifrado RSA");
System.out.println("4 - Salir");
System.out.print("Opción: ");
int opcion = scanner.nextInt();
scanner.nextLine(); // Limpiar buffer
if (opcion == 4) {
System.out.println("Saliendo...");
break;
System.out.print("Ingrese el mensaje a cifrar: ");
String mensaje = scanner.nextLine();
switch (opcion) {
case 1:
System.out.print("Ingrese el desplazamiento para César: ");
int desplazamiento = scanner.nextInt();
scanner.nextLine(); // Limpiar buffer
String cesarCifrado = cifrarCesar(mensaje, desplazamiento);
System.out.println("Cifrado César: " + cesarCifrado);
System.out.println("Descifrado César: " + descifrarCesar(cesarCifrado,
desplazamiento));
break;
case 2:
SecretKey desClave = generarClaveDES();
String desCifrado = cifrarDES(mensaje, desClave);
System.out.println("Cifrado DES: " + desCifrado);
System.out.println("Descifrado DES: " + descifrarDES(desCifrado, desClave));
break;
case 3:
KeyPair clavesRSA = generarClavesRSA();
String rsaCifrado = cifrarRSA(mensaje, clavesRSA.getPublic());
System.out.println("Cifrado RSA: " + rsaCifrado);
System.out.println("Descifrado RSA: " + descifrarRSA(rsaCifrado,
clavesRSA.getPrivate()));
break;
default:
System.out.println("Opción no válida.");
System.out.print("\n¿Desea cifrar otro mensaje? (s/n): ");
String respuesta = scanner.nextLine().trim().toLowerCase();
repetir = respuesta.equals("s");
} while (repetir);
scanner.close();
System.out.println("Programa finalizado.");
// ========== Cifrado César ==========
public static String cifrarCesar(String texto, int desplazamiento) {
String alfabeto = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ";
texto = texto.toUpperCase();
StringBuilder cifrado = new StringBuilder();
for (char letra : texto.toCharArray()) {
int index = alfabeto.indexOf(letra);
if (index != -1) {
int nuevoIndex = (index + desplazamiento) % alfabeto.length();
cifrado.append(alfabeto.charAt(nuevoIndex));
} else {
cifrado.append(letra);
return cifrado.toString();
public static String descifrarCesar(String texto, int desplazamiento) {
return cifrarCesar(texto, -desplazamiento);
}
// ========== Cifrado DES ==========
public static SecretKey generarClaveDES() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
return keyGen.generateKey();
public static String cifrarDES(String mensaje, SecretKey clave) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, clave);
byte[] cifrado = cipher.doFinal(mensaje.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(cifrado);
public static String descifrarDES(String cifrado, SecretKey clave) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, clave);
byte[] decifrado = cipher.doFinal(Base64.getDecoder().decode(cifrado));
return new String(decifrado, StandardCharsets.UTF_8);
// ========== Cifrado RSA ==========
public static KeyPair generarClavesRSA() throws Exception {
KeyPairGenerator generador = KeyPairGenerator.getInstance("RSA");
generador.initialize(2048);
return generador.generateKeyPair();
public static String cifrarRSA(String mensaje, PublicKey clavePublica) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, clavePublica);
byte[] cifrado = cipher.doFinal(mensaje.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(cifrado);
public static String descifrarRSA(String cifrado, PrivateKey clavePrivada) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, clavePrivada);
byte[] decifrado = cipher.doFinal(Base64.getDecoder().decode(cifrado));
return new String(decifrado, StandardCharsets.UTF_8);