Javier_Fernandez

cifrado

Mar 30th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package Entregable;
  2.  
  3. import Entregable.exception.TooHighNumberException;
  4.  
  5. /**
  6.  *
  7.  * @author javie
  8.  */
  9. public class cifrado {
  10.  
  11.     public static char cifrar(char letra, int des, boolean cfr) throws TooHighNumberException {
  12.  
  13.         if (des > 26 || des < 0) {
  14.             TooHighNumberException e = new TooHighNumberException();
  15.             throw e;
  16.         }
  17.  
  18.         if (cfr) {
  19.             des = des * -1;
  20.         }
  21.  
  22.         boolean Smi = false;
  23.         boolean Sma = false;
  24.         int car;
  25.         int cif;
  26.         char result = 0;
  27.         String juegocaracteresMinuscula = "abcdefghijklmnopqrstuvwxyz";
  28.         String juegocaracteresMayuscula = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  29.  
  30.  
  31.         if (juegocaracteresMinuscula.indexOf(letra) > -1) {
  32.             Smi = true;
  33.         }
  34.  
  35.        
  36.         if (juegocaracteresMayuscula.indexOf(letra) > -1) {
  37.             Sma = true;
  38.         }
  39.  
  40.         if (Smi || Sma) {
  41.             if (Smi) {
  42.                 car = juegocaracteresMinuscula.indexOf(letra);
  43.             } else {
  44.                 car = juegocaracteresMayuscula.indexOf(letra);
  45.             }
  46.  
  47.             cif = car + des;
  48.             if (cif > 25) {
  49.                 cif = des - (26 - car);
  50.             } else if (cif < 0) {
  51.                 des = des * -1;
  52.                 cif = 26 - (des - car);
  53.             }
  54.             if (Smi) {
  55.                 result = juegocaracteresMinuscula.charAt(cif);
  56.             } else {
  57.                 result = juegocaracteresMayuscula.charAt(cif);
  58.             }
  59.         } else {
  60.             result = letra;
  61.         }
  62.  
  63.         return result;
  64.     }
  65.  
  66.     public static boolean letra(String letra) {
  67.         boolean ca = false;
  68.  
  69.         String juegocaracteres = "abcdefghijklmnopqrstuvwxyz";
  70.  
  71.         for (int i = 0; i < juegocaracteres.length(); i++) {
  72.             if (juegocaracteres.contains(letra)) {
  73.                 ca = true;
  74.             }
  75.         }
  76.         return ca;
  77.     }
  78.  
  79.     public static int desplazamiento(String letraE) {
  80.         int des;
  81.         String juegocaracteres = "abcdefghijklmnopqrstuvwxyz";
  82.         int posE = 4;
  83.         int posLetraE = juegocaracteres.indexOf(letraE);
  84.  
  85.         if (posLetraE < posE) {
  86.             des = posLetraE + 20;
  87.         } else {
  88.             des = posLetraE - posE;
  89.         }
  90.  
  91.         return des;
  92.  
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment