Created
August 9, 2021 19:51
-
-
Save Roberto24p/3602549c0dcd977a6de8086d753d41c4 to your computer and use it in GitHub Desktop.
Cifrado Vigenere
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
using System; | |
namespace myFirstApp5._0 | |
{ | |
class Program | |
{ | |
private static int Abecedario = 97; | |
static void Main(string[] args) | |
{ | |
int opcion = Menu(); | |
Console.Write(" Ingresa la clave (SIN ESPACIOS)-> "); | |
string clave = Console.ReadLine(); | |
switch(opcion){ | |
case 1: | |
Console.Write(" Ingresa el texto a cifrar (SIN ESPACIOS): "); | |
string txtACifrar = Console.ReadLine(); | |
Cifrar(txtACifrar, clave); | |
break; | |
case 2: | |
Console.Write(" Ingresa el texto a descifrar (SIN ESPACIOS): "); | |
string txtCifrado = Console.ReadLine(); | |
Descifrar(txtCifrado, clave); | |
break; | |
} | |
Console.ReadKey(); | |
} | |
static void Cifrar(string txtACifrar, string clave){ | |
for(int i = 0, j = 0; i<txtACifrar.Length; i++, j++){ | |
if(j==clave.Length){ | |
j=0; | |
} | |
int newCodeCifrado = (txtACifrar[i] - Abecedario) + (clave[j] - Abecedario); | |
if(newCodeCifrado>=26){ | |
newCodeCifrado = newCodeCifrado%26; | |
} | |
Console.Write((char)(newCodeCifrado+Abecedario)+ " "); | |
} | |
} | |
static void Descifrar(string txt, string clave){ | |
for(int i = 0, j = 0; i<txt.Length; i++, j++){ | |
if(j==clave.Length){ | |
j = 0; | |
} | |
int codeDescifrado = (txt[i] - Abecedario) - (clave[j] - Abecedario); | |
if(codeDescifrado<0){ | |
codeDescifrado = codeDescifrado + 26; | |
} | |
Console.Write((char)(codeDescifrado+Abecedario) + " "); | |
} | |
Console.WriteLine(); | |
} | |
static int Menu(){ | |
string opcion; | |
Console.WriteLine("MENU DE CICRADO"); | |
Console.WriteLine(" 1-Cifrar"); | |
Console.WriteLine(" 2-Descrifrar"); | |
Console.Write(" Opcion->"); | |
opcion = Console.ReadLine(); | |
return int.Parse(opcion); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment