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