2016-12-02 72 views
-6

我試圖寫一個'enrypt-decrypt'程序,並且解密函數有一些問題。解密函數不工作c#

不知何故,我無法將流讀取器「ReadToEnd()」保存到名爲「text」的空字符串中。

我發現了互聯網的功能,我試圖通過改變變量名稱並使用「IDisposed」而不是「using」來解決它。我無法解決它。

static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV) 
{ 
    string text = String.Empty; 
    // Create an Aes object 
    // with the specified key and IV. 
    using (Aes aesAlg = Aes.Create()) 
    { 
     aesAlg.Key = Key; 
     aesAlg.IV = IV; 

     // Create a decrytor to perform the stream transform. 
     ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); 

     // Create the streams used for decryption. 
     using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
     { 
      using (CryptoStream cs = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write)) 
      { 

       using (StreamReader srDecrypt = new StreamReader(cs)) 
       { 
        // Read the decrypted bytes from the decrypting 
        stream 

       // and place them in a string. 
        text = srDecrypt.ReadToEnd(); 
       } 

      } 
     } 
    } 
    return text; 
} 

文本字符串僅在該功能定義,誤差進來的行:

文本= srDecrypt.ReadToEnd();

它說:

Crypt.cs(145,29):錯誤CS0136:命名爲「文本」的局部變量不能 在此範圍中聲明,因爲它會給予不同的含義 「文本「這已經是一個'父母或目前使用的」範圍表示 別的編譯失敗:1個錯誤,0警告

+3

錯誤是哪一行?還將代碼發佈爲**實際代碼**,而不是圖片。儘管如果我不得不猜測,你有'string text = String.Empty();',你可能已經在你的類中有一個名爲'text'的變量。 –

+0

「流」後代碼中有一點遺漏,但如果您將代碼(作爲文本)發佈到StackOverflow而不是鏈接到位圖,則會更容易。 –

+0

我想知道爲什麼*這麼多人喜歡張貼他們的代碼和錯誤的圖片,而不是代碼和錯誤文本本身。也許這是一個元話題...但爲什麼它經常發生? –

回答

-1

我想你錯過了這裏的一些代碼,原因是無效的:

// Read the decrypted bytes from the decrypting 
stream 
+0

您認爲我應該在此之後添加什麼?我沒有錯過粘貼在這裏的代碼,它就像我的 –

+0

嘗試刪除這一行,然後編譯你的代碼。 –