2012-03-20 19 views
0

如何使用此代碼指定文本輸入和輸出? 我需要打開一個文件並閱讀其內容(我知道該怎麼做),然後使用此代碼對其進行解密。PBEwithMD5andDES in C#

public string DecryptUsernamePassword(string cipherText) 
    { 
     if (string.IsNullOrEmpty(cipherText)) 
     { 
      return cipherText; 
     } 

     byte[] salt = new byte[] 
     { 
      (byte)0xc7, 
      (byte)0x73, 
      (byte)0x21, 
      (byte)0x8c, 
      (byte)0x7e, 
      (byte)0xc8, 
      (byte)0xee, 
      (byte)0x99 
     }; 

     PKCSKeyGenerator crypto = new PKCSKeyGenerator("PASSWORD HERE", salt, 20, 1); 

     ICryptoTransform cryptoTransform = crypto.Decryptor; 
     byte[] cipherBytes = System.Convert.FromBase64String(cipherText); 
     byte[] clearBytes = cryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length); 
     return Encoding.UTF8.GetString(clearBytes); 
    } 

密文加密文本和clearBytes是未加密的字節,但我需要使用與用於輸入和輸出C#的形式的文本框。

這就是它需要的工作方式:textBox1.Text(input) - > bytes - >^above^string - > bytes - > textBox2.Text(output)只要我的輸入是加密的文本,任何東西都可以工作tbh我的輸出是解密文本。

+0

目前尚不清楚你的要求。這段代碼不起作用嗎?你問如何將這個添加到一些文本框的GUI? – mfanto 2012-03-20 17:15:45

+0

準確地說,我應該補充一點,而不是陷入如何解釋我的目標的困惑之處。 – 2012-03-20 17:19:45

+0

最好是找到一個C#winforms或WPF教程,並根據您的需求進行調整。只需使用2個文本框創建您的表單。在按鈕點擊處理程序中,您可以執行以下操作:string result = DecryptUsernamePassword(inputTextBox.Text); resultTextBox.Text = result; – mfanto 2012-03-20 17:25:23

回答

0

根據您的意見,假設我仍然正確理解問題。使這個到它自己的類:

public class UsernameDecryptor 
{ 
     public string Decrypt(string cipherText) 
     { 
      if (string.IsNullOrEmpty(cipherText)) 
       return cipherText; 


      byte[] salt = new byte[] 
      { 
       (byte)0xc7, 
       (byte)0x73, 
       (byte)0x21, 
       (byte)0x8c, 
       (byte)0x7e, 
       (byte)0xc8, 
       (byte)0xee, 
       (byte)0x99 
      }; 

      PKCSKeyGenerator crypto = new PKCSKeyGenerator("PASSWORD HERE", salt, 20, 1); 

      ICryptoTransform cryptoTransform = crypto.Decryptor; 
      byte[] cipherBytes = System.Convert.FromBase64String(cipherText); 
      byte[] clearBytes = cryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length); 

      return Encoding.UTF8.GetString(clearBytes); 
     } 
} 

然後,你的按鈕處理程序中:

private void button1_Click (object sender, System.EventArgs e) 
{ 
    UsernameDecryptor decryptor = new UsernameDecryptor(); 

    string result = decryptor.Decrypt(inputTextBox.Text); 

    outputTextBox.Text = result; 
} 
+0

當我按下按鈕時,保持說64位字符數組的無效長度。 – 2012-03-20 17:57:29

+0

這工作nvm我把它放錯了XD – 2012-03-31 14:40:07