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

据说Vigenere密码易于使用(在某种程度上),但是当将其直接转换为程序代码时,这是另一个故事.显然.

这是Vigenere广场:
《c# – Vigenere Square Lookup(使用字符串数组)》

假设我有一种方法可以使用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. 07001

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

现在,上面的代码片段已被发现完美无缺,可以说是从人类使用Vigenere Square表的方式字面翻译出来的.

但是,解密方法似乎不喜欢这种方法:

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() 07001

已经确定该问题在解密方法本身的第二个循环内.它是一种原始的查找过程,它只是在与Vigenere Square表的行/列对应的字符串数组中查找.

逻辑是否有问题,或者代码本身有什么问题?伪代码是可以的,我承认我不太确定如何使用Vigenere Square表将人类解码方法转换为伪代码(然后最终转换为我正在使用的选择语言)

请注意,我知道这些问题,我不是在寻找怎么做,而是在我出错的地方:

> The Vigenere algorithm in C# explanation
> Vigenere Cipher c# with “ñ”

我想我的问题与这个问题非常相似,不过我怀疑问题是一样的:

> Vigenere cipher not working

测试加密……

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/检查

最佳答案 事实证明,我首先使用错误的方法查找表格.我的程序编码没有任何问题,因为它按预期工作.这只是它查找值的方式,换句话说,是错误的逻辑.下面是解密方法的修改代码,现在可以正常工作:

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: stackoverflowisthebest

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: stackoverflowisthebest

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

猜猜是时候让我试图找到一种方法来保留资本化以及转换后的资本化.

点赞