When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, September 18, 2002

Converting Mike Shaffer's VBScript RC4 Encryption Code to C#, Part 2

By Chris Scott

  • Read Part 1

  • In Part 1 we briefly looked at the VBScript code for Mike Shaffer's RC4 Encryption algorithm. In this part we'll begin our porting of the VBScript code to C# code!

    - continued -

    Converting the VBScript Code to C#

    Now that we've seen what the VBScript code looks like for the classic ASP version, it is time to convert the function and sub to C#. In my conversion I encountered five "hurdles," which are outlined below.

    Hurdle #1: The ASP code uses functions and subroutines but C# uses classes with properties and methods.

    To convert the ASP code to C#, it makes sense to create a new C# class file for the code. This allows you to reuse the code in your app and even compile the code to a dll for use in an app without giving the programmer of the app the source code. If you use Visual Studio .NET, this is as easy as right clicking on your Web project and Project/Add New Item... from the menu and then choosing C# Class as the file type and giving it a name. If you are coding with a text editor, you can create the empty class using your favorite text editor.

    This class, like all C# classes, will have the following form:

    using System;
    using System.Text;
    using Microsoft.VisualBasic;
    namespace main
        public class rc4encrypt
           // methods and properties go here

    Recall that at the beginning of our VBScript code, two arrays are declared and initialized. To do the equivalent in a class, add the following code after the class declaration:

        public class rc4encrypt
            protected int[] sbox = new int[256];
            protected int[] key = new int[256];

    All code examined henceforth will be code that appears within the class declaration; hence, for brevity, future code examples will not include the public class rc4encrypt { ... } portion...

    If you are used to VBScript and are new to C#, you'll probably notice something right away which brings us to...

    Hurdle #2: All VBScript variables are of type Variant with subtypes for all variables. C# requires you to explicitly declare data types for all your variables. (A great read on VBScript's handling of variables and their types can be found at: Variables and Values in VBScript!)

    In the C# code above, we need to tell the C# compiler that our arrays are of type int since they will contain integer data. The double brackets declare an array. The protected keyword is used so that the arrays can be used within our class or any classes that are derived from our class. You could use the private keyword instead if you knew that this class wouldn't be used as a base class for other classes.

    Note that in VBScript, the total number of elements in an array is the number passed to the array declaration plus one so sbox(255) is actually a 256 element array. In C#, when we create our array, we need to tell it the actual number of elements to use. We'll call this...

    Hurdle #3: VBScript arrays and C# arrays are a little bit different.

    Before converting the sub and function from VBScript to C#, we'll need to declare a couple additional protected variables. These protected variables are only accessible from methods within the class in which they are defined (or in classes that inherit our rc4encrypt class).

           protected string plaintext, password;

    In order to allow code external to the class to access these variables, we will need to provide a property accessor. For this class, we want to declare two such properties, PlainText and Password, which both include a set and get part (this makes the properties read/write).

           public string PlainText
               set { plaintext = value; }
               get { return plaintext; }
           public string Password
               set { password = value; }
               get { return password; }

    These properties will allow us to set the text we want to encrypt and the password we want to encrypt it with. That is, when using our rc4encrypt class we might use code like:

    rc4encrypt myEncrypt = new rc4encrypt();
    myEncrypt.PlainText = "This is a test";
    myEncrypt.Password = "secretsquirrel";

    If you've used COM objects in VBScript code, this format will look familiar. Now that we have our global arrays and our property accessors declared, its time to rewrite the RC4Initialize sub. Since the RC4Initialize method is only used within our class, we declare it as a private method (which means that only methods from within the rc4encrypt class can call RC4Initialize):

            private void RC4Initialize(string strPwd)
              // TODO: code for RC4Initialize goes here...

    This creates an empty method which accepts a string value that will be the password we use to encrypt our data. The void keyword specifies this method will not return any data. Now comes the fun part: converting the meat of the sub to C#! First, we'll take the following VBScript code:

          intLength = len(strPwd)
          For a = 0 To 255
             key(a) = asc(mid(strpwd, (a mod intLength)+1, 1))
             sbox(a) = a

    The first line gets the length of the password that is passed to the sub and sets a variable. Next, we use a for loop to fill up both our arrays with 256 values. These values are the ASCII character code, using Asc() for the value computed by the Mid() function.

    Unfortunately, C# doesn't have the Mid() and Asc() features that VBScript has. In Part 3 we'll examine how to overcome this hurdle!

  • Read Part 3!

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article