Advertisement
Guest User

descifradosinclave

a guest
Mar 31st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. /**
  5. *
  6. * @author Oscar
  7. */
  8. public class DescifradoSinDesplazamiento {
  9.  
  10. public static void main(String[] args) {
  11. int fin = 0;
  12. char[] x;
  13. int[] veces = new int[26];
  14. Scanner scFile = null;
  15. int numero = 0;
  16. File f = null;
  17. File f1 = null;
  18. try {
  19. f = new File(args[0]);
  20. f1 = new File(f.getName() + ".uncfr");
  21. scFile = new Scanner(f);
  22. while (scFile.hasNext()) {
  23. x = scFile.next().toCharArray();
  24. for (char ch : x) {
  25. if (Character.isLetter(ch) && Character.getNumericValue(ch) > 0) {
  26. veces[(Character.getNumericValue(ch) - 10)]++;
  27. }
  28. }
  29. }
  30. for (int i = 0; i < veces.length; i++) {
  31. if (veces[i] > fin) {
  32. fin = veces[i];
  33. numero = i;
  34. }
  35. }
  36. } catch (Exception e) {
  37. System.out.println("Ha ocurrido un error");
  38. } finally {
  39. if (scFile != null) {
  40. scFile.close();
  41. }
  42. }
  43. scFile = null;
  44. FileWriter fw = null;
  45. String salida;
  46. int distancia;
  47. String[] linea;
  48. try {
  49. scFile = new Scanner(f);
  50. fw = new FileWriter(f1);
  51. distancia = numero - 4;
  52. while (scFile.hasNextLine()) {
  53. linea = scFile.nextLine().split(" ");
  54. for (String pal : linea) {
  55. salida = Cifrar.cifrarDocumento(distancia * -1, pal);
  56. fw.write(salida + " ");
  57. }
  58. fw.write("\n");
  59. }
  60. System.out.println("el archivo se ha descifrado correctamente");
  61. } catch (Exception e) {
  62. System.out.println("Ha ocurrido un error");
  63. } finally {
  64. if (fw != null) {
  65. try {
  66. fw.close();
  67. } catch (Exception e) {
  68. System.out.println("ha ocurrido un error");
  69. }
  70. }
  71. if (scFile != null) {
  72. scFile.close();
  73. }
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement