• This is default featured slide 1 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 2 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 3 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 4 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 5 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

Mostrando entradas con la etiqueta julio. Mostrar todas las entradas
Mostrando entradas con la etiqueta julio. Mostrar todas las entradas

Algoritmo Julio Cesar (Criptografia)

Este es un algoritmo sencillo consiste en alterar con un intervalo numérico menor a 10 la posición ascii tanto para encriptar o desencriptar.

Por ejemplo a en ascii es 97 pero con un intervalo de 1 se imprimiría un ascii 98 que es b
//esto formatea el codigo java
public class cesar {

private String tabla = "abcdefghijklmnopqrstuvwxyzáéíóú 1234567890@.,;:-+*/$#¿?!¡=()[]{}";

    public cesar(){
    }

    public String Encriptar(String t, int key){
        String texto = LimpiarCadena(t);
        //aqui se almacena el resultado
        String res = "";
        for(int i = 0; i < texto.length();i++)
        {
            //busca la posicion del caracter en la variable tabla
            int pos = tabla.indexOf(texto.charAt(i));
            //realiza el reemplazo
            if ((pos + key) < tabla.length()){
                res = res + tabla.charAt(pos+key);
            }
            else
            {
                res = res + tabla.charAt((pos+key) - tabla.length());
            }
        }
        return res;
    }

    public String Desencriptar(String t, int key){
        String texto = LimpiarCadena(t);
        String res = "";
        for(int i = 0; i < texto.length();i++)
        {
            int pos = tabla.indexOf(texto.charAt(i));
            if ((pos - key) < 0){
                res = res + tabla.charAt((pos-key) + tabla.length());
            }
            else
            {
                res = res + tabla.charAt(pos-key);
            }
        }
        return res;
    }

    private String LimpiarCadena(String t){
        //transforma el texto a minusculas
        t = t.toLowerCase();
        //eliminamos todos los retornos de carro
        t = t.replaceAll("\n", "");
        //eliminamos caracteres prohibidos
        for(int i = 0; i < t.length();i++)
        {
            int pos = tabla.indexOf(t.charAt(i));
            if (pos == -1){
                t = t.replace(t.charAt(i), ' ');
            }
        }
        return t;
    }
}
Ahora la forma de instanciarlo es asi:
cesar cesar = new cesar();
String cadena_final = cesar.Encriptar(texto_inicial, intervalo);
String cadena_inicial= cesar.Desencriptar(cadena_final, intervalo);
Share:

Visitantes

Flag Counter

Popular Posts

Etiquetas

Recent Posts