2013-07-28 22 views
0

我使用的ASP中繼器顯示的圖像,我希望對它進行加密,但我不知道如何解密每次中繼火項目解密repeater項目使用的ItemDataBound

//client side <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound" >

`//server side 
//decrypt method 
private void DecryptFile(string inputFile, string outputFile) 
    { 

     { 
      string password = @"myKey123"; // Your Key Here 

      UnicodeEncoding UE = new UnicodeEncoding(); 
      byte[] key = UE.GetBytes(password); 

      FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); 

      RijndaelManaged RMCrypto = new RijndaelManaged(); 

      CryptoStream cs = new CryptoStream(fsCrypt, 
       RMCrypto.CreateDecryptor(key, key), 
       CryptoStreamMode.Read); 

      FileStream fsOut = new FileStream(outputFile, FileMode.Create); 

      int data; 
      while ((data = cs.ReadByte()) != -1) 
       fsOut.WriteByte((byte)data); 

      fsOut.Close(); 
      cs.Close(); 
      fsCrypt.Close(); 

     } 
    } 

`

protected void Repeater1_ItemDataBound(object source, RepeaterCommandEventArgs e) 
     { 
      if (e.CommandName =="openimage") 
      { 
       string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }); 
       DecryptFile(commandArgs[0], commandArgs[0]); 

      } 
     } 

當我嘗試運行它,它給我的錯誤CS0123:沒有重載「Repeater1_ItemDataBound」匹配委託「System.Web.UI.WebControls.Repeater ItemEventHandler」

請幫助我,因爲我是新來的C#

回答

0

方法簽名的事件處理程序不正確。您需要將RepeaterCommandEventArgs更改爲RepeaterItemEventArgs

protected void Repeater1_ItemDataBound(object source, RepeaterItemEventArgs e) 
{ 
} 

這將解決您的錯誤,但是與你正在試圖做的我不知道,這將是顯示解密圖像的好方法是什麼。我建議創建一個通用處理程序,通過傳入id來動態解密圖像,一旦通過了id,您就可以解密並寫入屏幕。

public class DisplayImage : IHttpHandler 
{ 

    /// <summary> 
    /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface. 
    /// </summary> 
    /// <param name="context">An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> 
    public void ProcessRequest(HttpContext context) 
    { 
     if (!this.HasAccess()) 
     { 
      context.Response.End(); 
      return; 
     } 

     string requestFileName = context.Request.QueryString["FileName"]; 

     DecryptFile(requestFileName, context); 
    } 

    /// <summary> 
    /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler" /> instance. 
    /// </summary> 
    /// <value><c>true</c> if this instance is reusable; otherwise, <c>false</c>.</value> 
    /// <returns>true if the <see cref="T:System.Web.IHttpHandler" /> instance is reusable; otherwise, false.</returns> 
    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

    /// <summary> 
    /// Determines whether the user has access to display an image. 
    /// </summary> 
    /// <returns><c>true</c> if this instance has access; otherwise, <c>false</c>.</returns> 
    private bool HasAccess() 
    { 
     // Check if user is logged in and has permissions 
     // to do the decryption 
     // use your own logic here 
     return true; 
    } 

    /// <summary> 
    /// Decrypts the file and outputs to the response buffer 
    /// </summary> 
    /// <param name="inputFile">The input file.</param> 
    /// <param name="context">The context.</param> 
    private void DecryptFile(string inputFile, HttpContext context) 
    { 
     if (PathTraversalCheck(inputFile)) 
     { 
      context.Response.End(); 
      return; 
     } 

     // get the base directory 
     inputFile = Path.Combine(ConfigurationManager.AppSettings["filedirectory"], inputFile); 

     if (!File.Exists()) 
     { 
      context.Response.End(); 
      return; 
     } 

     string password = @"myKey123"; // Your Key Here 

     UnicodeEncoding UE = new UnicodeEncoding(); 
     byte[] key = UE.GetBytes(password); 

     using (FileStream encryptedFile = new FileStream(inputFile, FileMode.Open)) 
     { 
      RijndaelManaged rijndael = new RijndaelManaged(); 

      using (MemoryStream output = new MemoryStream()) 
      { 
       using (CryptoStream cryptoStream = new CryptoStream(encryptedFile, rijndael.CreateDecryptor(key, key), CryptoStreamMode.Read)) 
       { 
        // write to the memory stream 
        var buffer = new byte[1024]; 
        var read = cryptoStream.Read(buffer, 0, buffer.Length); 
        while (read > 0) 
        { 
         output.Write(buffer, 0, read); 
         read = cryptoStream.Read(buffer, 0, buffer.Length); 
        } 

        cryptoStream.Flush(); 

        // output to the response buffer 
        context.Response.ContentType = "image/jpeg"; 
        context.Response.BinaryWrite(output.ToArray()); 
       } 
      } 

     } 
    } 

    /// <summary> 
    /// Checks for a path traversal attack 
    /// </summary> 
    /// <param name="inputFile">The input file.</param> 
    /// <returns>System.String.</returns> 
    private bool PathTraversalCheck(string inputFile) 
    { 
     if (inputFile.Contains(".") || inputFile.Contains('\\') || inputFile.Contains('/')) 
     { 
      return true; 
     } 

     return false; 
    } 
} 

在你的中繼器中,你只需要把一個帶有src的img標籤設置給處理程序。

<ItemTemplate> 
    <img src="DisplayImage.ashx?FIleName=<%# DataBinder.Eval(Container.DataItem, "FileName")%>" /> 
</ItemTemplate> 

其他我也會改變的是我不會將我的密鑰存儲在源代碼中,這很容易通過反編譯DLL來讀取。相反,您應該將其存儲在web.config中並加密web.config。查看http://msdn.microsoft.com/library/dtkwfdky.aspx的說明。

0

您需要更正轉發的事件:

void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { 

} 

此外,用於處理內部轉發活動,你應該將你的代碼Repeater1__ItemCommand事件。

void Repeater1__ItemCommand(Object Sender, RepeaterCommandEventArgs e) {  
     if (e.CommandName =="openimage") 
     { 
      string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }); 
      DecryptFile(commandArgs[0], commandArgs[0]); 

     } 
} 
相關問題