Vigenere Square Lookup(使用字符串数组)

Vigenere Square Lookup (using string arrays)

Vigenere 密码据说易于使用(嗯,在某种程度上),但是当将其直接转换为程序代码时,情况就完全不同了。显然。

这是维吉尼亚广场:

假设我有一种方法可以使用 Vigenere Square 密码加密文本,同时仍保留空格和特殊字符(或其中的大部分)。

static string EncryptedText(string plaintext, string keyword)
{
    string tempStore = "";
    string KeyToUse = ExpandKey(RemoveAllNonAlpha(plaintext), keyword);
    string[] tempList;
    int iSelector = 0;

    for (int ii = 0; ii < RemoveAllNonAlpha(plaintext).Length; ii++)
    {
        tempList = GetNewAlphaList(KeyToUse[ii].ToString());
        if (RemoveAllNonAlpha(plaintext)[ii].ToString() != " ")
        {
            iSelector = NeverOver26(GetNumericFromLetter(RemoveAllNonAlpha(plaintext)[ii].ToString())) - 1;

            tempStore += tempList[iSelector].ToLower();
        }
        else
        {
            tempStore += " ";
        }
    }

    return ReplaceAllNonAlpha(tempStore, plaintext);
}

如果我们假设对于上面的情况,下面的函数也是这样...

string ExpandKey(string input) => Lengthen the key until it matches the plaintext.

string RemoveAllNonAlpha(string input) => Basically remove anything that is not an alphabet. Uses Regex Replace.

int GetNumericFromLetter(string char) => Just converts a letter to a number, with A = 1, and Z = 26. Adapted version of this accepted answer.

string ReplaceAllNonAlpha(string processed, string original) => Basically it just checks the original string, and then replaces all the non alphabetical characters. It's mainly using a regex string to check if a character is not an alphabet.

int NeverOver26(int input) => Basically it just subtracts 26 from the value whenever it goes over 26.

string[] GetNewAlphaList(string char) => Generates a column or row of the vigenere cipher to lookup from, with the letter passed in being the first letter in the array. For example, if the passed letter is "L", then it returns { "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K" }.

现在,上面的代码片段被发现可以完美运行,可以说是从 humans 将使用 Vigenere Square table.

不过,解密方法好像不太喜欢这种做法,虽然:

static string DecryptedText(string ciphertext, string keyword)
{
    //Broken, non deciphering
    string tempStore = "";
    string KeyToUse = ExpandKey(RemoveAllNonAlpha(ciphertext), keyword);
    string[] tempList;

    for (int ii = 0; ii < RemoveAllNonAlpha(ciphertext).Length; ii++)
    {
        tempList = GetNewAlphaList(RemoveAllNonAlpha(ciphertext)[ii].ToString());

        for (int iii = 0; iii < tempList.Length; iii++)
        {
            if (tempList[iii].ToString().ToLower() == KeyToUse[ii].ToString().ToLower())
            {
                tempStore+= GetAlphaFromNumber(iii).ToLower();
                break;
            }
        }
    }

    return ReplaceAllNonAlpha(tempStore, ciphertext);
}

在(没有按预期工作)解密方法中,我们可以看到它使用了与加密方法大部分相同的功能,除了...

string GetAlphaFromNumber(int input) => Does the exact opposite of GetNumericFromLetter() Adapted version of this accepted answer.

已确定问题出在解密方法本身的第二个 for 循环中。这是一种原始的查找过程,它只是在一个字符串数组中查找,该字符串数组对应于 Vigenere Square table.

的 row/column

逻辑有问题,还是代码本身有问题?伪代码还可以,我承认我不太确定如何翻译人类使用 Vigenere Square table 解码为伪代码的方法(然后最终解码为我正在使用的选择语言)

请注意,我知道这些问题,我不是在寻找如何去做,而是在寻找我哪里出错了:

我想我的问题与这个问题非常相似,但我怀疑问题是否相同:


正在测试加密...

Input: ATTACK ON DAWN. LOL

Keyword: LEMON

Output: lxfopv ef rnhr. xcy


正在测试解密...

Input: lxfopv ef rnhr. xcy

Keyword: LEMON

Output: ahhayq ah xaen. pmp

结果检查使用:http://planetcalc.com/2468/

原来我一开始使用错误的方法查找table。我的程序编码没有任何问题,因为它按预期工作。这只是它查找值的方式,或者换句话说,错误的逻辑。下面是修改后的解密方法代码,现在可以正常使用了:

internal static string DecryptedText(string ciphertext, string keyword)
{
    string tempStore = "";
    string KeyToUse = ExpandKey(RemoveAllNonAlpha(ciphertext), keyword);
    string[] tempList;
    int iSelector = 0;

    for (int ii = 0; ii < RemoveAllNonAlpha(ciphertext).Length; ii++)
    {
        tempList = GetNewAlphaList(KeyToUse[ii].ToString());

        for (int iii = 0; iii < tempList.Length; iii++)
        {
            ////seperated the two to verify they were not returning the wrong values
            //string FromList = tempList[iii].ToString().ToLower();
            //string FromCipher = RemoveAllNonAlpha(ciphertext)[ii].ToString().ToLower();

            if (tempList[iii].ToString().ToLower() ==  RemoveAllNonAlpha(ciphertext)[ii].ToString().ToLower())//if (FromList == FromCipher)
            {
                tempStore += GetAlphaFromNumber(iii).ToLower();
                break;
            }
        }
    }
        
    return ReplaceAllNonAlpha(tempStore, ciphertext);
}

正在测试加密...

Input: this is just a simple test. complete with punctuations galore, wohoo!

Keyword: Whosebugisthebest

Output: laiu sg eyjy l geuhel xfwl. vgfpnohz azys dqvumbeumggk zanyfz, afmzc!


正在测试解密...

Input: laiu sg eyjy l geuhel xfwl. vgfpnohz azys dqvumbeumggk zanyfz, afmzc!

Keyword: Whosebugisthebest

Output: this is just a simple test. complete with punctuations galore, wohoo!


我想是时候尝试找到一种方法让它在转换后也能保留大写了。