Skip to content

Commit

Permalink
Merge pull request #3818 from borazuwarah/reto24
Browse files Browse the repository at this point in the history
Reto #24 - c#
  • Loading branch information
Roswell468 authored Jun 13, 2023
2 parents 152b199 + 5c3bb61 commit b0b09db
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Retos/Reto #24 - CIFRADO CÉSAR [Fácil]/c#/borazuwarah.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
string texto = "MOUREDEV - PRIMO DE LAURA MOURE (LA RULETA DE LA SUERTE)";
int desplazamiento = 3;
string textoCifrado = CifrarCesar(texto, desplazamiento);

Console.WriteLine("Texto original: " + texto);
Console.WriteLine("Texto cifrado: " + textoCifrado);


Console.WriteLine("***************");
Console.WriteLine("Descrifrando");
Console.WriteLine("***************");
string Descifrado = DescifrarCesar(textoCifrado, desplazamiento);
Console.WriteLine("Texto Descifrado: " + Descifrado);
Console.ReadKey();

static string CifrarCesar(string texto, int desplazamiento)
{
string textoCifrado = "";

foreach (char c in texto)
{
if (char.IsLetter(c))
{
char cifrado = (char)(((c - 'A') + desplazamiento) % 26 + 'A');
textoCifrado += cifrado;
}
else
{
textoCifrado += c;
}
}
return textoCifrado;
}

static string DescifrarCesar(string textoCifrado, int desplazamiento)
{
string textoDescifrado = "";

foreach (char c in textoCifrado)
{
if (char.IsLetter(c))
{
char descifrado = (char)(((c - 'A') - desplazamiento + 26) % 26 + 'A');
textoDescifrado += descifrado;
}
else
{
textoDescifrado += c;
}
}

return textoDescifrado;
}

0 comments on commit b0b09db

Please sign in to comment.