Microsoft.VisualBasic.Strings.Asc ?

J

James

Hello,

I'm trying to put together a simple utility to do rc4 encryption based on
the rc4 algorithm here:
http://aspnet.4guysfromrolla.com/articles/091802-1.3.aspx

that article is about porting Mike Shaffer's VBScript RC4 to c#... I'm using
that code to make the class but use in a windows form. Using MS Visual C#
Express edition sp1.

so the problem I get when compiling the class is from the use of
Microsoft.VisualBasic.Strings.Asc(), like the following line:

key[a] = Microsoft.VisualBasic.Strings.Asc(ctmp);

The error I get is:
"The type or namespace name 'Strings' does not exist in the namespace
'Microsoft.VisualBasic' (are you missing an assembly reference?)"

this function is used a few times within the class. I am not a real
experienced csharper and literally just installed this c# express edition to
do this little thing... any help would be greatly appreciated. I did
include the 'using Microsoft.VisualBasic;' include directive at the top to
include the namespace.

This is the source code:
http://aspnet.4guysfromrolla.com/code/rc4encrypt.cs.htm

I'm trying to compile this so I can wire it up for use in a windows form
app. (I know .net has builtin support for many crypto algorithms but the
original vbscript version of this RC4 'like' algorithm is already in use for
something which this utiltiy I want to make will be used for.

any input would be appreciated. thanks.
 
J

James

thanks Mark. I'll do that. I just noticed there is one more item this code
is using from the Microsoft.VisualBasic namespace:

Microsoft.VisualBasic.Strings.Chr(cipherby);

also, here is a quote from the article:
"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;
char tmp = (char) i;
Response.Write(tmp.ToString());


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."

I'm all for changing to the c# methods, can you tell me the c# way to do
this one?


Mark Rae said:
This function is used a few times within the class. I am not a real
experienced csharper and literally just installed this c# express edition
to do this little thing... any help would be greatly appreciated. I did
include the 'using Microsoft.VisualBasic;' include directive at the top
to include the namespace.

This is the source code:
http://aspnet.4guysfromrolla.com/code/rc4encrypt.cs.htm

See the following section from that article:

// We now have our character and need to get the ASCII
// code for it. C# doesn't have the VB Asc(), but that
// doesn't mean we can't use it.

IMO, that should read:

// We now have our character and need to get the ASCII
// code for it. C# doesn't have the VB Asc(), because it
// doesn't need it.

1) Remove the reference to the Microsoft.VisualBasic "training wheels"
namespace and forget about it completely.

2) When you need the old VB6 Asc() function, use he following:

//key[a] = Microsoft.VisualBasic.Strings.Asc(ctmp);
key[a] = (int)System.Text.Encoding.ASCII.GetBytes(ctmp)[0];
 
J

James

Thank you, I appreciate it.

Mark Rae said:
1) Remove the reference to the Microsoft.VisualBasic "training wheels"
namespace and forget about it completely.

2) When you need the old VB6 Asc() function, use he following:

//key[a] = Microsoft.VisualBasic.Strings.Asc(ctmp);
key[a] = (int)System.Text.Encoding.ASCII.GetBytes(ctmp)[0];

I'll do that. I just noticed there is one more item this code is using
from the Microsoft.VisualBasic namespace:

Microsoft.VisualBasic.Strings.Chr(cipherby);

byte bytTest = 230;
string strChar = System.Text.Encoding.GetEncoding(1252).GetString(new
byte[] { bytTest });
 
J

John

Mark Rae said:
//key[a] = Microsoft.VisualBasic.Strings.Asc(ctmp);
key[a] = (int)System.Text.Encoding.ASCII.GetBytes(ctmp)[0];

In Reflector it shows the following for the "training wheels" version of
Asc:

public static int Asc(char String)
{
int num;
int num2 = Convert.ToInt32(String);
if (num2 < 0x80)
{
return num2;
}
try
{
byte[] buffer;
Encoding fileIOEncoding = Utils.GetFileIOEncoding();
char[] chars = new char[] { String };
if (fileIOEncoding.IsSingleByte)
{
buffer = new byte[1];
int num3 = fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0);
return buffer[0];
}
buffer = new byte[2];
if (fileIOEncoding.GetBytes(chars, 0, 1, buffer, 0) == 1)
{
return buffer[0];
}
if (BitConverter.IsLittleEndian)
{
byte num4 = buffer[0];
buffer[0] = buffer[1];
buffer[1] = num4;
}
num = BitConverter.ToInt16(buffer, 0);
}
catch (Exception exception)
{
throw exception;
}
return num;
}

Is your code truly equivalent?

J
 
T

Tom Dacon

Mark Rae said:
IMO, that should read:

// We now have our character and need to get the ASCII
// code for it. C# doesn't have the VB Asc(), because it
// doesn't need it.

Strictly speaking, Mark, VB doesn't "need" it either. There's nothing
that prevents a VB coder from using the TextEncoding.ASCII.GetBytes
function.

I suspect that many VB programmers now eschew the use of the
Microsoft.VisualBasic namespace altogether, in favor of using the
..Net framework for everything. I certainly have. Makes it simpler
to take code between languages, and generally the framework
functionality is more flexible and powerful.

Tom Dacon
Dacon Software Consulting
"Smile more, sneer less"
 
J

James

oh, so thats what it meant by assembly reference... good to know. Thank you.

John said:
Hi,

Add a Reference to Microsoft.VisualBasic. Project-->Add Reference.

J

James said:
Hello,

I'm trying to put together a simple utility to do rc4 encryption based on
the rc4 algorithm here:
http://aspnet.4guysfromrolla.com/articles/091802-1.3.aspx

that article is about porting Mike Shaffer's VBScript RC4 to c#... I'm
using that code to make the class but use in a windows form. Using MS
Visual C# Express edition sp1.

so the problem I get when compiling the class is from the use of
Microsoft.VisualBasic.Strings.Asc(), like the following line:

key[a] = Microsoft.VisualBasic.Strings.Asc(ctmp);

The error I get is:
"The type or namespace name 'Strings' does not exist in the namespace
'Microsoft.VisualBasic' (are you missing an assembly reference?)"

this function is used a few times within the class. I am not a real
experienced csharper and literally just installed this c# express edition
to do this little thing... any help would be greatly appreciated. I did
include the 'using Microsoft.VisualBasic;' include directive at the top
to include the namespace.

This is the source code:
http://aspnet.4guysfromrolla.com/code/rc4encrypt.cs.htm

I'm trying to compile this so I can wire it up for use in a windows form
app. (I know .net has builtin support for many crypto algorithms but the
original vbscript version of this RC4 'like' algorithm is already in use
for something which this utiltiy I want to make will be used for.

any input would be appreciated. thanks.
 
J

James

thanks for the input and the link. This little tool I'm trying to make is
for a system that already exists and already uses RC4... otherwise I would
definitely be using something stronger. Changing the original system may
actually be an option but there are 2 issues with that: 1, the encrypting
needs to take place from a wsh/vbscript which as I understand it is not a
good language for writing cipher code, and 2, I'm not at all experienced
with writing cipher code at this point and I would have to do it... and
really, mild security is all my particular situation calls for.

thanks agian. I appreciate the input.
 
J

Jean-Francois Payette

James, did you find the solution? I'm having the same problem, i have a
old system that use RC4 and now i need to make a csharp app that
retreive the info and have to be able to decrypt that.

Thanks
 
D

David Anton

For a character 'c':
System.Convert.ToInt32(c)

For a string 's':
System.Convert.ToInt32(s[0])

ToInt32 might not handle some boundary cases identically to Asc, so if you
really need it you can always reference the Microsoft.VisualBasic dll and use
the Asc method directly.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top