-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pablo Ramirez
committed
Jun 13, 2023
1 parent
2dc4642
commit 558a016
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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; | ||
} |