Tuesday, September 23, 2008

Tips On Brazilain Waxing

errors in a text file in encrypted form

always a good practice to keep a record of errors that occur in any application, I believe that the most common errors is to place a text file as a sort of log, or possibly within of a database to keep track. The more sophisticated there will make the information as an event of windows.

But if we want users of the application that are the curious, seeing and reading errors that occur in our application, a practical way is to encrypt the file. The problem with this is that if the text file becomes too large can slow the encryption application, and you have to open decrypt the file, post a message, and re-encrypt the file. Being

above a little impractical decided best to put the messages encrypted within the file. To modify the class which I am using to save your messages:

  1:     Public Shared Sub    WriteError (ByVal    errorMessage As String   ) 
  2: Try    
  3: Dim     path As String    =  "error.txt"  
     4:                  If   (  Not   File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))   Then   
      5:                    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close() 
      6:                  End     If   
      7:                Using w   As   StreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)) 
      8:                    w.WriteLine(Constants.vbCrLf &   "Error : "  ) 
      9:                  w.WriteLine ( "{0}" , DateTime.Now.ToString (CultureInfo.InvariantCulture)) 
  10:    Dim err As     String =  "In line:"  & System . Web.HttpContext.Current.Request.Url.ToString () & . "Message:" & errorMessage  
  11:  w.WriteLine (Encrypt (err,   True)) 
  12:  w.WriteLine ( "__________________________" ) 
  13:  w.Flush () 
  14:                   w.Close() 
     15:                  End   Using 
     16:              Catch   ex   As   Exception 
     17:                WriteError(ex.Message) 
     18:              End     Try   
     19:          End   Sub 


If you look at line 11 I put the word Encrypt, is there where I'm calling another function, the file imported into the library. NET Cryptography: Imports System.Security.Cryptography


 Public     Function Encrypt (ByVal   toEncrypt    As String , ByVal   useHashing    As Boolean )    As String Dim  
   keyArray  As    Byte () Dim 
   toEncryptArray  As    Byte () = UTF8Encoding. UTF8.GetBytes (toEncrypt) Dim 
     key   As     String   =   "MyKEY"   
           If   useHashing   Then   
               Dim   hashmd5   As     New   MD5CryptoServiceProvider() 
             keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)) 
             hashmd5.Clear() 
           Else   
             keyArray = UTF8Encoding.UTF8.GetBytes(key) 
           End     If   
           Dim   tdes   As     New   TripleDESCryptoServiceProvider() 
        tdes.Key keyArray 
 tdes.Mode = = = CipherMode.ECB 
 tdes.Padding PaddingMode.PKCS7 
  Dim  ctransform  The  ICryptoTransform tdes.CreateEncryptor = () Dim 
   resultArray  The   GB  () = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length) 
 tdes.Clear () Return 
   Convert.ToBase64String (resultArray, 0, resultArray.Length) 
  End  function 


Now we have our encrypted messages. If we want to decrypt messages only have to use the following function somewhere and call it:



    Public Shared Function    Decrypt (ByVal   cipherString  As    String, ByVal   useHashing As     Boolean) As    String Dim  
   keyArray  As    Byte () Dim 
     toEncryptArray   As     Byte  () = Convert.FromBase64String(cipherString) 
           Dim   key   As     String   =   "MyKEY"   
           If   useHashing   Then   
               Dim   hashmd5   As     New   MD5CryptoServiceProvider() 
             keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)) 
             hashmd5.Clear() 
           Else   
             keyArray = UTF8Encoding.UTF8.GetBytes(key) 
           End     If  
  Dim TDES   The New    TripleDESCryptoServiceProvider () 
 tdes.Key keyArray 
 tdes.Mode = = = CipherMode.ECB 
 tdes.Padding PaddingMode.PKCS7 
  Dim  ctransform  The  ICryptoTransform tdes.CreateDecryptor = () Dim 
   resultArray  The    Byte () = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length) 
 tdes.Clear () Return 
   UTF8Encoding.UTF8. GetString (resultArray) 
  End      Function   

} Maybe use an aspx form to receive the encrypted code:


  \u0026lt;% @ Page Language = "vb" AutoEventWireup = "false" CodeBehind = "WebForm1.aspx.vb" Inherits = "Bintech . WebForm1 "%>  
  \u0026lt;!   DOCTYPE html       PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "     >   
   <    html     xmlns    ="http://www.w3.org/1999/xhtml"     >   
   <    head     runat    ="server"    >   
       <    title    >  Untitled Page  </    title    >   
   </    head    >   
   <    body    >    
       <    form     id    ="form1"     runat    ="server"    >   
       <    div    >   
         Forma de desencriptacion:  <    br     />   
           <    asp:TextBox     ID    ="TextBoxEncriptado"     runat    ="server"     Height    ="173px"     TextMode    ="MultiLine"     Width = "308px"  > \u0026lt;/   asp: TextBox  >  
  \u0026lt;    br />  
  \u0026lt;  asp: Button     ID = "Button1"  
    runat = "server" Text     = "Button"  >  
  \u0026lt;    br />  
  \u0026lt;  asp: Label      ID    ="LabelResult"     runat    ="server"    ></    asp:Label    ></    div    >   
       </    form    >   
   </    body    >   
   </    html    >   
  

and add an event to the button:







    Protected Sub Button1_Click  (  ByVal sender As   System.   Object, ByVal    and  As System.EventArgs) Handles   Button1.Click 
 LabelResult.Text = Decrypt (TextBoxEncriptado.Text,   False) End 
   Sub 


Ready!

0 comments:

Post a Comment