EJERCICIO PRÁCTICO ENCRIPTACIÓN
package juan.d.landinez.ejercicioencriptar;
import java.util.Scanner;
public class EjercicioEncriptar {
public static void main(String[] args) {
Scanner sn = new Scanner (System.in);
sn.useDelimiter("\n");
String letras="AbCdEfGhIjKlMnOpQrStUvWxYz";
System.out.println("INGRESE LA PALABRA QUE DESEA CODIFICAR");
String frase = sn.next();
String texto = codificar(letras, frase);
System.out.println("Mensaje Cifrado: " + texto);
texto = descodificar(letras, texto);
System.out.println("Texto Descifrado: " + texto);
}
public static String codificar(String letras, String texto){
String textoCodificado="";
texto = texto.toUpperCase();
char caracter;
for (int i = 0; i<texto.length(); i++){
caracter=texto.charAt(i);
int pos = letras.indexOf(caracter);
if(pos== -1){
textoCodificado += caracter;
}else{
textoCodificado+= letras.charAt((pos + 4) % letras.length());
}
}
return textoCodificado;
}
public static String descodificar(String letras, String texto){
String textoDescodificado = "";
texto=texto.toUpperCase();
char caracter;
for (int i = 0; i<texto.length(); i++) {
caracter = texto.charAt (i);
int pos = letras.indexOf(caracter);
if (pos== -1){
textoDescodificado += caracter;
}else {
if(pos - 3 < 0){
textoDescodificado += letras.charAt(letras.length() + (pos - 4));
}else {
textoDescodificado+= letras.charAt((pos - 4)% letras.length ());
}
}
}
return textoDescodificado;
}
}
.jpg)
Comentarios
Publicar un comentario