Vigenere Algorithm in C # Explanation

I came across this code:

Vigenere Cipher byte, decryption error

But trying to follow the rules, I asked a new question.

The following algorithm is used, and I'm trying to better understand it:

Byte[] result= new Byte[plaintext.Length];

key = key.Trim().ToUpper();

int keyIndex = 0;
int keylength = key.Length;

for (int i = 0; i < plaintext.Length; i++)
{
    keyIndex = keyIndex % keylength;
    int shift = (int)key[keyIndex] - 65;
    result[i] = (byte)(((int)plaintext[i] + shift) % 256);
    keyIndex++;
}

Do I think that you need to trim the key, as in Unicode? so subtracting 65 from capital gives a common symbol / symbol?

+2
source share
1 answer

The ASCII value for capital A is 65. All characters in are keyconverted to uppercase, this simply returns the alphabetical index of each letter in key.

key , " " .

BAD, 1, 0 3, " " :

Hello world
10310310310 <-- added to each character
Ieomo#xoumd

, :

StringBuilder demonstration = new StringBuilder();
foreach (byte b in result)
{
    demonstration.Append((char)b);
}
Console.WriteLine(demonstration.ToString());
+1

Source: https://habr.com/ru/post/1616587/


All Articles