将字符串数组转换为字符串

时间:2016-11-15 01:15:29

标签: c# arrays string

我正在使用一个使用柱状转置密码的密码程序。我首先抓取用户输入并将其放入字符串数组中,然后打印一个洗牌版本的数组。为了破译消息,我正在解密加密的消息并尝试从字符串数组中的每个“列”打印消息一个元素。

例如,如果我的消息是“Hey there dude”,它将被加密为“TD * HU * ERE __ * YE * HED”(空格由下划线替换,空格替换为星号)。然后解密应该重新组织数组,然后遍历数组的每个元素中的第一项,将其打印到字符串,然后移动到数组的每个元素中的第二项以将其打印到字符串,等等类推。

        string[] columnEncrypted = userMessage.Split(' ');
        string[] columnDecrypted = new string[6];
        string DecryptedString = string.Empty;

        columnDecrypted[0] = columnEncrypted[5];
        columnDecrypted[1] = columnEncrypted[2];
        columnDecrypted[2] = columnEncrypted[4];
        columnDecrypted[3] = columnEncrypted[3];
        columnDecrypted[4] = columnEncrypted[0];
        columnDecrypted[5] = columnEncrypted[1];

        for (int r = 0; r <= columnDecrypted.Length; r++)
        {
            for (int c = 0; c < 6; c++)
            {
                if (columnDecrypted[c] == "_")
                {
                    DecryptedString += ' ';
                }
                else
                {
                    DecryptedString += columnDecrypted[c];
                }
            }
        }
        return DecryptedString;

这是我第一次在这里发帖提问,如果我的解释不清楚,请耐心等待。如果需要更多代码来更好地回答我的问题,请告诉我,我会提供更多。

加密代码如下:

public string CipheredString(string userMessage)
    {
        string[] column = new string[6];

        for(int c = 0; c <= 5; c++)
        {
            for (int i = c; i < userMessage.Length; i += 6)
            {
                if (userMessage[i] == '.')
                {
                    column[c] += "."; 
                }
                else if (userMessage[i] == ',')
                {
                    column[c] += ",";
                }
                else if (userMessage[i] == '?')
                {
                    column[c] += "?";
                }
                else if (userMessage[i] == '!')
                {
                    column[c] += "!";
                }
                else if (userMessage[i] == ' ')
                {
                    column[c] += "_";
                }
                else
                {
                    column[c] += userMessage[i];
                }
            }
        }

        if (column[5].Length < column[0].Length)
        {
            if (column[4].Length < column[0].Length)
            {
                if (column[3].Length < column[0].Length)
                {
                    if (column[2].Length < column[0].Length)
                    {
                        if (column[1].Length < column[0].Length)
                        {
                            column[1] += "*";
                            column[2] += "*";
                            column[3] += "*";
                            column[4] += "*";
                            column[5] += "*";
                        }
                        else
                        {
                            column[2] += "*";
                            column[3] += "*";
                            column[4] += "*";
                            column[5] += "*";
                        }
                    }
                    else
                    {
                        column[3] += "*";
                        column[4] += "*";
                        column[5] += "*";
                    }
                }
                else
                {
                    column[4] += "*";
                    column[5] += "*";
                }
            }
            else
            {
                column[5] += "*";
            }
        }

        string EncryptedString = column[4] + " " + column[5] + " " + column[1] + " " + column[3] + " " + column[2] + " " + column[0];
        return EncryptedString;
    }
}

1 个答案:

答案 0 :(得分:0)

这就是我最终解决问题的方法:

public string CipheredString(string userMessage)
    {
        string[] columnEncrypted = userMessage.Split(' ');
        string[] columnDecrypted = new string[6];
        string DecryptedString = string.Empty;
        string DirtyDecrypt = string.Empty;

        columnDecrypted[0] = columnEncrypted[5];
        columnDecrypted[1] = columnEncrypted[2];
        columnDecrypted[2] = columnEncrypted[4];
        columnDecrypted[3] = columnEncrypted[3];
        columnDecrypted[4] = columnEncrypted[0];
        columnDecrypted[5] = columnEncrypted[1];

        for (int c = 0; c < columnDecrypted[0].Length; c++)
        {
            for (int r = 0; r < 6; r++)
            {
                string row = columnDecrypted[r];
                string column = row[c].ToString();
                DirtyDecrypt += column;
            }
        }
        string CleanDecrypt = DirtyDecrypt.Replace("_", " ").Replace("*","");

        return CleanDecrypt;
    }