Advertisement
Javier_Fernandez

DescifradoCesarSinDesplazamiento

Mar 30th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package Entregable;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.util.HashMap;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  *
  10.  * @author javie
  11.  */
  12. public class DescifradoSinDesplazamiento {
  13.  
  14.     public static void main(String[] args) {
  15.         HashMap<String, Integer> letras = new HashMap<String, Integer>();
  16.         FileWriter fw = null;
  17.         Scanner scFile = null;
  18.         String nombreFichero = args[0];
  19.         int des;
  20.         String[] linea;
  21.         char[] fila;
  22.         String texto = "";
  23.         String LetraE = "a";
  24.         int CantidadLetraE = 0;
  25.  
  26.         File f = new File(nombreFichero);
  27.         File fe = new File("./" + nombreFichero + ".uncfr");
  28.  
  29.         if (!f.exists()) {
  30.             System.out.println("El fichero no existe");
  31.         } else {
  32.             try {
  33.  
  34.                 scFile = new Scanner(f);
  35.                 fw = new FileWriter(fe);
  36.                 while (scFile.hasNextLine()) {
  37.                     linea = scFile.nextLine().split("");
  38.  
  39.                     for (String letra : linea) {
  40.                         letra = letra.toLowerCase();
  41.                         if (cifrado.letra(letra)) {
  42.                             if (letras.containsKey(letra)) {
  43.                                 letras.put(letra, letras.get(letra) + 1);
  44.                             } else {
  45.                                 letras.put(letra, 0);
  46.                             }
  47.                         }
  48.                     }
  49.                 }
  50.                 for (String c : letras.keySet()) {
  51.                     if (letras.get(c) > CantidadLetraE) {
  52.                         CantidadLetraE = letras.get(c);
  53.                         LetraE = c;
  54.  
  55.                     }
  56.  
  57.                 }
  58.                 scFile = new Scanner(f);
  59.                 while (scFile.hasNextLine()) {
  60.                     fila = scFile.nextLine().toCharArray();
  61.                     for (char letra : fila) {
  62.                         texto = texto + cifrado.cifrar(letra, cifrado.desplazamiento(LetraE), true);
  63.                     }
  64.                     fw.write(texto + "\n");
  65.                     texto = "";
  66.                 }
  67.                 System.out.println("Fichero descifrado");
  68.  
  69.             } catch (Exception e) {
  70.                 System.out.println("Se ha producido un error");
  71.             } finally {
  72.                 if (scFile != null) {
  73.                     scFile.close();
  74.                 }
  75.                 if (fw != null) {
  76.                     try {
  77.                         fw.close();
  78.                     } catch (Exception e) {
  79.                         System.out.println("Error al cerrar");
  80.                     }
  81.                 }
  82.             }
  83.  
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement