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

时间:2015-11-19 15:14:03

标签: c# algorithm encryption pseudocode vigenere

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

这里是Vigenere广场: Vigenere Square from Wikipedia

假设我有一种方法可以使用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)=&gt;加长键直到它与明文匹配。

     

string RemoveAllNonAlpha(string input)=&gt;基本上删除任何不是字母表的东西。使用正则表达式替换。

     

int GetNumericFromLetter(string char)=&gt;只需将字母转换为数字,A = 1,Z = 26. Adapted version of this accepted answer.

     

string ReplaceAllNonAlpha(string processed,string original)=&gt;基本上它只检查原始字符串,然后替换所有非字母字符。它主要使用正则表达式字符串来检查字符是否不是字母。

     

int NeverOver26(int input)=&gt;基本上它只要超过26就从值中减去26。

     

string [] GetNewAlphaList(string char)=&gt;生成vigenere密码的列或行以进行查找,传入的字母是数组中的第一个字母。例如,如果传递的字母是&#34; L&#34;,则返回{&#34; L&#34;,&#34; M&#34;,&#34; N&#34;,&# 34; O&#34;,&#34; P&#34;,&#34; Q&#34;,&#34; R&#34;,&#34; S&#34;,&#34; T&#34 ;,&#34; U&#34;,&#34; V&#34;,&#34; W&#34;,&#34; X&#34;,&#34; Y&#34;,&#34 ; Z&#34;,&#34; A&#34;,&#34; B&#34;,&#34; C&#34;,&#34; D&#34;,&#34; E&#34; ,&#34; F&#34;,&#34; G&#34;,&#34; H&#34;,&#34; I&#34;,&#34; J&#34;,&#34; K&#34; }。

现在,上面的代码片段已被发现完美无缺,可以说是从人类使用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)=&gt;与GetNumericFromLetter()Adapted version of this accepted answer.

完全相反

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

逻辑是否有问题,或者代码本身出了什么问题? Pseudocode没关系,我承认我不太清楚如何翻译人类的解码方法使用Vigenere Square表进入伪代码(然后最终进入我正在使用的语言选择)

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

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

测试加密......

  

输入:攻击DAWN。 LOL

     

关键字:LEMON

     

输出:lxfopv ef rnhr。 XCY

测试解密......

  

输入:lxfopv ef rnhr。 XCY

     

关键字:LEMON

     

输出:ahhayq啊xaen。 PMP

使用:http://planetcalc.com/2468/

检查结果

1 个答案:

答案 0 :(得分:2)

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

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);
}

测试加密......

  

输入:这只是一个简单的测试。哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇!      

关键字:stackoverflowisthebest

     

输出:laiu sg eyjy l geuhel xfwl。 vgfpnohz azys dqvumbeumggk zanyfz,afmzc!

测试解密......

  

输入:laiu sg eyjy l geuhel xfwl。 vgfpnohz azys dqvumbeumggk zanyfz,afmzc!

     

关键字:stackoverflowisthebest

     

输出:这只是一个简单的测试。哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇!

猜猜我现在是时候试图找到一种方法来保留转换后的大写字母。