2016-11-15 146 views
-1

我正在研究使用列轉置密碼的密碼程序。我首先抓取用戶輸入並將其放入字符串數組,然後打印一個混合版本的數組。爲了破譯消息,我對加密消息解密並試圖從字符串數組中的每個「列」中打印消息一個元素。例如,如果我的消息是「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

嗯...有什麼問題嗎? – BrunoLM

+3

你在描述你的程序應該做什麼時做了很好的工作,但我認爲你忘了解釋你的問題。 :) – Amy

+0

對於我們來回答你的問題,你需要告訴我們你的問題。 – Jens

回答

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