MÉTODO DE CIFRADO CÉSAR | explicación + código en java
Вставка
- Опубліковано 29 лис 2021
- UNIVERSIDAD PUBLICA DE EL ALTO
Ingeniería de Sistemas
Sistemas Operativos
Nelson Mamani Ramos
-------------------------------------------------------------------------------------
mensaje oculto: (no incluir la ñ en el alfabeto)
YZ ZWGTOPD OPULC EF WTVP J NZXALCETC PDEP GTOPZ
código de java en el primer comentario
Si te ha ayudado por favor dale like :)
gracias por ver!
package cifradocesar;
import java.util.Scanner;
public class CifradoCesar {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
sn.useDelimiter("
");
String abecedario = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("********** CIFRADO CESAR por NELSON MAMANI RAMOS **********");
System.out.println("USTED DESEA:
1=codificar
2=descodificar
3=descodificar con todas las combinaciones posibles");
int opcion = sn.nextInt();
switch (opcion){
case 1 -> {
System.out.print("Ingrese el texto a codificar: ");
String texto = sn.next();
System.out.print("Ingresar el numero de recorridos: ");
int k = sn.nextInt();
String textoCodificado = codificar(abecedario, texto, k);
System.out.println("Texto codificado: " + textoCodificado);
}
case 2 -> {
System.out.print("Ingrese el texto a descodificar: ");
String texto = sn.next();
System.out.print("Ingresar el numero de recorridos: ");
int k = sn.nextInt();
String textoDescodificado = descodificar(abecedario, texto, k);
System.out.println("Texto descodificado: " + textoDescodificado);
}
case 3 -> {
System.out.print("Ingrese el texto a descodificar en todas sus combinaciones: ");
String texto = sn.next();
combinaciones(abecedario, texto);
}
default -> System.out.println("Ingrese una opcion correcta!");
}
}
//funcion para codificar
public static String codificar(String abecedario, String texto, int k){
String textoCodificado = "";
texto = texto.toUpperCase();
char caracter;
for (int i = 0; i < texto.length(); i++) {
caracter = texto.charAt(i);
int pos = abecedario.indexOf(caracter);
if(pos == -1){
textoCodificado += caracter;
}else{
textoCodificado += abecedario.charAt( (pos + k) % abecedario.length() );
}
}
return textoCodificado;
}
//funcion para descodificar
public static String descodificar(String abecedario, String texto, int k){
String textoDescodificado = "";
texto = texto.toUpperCase();
char caracter;
for (int i = 0; i < texto.length(); i++) {
caracter = texto.charAt(i);
int pos = abecedario.indexOf(caracter);
if(pos == -1){
textoDescodificado += caracter;
}else{
if(pos - k < 0){
textoDescodificado += abecedario.charAt(abecedario.length() + (pos - k) );
}else{
textoDescodificado += abecedario.charAt( (pos - k) % abecedario.length() );
}
}
}
return textoDescodificado;
}
//procedimiento para todas las combinaciones posibles
public static void combinaciones(String abecedario, String texto){
String textoDescodificado = "";
texto = texto.toUpperCase();
char caracter;
for (int k = 1; k
No podrías pasar el código ya en java? 🥺