Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package unsch.efpis.metodo;
  2.  
  3. import unsch.efpis.cifrador.CifradorVigenere;
  4. import unsch.efpis.cifrador.DescifradorVigenere;
  5.  
  6. /**
  7.  *
  8.  * @author sin_querer@hotmail.com
  9.  */
  10. public class Vigenere {
  11.     private String textoCifrado = "";
  12.     private String textoClaro = "";
  13.     CifradorVigenere cifradoVigenere = new CifradorVigenere();
  14.     DescifradorVigenere descifradoVigenere = new DescifradorVigenere();
  15.  
  16.     public String encriptarTextoClaro(String textoClaro, String clave) {
  17.         String claveCompletada = "";
  18.         int indice = 0;
  19.         for (int i = indice; i < textoClaro.length(); i++) {
  20.             for (int j = 0; j < clave.length(); j++) {
  21.                 if (claveCompletada.length() < textoClaro.length()) {
  22.                     if (textoClaro.charAt(indice) != ' ') {
  23.                         claveCompletada += clave.charAt(j) + "";
  24.                     } else {
  25.                         claveCompletada += " ";
  26.                         j--;
  27.                     }
  28.                     indice++;
  29.                 }
  30.             }
  31.         }
  32.  
  33.         for (int i = 0; i < textoClaro.length(); i++) {
  34.             char charTextoClaro = textoClaro.charAt(i);
  35.             char charClaveCompletada = claveCompletada.charAt(i);
  36.             if (charTextoClaro != ' ') {
  37.                 textoCifrado += cifradoVigenere.getTextoCifrado(charTextoClaro, charClaveCompletada) + "";
  38.             } else {
  39.                 textoCifrado += " ";
  40.             }
  41.         }
  42.  
  43.         return textoCifrado;
  44.     }
  45.    
  46.     public String desencriptarTextoCifrado(String textoCifrado, String clave) {
  47.         String claveCompletada = "";
  48.         int indice = 0;
  49.         for (int i = indice; i < textoCifrado.length(); i++) {
  50.             for (int j = 0; j < clave.length(); j++) {
  51.                 if (claveCompletada.length() < textoCifrado.length()) {
  52.                     if (textoCifrado.charAt(indice) != ' ') {
  53.                         claveCompletada += clave.charAt(j) + "";
  54.                     } else {
  55.                         claveCompletada += " ";
  56.                         j--;
  57.                     }
  58.                     indice++;
  59.                 }
  60.             }
  61.         }
  62.  
  63.         for (int i = 0; i < textoCifrado.length(); i++) {
  64.             char charTextoCifrado = textoCifrado.charAt(i);
  65.             char charClaveCompletada = claveCompletada.charAt(i);
  66.             if (charTextoCifrado != ' ') {
  67.                 textoClaro += descifradoVigenere.getTextoDescifrado(charTextoCifrado, charClaveCompletada) + "";
  68.             } else {
  69.                 textoClaro += " ";
  70.             }
  71.         }
  72.  
  73.         return textoClaro;    
  74.     }
  75.  
  76. }