Skip to content

Instantly share code, notes, and snippets.

@Roberto24p
Created August 9, 2021 19:51
Show Gist options
  • Save Roberto24p/3602549c0dcd977a6de8086d753d41c4 to your computer and use it in GitHub Desktop.
Save Roberto24p/3602549c0dcd977a6de8086d753d41c4 to your computer and use it in GitHub Desktop.
Cifrado Vigenere
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