![]() |
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
Published: Wednesday, September 18, 2002 By Chris Scott
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!
Converting the VBScript Code to C# 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:
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:
All code examined henceforth will be code that appears within the 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
In the C# code above, we need to tell the C# compiler that our arrays are
of type
Note that in VBScript, the total number of elements in an array is the number
passed to the array declaration plus one so 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
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,
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
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
This creates an empty method which accepts a string value that will be the
password we use to encrypt our data. The
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
Unfortunately, C# doesn't have the
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||