2016-11-23 27 views
-9
string cipherData; 
byte[] cipherbytes; 
byte[] plainbytes; 
byte[] plainbytes2; 
byte[] plainkey; 

SymmetricAlgorithm desObj; 
private void button1_Click(object sender, EventArgs e) 
{ 
    cipherData = textBox_Plain_text.Text; 
    plainbytes = Encoding.ASCII.GetBytes(cipherData); 
    plainkey = Encoding.ASCII.GetBytes("123456789abcdef"); 
    desObj.Key = plainkey; 
    // choose other appropriate modes (CBC, CFB, CTS, ECB, OFB) 
    desObj.Mode = CipherMode.CBC; 
    desObj.Padding = PaddingMode.PKCS7; 
    System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
    CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), new CryptoStreamMode()); 
    cs.Write(plainbytes, 0, plainbytes.Length); 
    cs.Close; 
    cipherbytes = ms.ToArray(); 
    ms.Close; 
    textBox_Encrypted_text.Text = Encoding.ASCII.GetString(cipherbytes); 
} 

error :Only assignment, call, increment, decrement, and new object expressions can be used as a statement.編譯錯誤:只有轉讓,電話,遞增,遞減和新對象表達式可以用作聲明

+5

而究竟在何處是錯誤?請提供[mcve],清楚說明錯誤的位置。 –

+4

看起來你在'.Close'之後丟失了括號。應該是'.Close()',因爲它是方法調用。 –

+0

你留下幾個未處理的一次性用品。 Encoding.UTF8是比.ASCII更好的選擇 –

回答

2

Only assignment, call, increment, decrement, and new object expressions can be used as a statement.

的文件說,爲statement

The actions that a program takes are expressed in statements. Common actions include declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition

您的基本問題在於你錯過了()括號,它會告訴編譯器你想在這些行中調用一個方法:

cs.Close; 
ms.Close; 

所以他們更改爲:

cs.Close(); 
ms.Close(); 

否則編譯器認爲您試圖訪問一個字段或屬性,告訴你這不能單獨作爲一個聲明。由於錯誤消息指出您可以執行:

assignment,

int c = ms.Capacity; 

call

ms.Close(); 

increment, decrement

ms.Capacity++; 

new object expressions

new MemoryStream(); 
相關問題