Cipher of vigenere

4
 private static void algoritmo(String input, String chave, Boolean b)
    {
        Console.WriteLine("Digite a mensagem: ");
        input = Console.ReadLine();
        Console.WriteLine("Digite a chave: ");
        chave = Console.ReadLine();
        input = input.ToUpper();
        chave = chave.ToUpper();


        if (b)
        {
            // para cada caracter da entrada, com exceção do espaço, converterá para valor ascii
            for (int contaCar = 0; contaCar < input.Length; contaCar++)
            {
                if (input.Equals(" "))
                {
                    output += input;
                }
                else
                {
                    char carTex = (char)input[contaCar];
                    output += carTex;
                }
            }
            Console.WriteLine(output);
            // para cada caracter da chave, com exceção do espaço, converterá para valor ascii
            for (int contaCar = 0; contaCar < chave.Length; contaCar++)
            {
                if (chave.Equals(" "))
                {
                    codeKey += chave;
                }
                else
                {
                    char asciiChave = (char)chave[contaCar];
                    codeKey += asciiChave;
                }
            }
            Console.WriteLine(codeKey);
            // agora que a mensagem está transformada em ascii(output) assim como a chave (codeKey),
            // deverá ocorrer a codificação
            for (int i = 0; i < output.Length; i++)
            {
                for (int j = 0; j < codeKey.Length; j++)
                {
                    code = ((codeKey[j] + output[i]) - 65) % 26;
                }
            }
            Console.Write(code);
            Console.ReadKey();
        }

The goal would be to have each key character receive its ASCII value and return it as an array so I can do some sum operations between each ASCII value of each character. For example: ASCII values of the word "nome": [65, 67, ...] and ASCII values of the key "cade": [65, 67, ...] , so I would sum the values of the same index of array .

How can I make the output and the codeKey exit with the ASCII values of the input?

    
asked by anonymous 19.02.2017 / 01:35

2 answers

3

It took me a while to figure out what the code should do. Now I enjoyed the joke and did what should have been done.

I separated the interface part of the algorithm, gave better names for everything.

I mentioned in the previous question that for the algorithm I did not need ToUpper() , I used it only in the interface for convenience.

Ideally you should have one method to encrypt and one method to decrypt. I did not do that. You can use this code to decipher, just change the formula.

Only one bond is enough. In fact one nested in the other would not produce the expected result.

I have already filtered the characters that are accepted and promoted to uppercase within the loop to gain speed.

I researched the correct formula and applied it. I can not guarantee it's done in the best way, but this is also a pseudo-encryption.

using static System.Console;

public class Program {
    public static void Main() {
        int escolha = 1;
        while (escolha != 0) {
            WriteLine("ESCOLHA UMA OPÇÃO");
            WriteLine("-----------------");
            WriteLine("1-Encriptar");
            WriteLine("2-Decriptar");
            WriteLine("0-Encerrar");
            WriteLine("-----------------");
            if (!int.TryParse(ReadLine(), out escolha) || escolha < 0 || escolha > 2) {
                WriteLine("Opção inválida");
                continue;
            }
            if (escolha != 0) {
                WriteLine("Digite a mensagem: ");
                var mensagem = ReadLine();
                WriteLine("Digite a chave: ");
                var chave = ReadLine();
                WriteLine(mensagem.ToUpper());
                WriteLine(chave.ToUpper());
                WriteLine(CifraVigenere(mensagem, chave, escolha == 1));
            }
        }
    }
    private static string CifraVigenere(string mensagem, string chave, bool flag) {
        if (flag) {
            var codigo = "";
            for (int i = 0, j = 0; i < mensagem.Length; i++, j++) {
                char c = char.ToUpper(mensagem[i]);
                if (c < 'A' || c > 'Z') {
                    continue;
                }
                codigo += (char)((c + char.ToUpper(chave[j % chave.Length]) - 2 * 'A') % 26 + 'A');
            }
            return codigo;
        }
        return ""; //até criar o decript
    }
}

See running on .NET Fiddle . And at Coding Ground . Also I put it in GitHub for future reference .

    
19.02.2017 / 03:24
0

Headers private void Decrypt (string cipher, string key)

 private void Decifra(string cifra, string chave){
            var mensagem = "";
            for (int i = 0 ; i < cifra.Length; i++)
            {
                char c = char.ToUpper(cifra[i]);
                if (c < 'A' || c > 'Z')
                {
                    continue;
                }
            char cha = char.ToUpper(chave[i % chave.Length]);
                mensagem += (char)((26-(cha%65-c%65))%26+65);
            }
        c.Text = mensagem;

    }
    
02.06.2017 / 14:07