Facebook
From José Galea 1ºB DAM, 4 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 121
  1. public class Cifrado_Cesar {
  2.  
  3.     public static void main(String[] args) {
  4.         Scanner sc = new Scanner(System.in);
  5.         String nombreFichero = "";
  6.         int desplazamiento = 0;
  7.         FileWriter fw = null;
  8.         String textoCifrado;
  9.  
  10.         if (args.length >= 1) {
  11.             nombreFichero = args[0];
  12.             desplazamiento = Integer.parseInt(args[1]);
  13.         } else {
  14.             nombreFichero = "./Prueba1.txt";
  15.             desplazamiento = 2;
  16.         }
  17.         if (desplazamiento > 26 || desplazamiento < -26) {
  18.             System.out.println("Error ---> El desplazamiento no puede ser > 26 ni < -26");
  19.         } else {
  20.             textoCifrado = cifrarFichero(nombreFichero, desplazamiento);
  21.  
  22.             //File f2 = new File("./cifrado_Cesar.cfr");
  23.             File f2 = new File(nombreFichero + ".cfr");
  24.             try {
  25.                 fw = new FileWriter(f2);
  26.                 fw.write(textoCifrado);
  27.  
  28.             } catch (IOException ex) {
  29.                 System.out.println("ERROR ---> " + ex.getMessage());
  30.             } finally {
  31.                 if (fw != null) {
  32.                     try {
  33.                         fw.close();
  34.                     } catch (Exception e) {
  35.                         System.out.println("Error al cerrar");
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             System.out.println("¡Cifrado completado!");
  41.             //System.out.println("Te hemos creado un archivo llamado cifrado_Cesar.cfr en el que tienes tu texto cifrado.");
  42.             System.out.println("Te hemos creado un archivo llamado " + nombreFichero + ".cfr" + " en el que tienes tu texto cifrado.");
  43.             System.out.println("Gracias por trabajar con nosotros. Que la fuerza te acompañe.");
  44.             System.out.println();
  45.            // System.out.println("Desplazamiento: " + desplazamiento);
  46.         }
  47.  
  48.     }
  49. }