Cifrado de Cesar app - By Zentraedi - Xamarin Android C#

Zentraedi Seguir

Dseda
Programador
Verificación en dos pasos activada
Desde
5 Nov 2014
Mensajes
1.066
Hola Betas, sigo practicando con las aplicaciones android y acá les traigo un nuevo programa quizás viejo pero al mismo tiempo interesante para quienes estamos aprendiendo día a día a programar y porque no en android.
Esta app consiste en el famoso cifrado de César con el cual enviaba mensajes a sus comandantes para dirigir su ejercito y no correr el riesgo que sus enemigos puedan leer este mensaje, si bien ya es un poco viejo y existen mejores formas de cifrado, se me ocurrió implementarlo en android de igual manera en forma de practica y exponerlo para aquellos que quieran aventurarse en la programación para teléfonos móviles.
El programa consiste en leer letra por letra y desplazarla según la clave. EJ : si Clave= 2..... A=C, B=D, C=D, etc.
El mensaje cifrado puede enviarse por E-mail utilizando nuestra cuenta configurada en el telefono.

Capturas de la app:



15eteo4.jpg


2vsewbt.jpg


4syzj8.jpg

Código para Xamarin en C#:
Insertar CODE, HTML o PHP:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace CifradoCesar
{
    [Activity(Label = "CifradoCesar", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        static string abc = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ_-+,#$%/()¿?¡!|,.;:{}[]";
        EditText Clave, Mensaje;
        ImageButton Cifrar, Borrar, Descifrar, Enviar;
        TextView Msj;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            Clave = FindViewById<EditText>(Resource.Id.editText1);
            Mensaje = FindViewById<EditText>(Resource.Id.editText2);
            Cifrar = FindViewById<ImageButton>(Resource.Id.imageButton1);
            Borrar = FindViewById<ImageButton>(Resource.Id.imageButton2);
            Descifrar = FindViewById<ImageButton>(Resource.Id.imageButton3);
            Msj = FindViewById<TextView>(Resource.Id.textView2);
            Enviar = FindViewById<ImageButton>(Resource.Id.Enviar);

            Clave.Text = "0";

          
            Cifrar.Click += delegate
            {
                if (Clave.Text.Equals(""))
                {
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    AlertDialog error = alert.Create();
                    error.SetTitle("Error");
                    error.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                    error.SetMessage("Debes colocar la clave para Cifrar");
                    error.SetButton("OK", (w, a) => { });
                    error.Show();
                }
                else  { 
                    int clave = Convert.ToInt32(Clave.Text);

                    if (clave > 76 || clave == 0)
                    {
                        AlertDialog.Builder alert = new AlertDialog.Builder(this);
                        AlertDialog error = alert.Create();
                        error.SetTitle("Error");
                        error.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                        error.SetMessage("La clave no es un numero valido, la clave debe ser un numero de 1 a 76");
                        error.SetButton("OK", (w, a) => { });
                        error.Show();
                    }
                
                else if (Mensaje.Text == "")
                {
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    AlertDialog error = alert.Create();
                    error.SetTitle("Error");
                    error.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                    error.SetMessage("No hay mensaje para cifrar, escribe uno");
                    error.SetButton("OK", (w, a) => { });
                    error.Show();
                }
                else
                {
                    
                    string MsjCifrado = cifrar(Mensaje.Text, clave);
                    Mensaje.Text = MsjCifrado;
                    Msj.Text = "Mensaje Cifrado:";
                }
                }
            };

            Descifrar.Click += delegate
            {
                if (Clave.Text.Equals(""))
                {
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    AlertDialog error = alert.Create();
                    error.SetTitle("Error");
                    error.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                    error.SetMessage("Debes colocar la clave para Descifrado");
                    error.SetButton("OK", (w, a) => { });
                    error.Show();
                }
                else
                {
                    int clave = Convert.ToInt32(Clave.Text);
                    if (clave > 76 || clave == 0)
                    {
                        AlertDialog.Builder alert = new AlertDialog.Builder(this);
                        AlertDialog error = alert.Create();
                        error.SetTitle("Error");
                        error.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                        error.SetMessage("La clave no es un numero valido, la clave debe ser un numero de 1 a 76");
                        error.SetButton("OK", (w, a) => { });
                        error.Show();
                    }
                    else if (Mensaje.Text == "")
                    {
                        AlertDialog.Builder alert = new AlertDialog.Builder(this);
                        AlertDialog error = alert.Create();
                        error.SetTitle("Error");
                        error.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                        error.SetMessage("No hay mensaje para descifrar");
                        error.SetButton("OK", (w, a) => { });
                        error.Show();
                    }
                    else
                    {
                        string MsjeDescifrado = descifrar(Mensaje.Text, clave);
                        Mensaje.Text = MsjeDescifrado;
                        Msj.Text = "Mensaje Descifrado:";
                    }
                }
            };

            Enviar.Click += (s, e) =>
            {
                if (Mensaje.Text == "")
                {
                    AlertDialog.Builder alert = new AlertDialog.Builder(this);
                    AlertDialog error = alert.Create();
                    error.SetTitle("Error");
                    error.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                    error.SetMessage("No hay ningun mensaje para enviar");
                    error.SetButton("OK", (w, a) => { });
                    error.Show();
                }
                else
                {
                    Intent email = new Intent(Intent.ActionSend);
                    email.PutExtra(Intent.ExtraText, Mensaje.Text.ToString());
                    email.SetType("message/rfc822");
                    StartActivity(Intent.CreateChooser(email, "Enviar por medio de:"));
                }
            };



            Borrar.Click += delegate
            {
                Clave.Text = "0";
                Mensaje.Text = "";
                Msj.Text = "Mensaje:";
            };
        }
        static string cifrar(string mensaje, int clave)
        {
            String cifrado = "";
            if (clave > 0 && clave < abc.Length)
            {
                for (int i = 0; i < mensaje.Length; i++)
                {
                    int posCaracter = getPosABC(mensaje[i]);

                    if (posCaracter != -1)
                    {
                        int pos = posCaracter + clave;
                        while (pos >= abc.Length)
                        {
                            pos = pos - abc.Length;
                        }
                        cifrado += abc[pos];
                    }
                    else
                    {
                        cifrado += mensaje[i];
                    }
                }
            }
            return cifrado;
        }
        static string descifrar(string mensaje, int clave)
        {
            String cifrado = "";
            if (clave > 0 && clave < abc.Length)
            {
                for (int i = 0; i < mensaje.Length; i++)
                {
                    int posCaracter = getPosABC(mensaje[i]);
                    
                    if (posCaracter != -1)
                    {
                        int pos = posCaracter - clave;
                        while (pos < 0)
                        {
                            pos = pos + abc.Length;
                        }
                        cifrado += abc[pos];
                    }
                    else
                    {
                        cifrado += mensaje[i];
                    }
                }
            }
            return cifrado;
        }
        static int getPosABC(char caracter)
        {
            for (int i = 0; i < abc.Length; i++)
            {
                if (caracter == abc[i])
                {
                    return i;
                }
            }

            return -1;
        }


    }
}

Link de descarga: CifradoCesarV2.0-ByZentraedi


Espero que sea de su agrado y que si tienen tiempo la prueben y me comenten si les agrado o no o alguna recomendacion para seguir progresando, acepto todo tipo de criticas al respecto. Saludos:encouragement:
 

¡Regístrate y comienza a ganar!

Beneficios

  • Gana dinero por participar
  • Gana dinero por recomendarnos
  • Descubre ofertas de empleo diariamente
  • Negocios seguros
  • ¡Información premium y más!

Acceder

¿Ya tienes una cuenta? Accede aquí

Arriba