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

本文关键字:字符串 转换 数组 | 更新日期: 2023-09-27 18:17:50

我正在研究一个使用列换位密码的密码程序。我首先获取用户输入并将其放入字符串数组中,然后打印该数组的打乱版本。为了解密消息,我对加密的消息进行了解密,并尝试从字符串数组中的每个"列"中打印一个元素。

例如,如果我的消息是"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;
    }
}

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

我是这样解决我的问题的:

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