当前位置: 动力学知识库 > 问答 > 编程问答 >

How can i encode a sentence in C# [Cryptography]

问题描述:

I am currently working on an exercise where I am supposed to create two functions. One for encoding a sentence. And the second one for decoding another sentence. It's like cryptography.

Functions apart, I'm trying to program the code as a simple way.

Here are the steps:

  1. Reading what user is typing.
  2. Compare the string recieved with an alphabet.
  3. when the letter is similar, the cryptography's command's acting (letter moves forward 8 steps).

Example:

String Alphabet = "abcdefghijklmnopqrstuvwxyz";

Console.WriteLine("Indiquez votre phrase à encoder:");

string PhraseAEncoder = Console.ReadLine();

Console.WriteLine(PhraseAEncoder);

int PositionLettreFinale = 0;

for (int i = 0; i < Alphabet.Length; i++)

{

if (PhraseAEncoder[i] == Alphabet[i])

{

PositionLettreFinale = Alphabet[i + 8]; // Je récupère la position finale.

Console.WriteLine(PositionLettreFinale);

}

while (i != PositionLettreFinale)

{

i++;

Console.WriteLine(Alphabet[i]);

}

}

It is written in French. But I think you could get it.

Problem: My for is infinite, I can't get a single result.

网友答案:

I see two main issues in your code:

PositionLettreFinale = Alphabet[i + 8];

  1. Here you can get outside of Alphabet array (For example when i equals to 24, you will access element in 32nd position and it doesn't exist). IndexOutOfRangeException

  2. Also in the same line, you are assigning a value of type char to the variable PositionLettreFinale type of int.

When assigning a value of type char to a variable of type int, it will be converted to int based on ASCII Table.

And now, an answer to your question.

Problem: My for is infinite, I can't get a single result.

for isn't infinite, while is infinite.

while (i != PositionLettreFinale)

You are comparing i with a decimal value of char. They will never be equal because of i has maximal value 25 (Alphabet.Length - 1) and PositionLettreFinale has minimal value 'a' (97).

网友答案:

The code can not work. Simple "crypto" method example: (based on your code)

private static string Crypto(string PhraseAEncoder, int shift)
{
  string Alphabet = "abcdefghijklmnopqrstuvwxyz";
  StringBuilder result = new StringBuilder(PhraseAEncoder.Length);
  foreach (char c in PhraseAEncoder)
  {
    int code = Alphabet.IndexOf(c);
    if (code < 0)
      result.Append(c);
    else
    {
      code += shift;
      if (code >= Alphabet.Length)
        code %= Alphabet.Length;
      else
      {
        while (code < 0)
          code += Alphabet.Length;
      }
      result.Append(Alphabet[code]);
    }
  }
  return result.ToString();
}

And usage:

static void Main(string[] args)
{
  Console.WriteLine("Indiquez votre phrase à encoder:");
  string PhraseAEncoder = Console.ReadLine().ToLower(); // alphabet is only in lowercase
  string result = Crypto(PhraseAEncoder, 8); // crypto password "8"
  Console.WriteLine(result);
  result = Crypto(result, -8);  // crypto reverse password "-8"
  Console.WriteLine(result);
  if (string.Equals(result, PhraseAEncoder))
    Console.WriteLine("OK");
  else
    Console.WriteLine("Error");
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: