Converting Mike Shaffer's VBScript RC4 Encryption Code to C#, Part 3
By Chris Scott
Read Part 2
In Part 2 we started the conversion of Mike's code from VBScript
to C#. Specifically, we had examined three hurdles involved in the conversion process. In this final
part we'll look at the remaining hurdles and examine the rc4encyrpt class in use!
Hurdle #4: C# doesn't have the VBScript functions Mid() and
Asc(). In the code below, you'll see how we work around those problems.
Here's the C# version with comments (prefixed with //):
|
Some of you may wonder why I didn't just cast the ASCII character code to a
char in C# instead of using Chr() from the VisualBasic namespace (as well as
casting the char to an int instead of using Asc()). As an example, take
casting an int to a char:
int i = 65;
|
This bit of code works and returns the same as the VB function Chr(65): a
capital letter "A". The problem is when the character code (i, in the code
above) is greater than 128. This is the point where the results from both
methods are not the same. As a test, you can use the following code to write
out the integer value, the character value when the int is casted as a char,
and the character value when the Chr() function is used:
|
Due to this, it became clear that it was much easier to use the VB functions
provided by the VisualBasic namespace.
The remaining VBScript code in the RC4Initialize sub is as follows:
|
Here's the C# version with comments:
|
See, that wasn't too bad. All that remains now is to convert the
VBScript EnDeCrypt function to a method in our rc4encrypt class.
This time, we need to declare it as a public method so that our external source code can
call the method. If we marked the method as private, as we did with the
RC4Initialize method, the EnDeCrypt method would only be available
to methods within our rc4encrypt class.
Also, we need to have the EnDeCrypt method return a string value.
|
Note that unlike the EnDeCrypt function in the VBScript version, the C# version doesn't
take any input parameters. Since we
already have class-level properties for Password and PlainText, we can make
use of these properties instead of passing them in as input parameters in the call to EnDeCrypt.
This isn't a requirement, as we could have omitted the
properties and created the EnDeCrypt method to accept two string input parameters.
In the EnDeCrypt method, the first thing we need to do is to declare some variables
that will be used by this method:
|
An important thing to note about the above code is that the string cipher is initialized to an empty string. This is necessary since in the code that follows we will use that variable to append values to and then return as the value of the EnDeCrypt method. This is important because if you don't, you'll get a runtime error (the compiler won't complain) about trying to use an uninitialized value.
We're now left with converting the following VBScript code:
|
We've already overcome the hurdle of not having the Asc() and Mid()
functions available, but what about that Chr() function?
Hurdle #5: C# does not have a Chr() function. However, you'll see below
how we use the Microsoft.VisualBasic namespace again.
Here's the C# version with comments:
|
So, there you have it, a complete conversion from VBScript to C#. Be sure to view the complete source code.
Using the rc4encrypt Class
At this point you may be wondering how to use the
rc4encrypt class in
your C# app (or ASP.NET Web page). First things first - make sure you have compiled the class
into a DLL. If you are using VS.NET you can simply go to the Build menu and select the Build Solution
option. If you are not using VS.NET you'll need to use the command-line compiler. Navigate to the
directory where your .cs file resides and type in:
csc /t:library /r:Microsoft.VisualBasic.dll FileName.cs
Where FileName.cs is the name of the class file you created that contains
the source code presented above.
Once you have the class in DLL-form, to use it simply instantiate the object, set the PlainText and Password properties and
then call the EnDeCrypt() method. (Note that to use it in an ASP.NET Web page the
compiled DLL will need to be placed in your Web application's /bin directory).
Here's a quick code sample with comments:
|
If everything worked, you should see the encrypted output and on the next line the decrypted output which is equal to "testing123". The live demo allows you to provide your own password and plain text strings.
If you have any questions or feedback, please contact me at chris@hostorlando.com.
Happy Programming!




