October 2009
M T W T F S S
    Nov »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Categories

  • [+]ASP.NET (7) 
  • [—]C# (87) 
  • [+]C++ (13) 
  • [+]Delphi (20) 
  • [+]JavaScript (24) 
  • [+]Regular Expressions (7) 
  • [+]SQL (13) 
  • [+]VB (100) 
  • [+]VB.NET (4) 

Online

Users: 6 Guests, 1 Bot
  • Loading...


    Loading...

    Login






    Register | Lost password?

    Register





    A password will be mailed to you.
    Log in | Lost password?

    Retrieve password





    A confirmation mail will be sent to your e-mail address.
    Log in | Register
  • RC4 Encryption

    Author: Rick Sands

    URL: http://www.bytemycode.com/snippets/snippet/93/

    This implementation encodes the byte stream to be encrypted "in-place"

    // RC4 Encryption
    
    /*
      From RSA Security's website:
      "RC4 is a stream cipher designed by Rivest for RSA Data
      Security (now RSA Security). It is a variable key-size stream
      cipher with byte-oriented operations. The algorithm is based on
      the use of a random permutation. Analysis shows that the period
      of the cipher is overwhelmingly likely to be greater than 10^100.
      Eight to sixteen machine operations are required per output byte,
      and the cipher can be expected to run very quickly in software.
      Independent analysts have scrutinized the algorithm and it is
      considered secure."
    
      This implementation encodes the byte stream to be encrypted
      "in-place".
    
      ------------
      // Examples:
      ------------
    
      Byte[] Key = new Byte[5] { 12, 34, 22, 12, 32 };
      Byte[] B = new Byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    
      // Examine B array before and after this next call.
      RC4(ref B, Key);
    
      // Examine B array before and after this next call.
      RC4(ref B, Key);
    */
    
    public void RC4(ref Byte[] bytes, Byte[] key )
    {
            Byte[] s = new Byte[256];
            Byte[] k = new Byte[256];
            Byte temp;
            int i, j, t;
            int byteLen = bytes.GetLength(0);
            int keyLen = key.GetLength(0);
    
            // Generate "8x8 S-Box" and initialize key index
            for (i = 0; i < 256; i++)
            {
                    s[i] = (Byte) i;
                    k[i] = key[i % keyLen];
            }
    
            j = 0;
            for (i = 0; i<256; i++)
            {
                    j = (j + s[i] + k[i]) % 256;
                    // swap
                    temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
            }
    
            i = j = 0;
            for (int x = 0; x < byteLen; x++)
            {
                    // The following is used to generate a random byte
                    i = (i + 1) % 256;
                    j = (j + s[i]) % 256;
                    temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
                    t = ((int)s[i] + s[j]) % 256;
                    // which is xor'd to the source
                    bytes[x] ^= s[t];
            }
    }
     

    Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • del.icio.us
    • Bloglines
    • Facebook
    • Google Bookmarks
    • LinkedIn
    • Technorati
    • TwitThis
    • Webnews

    Leave a Reply

     

     

     

    You can use these HTML tags

    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">

    Spam Protection by WP-SpamFree Plugin