Cipher de Vigenere

Asked

Viewed 1,511 times

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 make each key character receive its ASCII value and return as one array so I can do some sum operations between each ASCII value of each character. For example: ASCII word values "nome": [65, 67, ...] and key ASCII values "cade": [65, 67, ...], so I would add the values of the same index of array.

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

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

2 answers

3

It took me a long time to understand what the code was supposed to do. Now I liked 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 didn’t need the ToUpper(), I used only in the interface for convenience.

Ideally I should have one method to encrypt and another to decipher. I didn’t fix that. You can use that code to decipher, just change the formula.

Only one loop is enough. Actually one nested in the other would not produce the expected result.

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

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

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
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

0

Private void Decifra headers(string cipher, key string)

 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;

    }
  • This response was flagged as low quality due to size or content. Please, if possible and if this is a solution to the problem, explain better how you solved it. The code may be trivial to you, but to other users it may not be.

  • is just a method of deciphering complementing the above program

  • and the result is sent to a textbox

  • You can [Edit] the answer and add this information directly to it.

  • Sorry, I’m new here.

  • Starting to program now

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.