Bueno hace poco en un
viernes negro, pusieron un reto de cifrado por desplazamiento, este ejercicio creo que no es tan complicado, así que tuve un tiempo libre en el trabajo y me puse a realizar el ejercicio, puede que no sea el código mas optimo o el deber ser de la programación, pero creo que es funcional.
@Lechugo fue quien explico este reto, para saber mas sobre el tema pueden ir a dicho post y verificar el
reto1.
El código es el siguiente:
//@author rush
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CifradoCesar
{
public partial class Form1 : Form
{
public char[] letras = new char[] { 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'ñ',
'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x',
'y', 'z'};
public int value;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox3.Text))
{
MessageBox.Show("Desplazamiento obligatorio");
}
else if(int.Parse(textBox3.Text) > 10)
{
MessageBox.Show("El desplazamiento es entre 1 y 10");
}
else
{
textBox1.Text = "";
string text = textBox2.Text;
textBox1.Text = Cifrar(text);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox3.Text))
{
MessageBox.Show("Desplazamiento obligatorio");
}
else if (int.Parse(textBox3.Text) > 25)
{
MessageBox.Show("El desplazamiento es entre 1 y 10");
}
else
{
textBox1.Text = "";
string text = textBox2.Text;
textBox1.Text = Descifrar(text);
}
}
public string Cifrar(string text)
{
value = int.Parse(textBox3.Text);
char[] text1 = text.ToCharArray();
string[] cifrado = new string[text.Length];
for (int i = 0; i < text1.Length; i++)
{
for (int j = 0; j < letras.Length; j++)
{
if (string.Equals(text1[i], letras[j]))
{
if (j == 26)
{
cifrado[i] = letras[-1 + value].ToString();
}
else
{
if ((j + value) > 26)
{
int aux = j + value - 27;
cifrado[i] = letras[aux].ToString();
}
else
{
cifrado[i] = letras[j + value].ToString();
}
}
}
else if (string.Equals(' ', text1[i]))
{
cifrado[i] = text1[i].ToString();
}
}
}
return String.Join("", cifrado);
}
public string Descifrar(string text)
{
value = int.Parse(textBox3.Text);
char[] text1 = text.ToCharArray();
string[] cifrado = new string[text.Length];
for (int i = 0; i < text1.Length; i++)
{
for (int j = 0; j < letras.Length; j++)
{
if (string.Equals(text1[i], letras[j]))
{
if (j == 0)
{
cifrado[i] = letras[27 - value].ToString();
}
else
{
if ((j - value) < 0)
{
int aux = j - value + 27;
cifrado[i] = letras[aux].ToString();
}
else
{
cifrado[i] = letras[j - value].ToString();
}
}
}
else if (string.Equals(' ', text1[i]))
{
cifrado[i] = text1[i].ToString();
}
}
}
return String.Join("", cifrado);
}
}
}
@Lechugo, dice que la rotación que utilizo fue 22, pero yo encontré que con una rotación de 5 se logra descifrar, ya que para descifrar se hace de manera contraria o inversa.
Acá el diseño:

Espero les sirva.
Si encuentran algún error, se los agradecería

Saludos!!!