2

I came across this code:

Byte Vigenere Cipher, error with decryption

But trying to follow the rules I made a new question about it.

The following algorithm is used and I'm trying to get a better understanding into 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++;
}

Am I right in thinking the key needs to be trimmed as it is in Unicode? therefore subtracting 65 from a capital produces a common character/symbol?

4

1 回答 1

1

大写字母A 的 ASCII 值为 65。 中的所有字符key都转换为大写,这将简单地返回 中每个字母的字母索引key

以这种方式将每个字母key转换为一个数字,并且原始字符串中的每个字母都“向上移动”该位置数。

如果您的键是BAD,这将变成数字 1、0 和 3,然后应用到“hello world”,如下所示:

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());
于 2012-11-25T16:30:25.490 回答