RC4 StrToHex function

J

Jim

I'm slowly switching over from VB6 to C# and need help converting an
encrypted RC4 string to hex. The function below works perfectly within my
vb6 app but I'm having difficulty finding a replacement written in C#.
Converting to C# is beyond my skillset at the moment. Where can I find the
same function in C#?

Thank you

Function StrToHex(Text As String, Optional Separator As String = " ") As
String

Dim a As Long
Dim Pos As Long
Dim Char As Byte
Dim PosAdd As Long
Dim ByteSize As Long
Dim ByteArray() As Byte
Dim ByteReturn() As Byte
Dim SeparatorLen As Long
Dim SeparatorChar As Byte

'Initialize the hex routine
If (Not m_InitHex) Then Call InitHex

'Initialize variables
SeparatorLen = Len(Separator)

'Create the destination bytearray, this
'will be converted to a string later
ByteSize = (Len(Text) * 2 + (Len(Text) - 1) * SeparatorLen)
ReDim ByteReturn(ByteSize - 1)
Call FillMemory(ByteReturn(0), ByteSize, Asc(Separator))

'We convert the source string into a
'byte array to speed this up a tad
ByteArray() = StrConv(Text, vbFromUnicode)

'Now convert every character to
'it's equivalent HEX code
PosAdd = 2 + SeparatorLen
For a = 0 To (Len(Text) - 1)
ByteReturn(Pos) = m_ByteToHex(ByteArray(a), 0)
ByteReturn(Pos + 1) = m_ByteToHex(ByteArray(a), 1)
Pos = Pos + PosAdd
Next

'Convert the bytearray to a string
StrToHex = StrConv(ByteReturn(), vbUnicode)

End Function


Private Sub InitHex()

Dim a As Long
Dim b As Long
Dim HexBytes() As Byte
Dim HexString As String

'The routine is initialized
m_InitHex = True

'Create a string with all hex values
HexString = String$(512, "0")
For a = 1 To 255
Mid$(HexString, 1 + a * 2 + -(a < 16)) = Hex(a)
Next
HexBytes = StrConv(HexString, vbFromUnicode)

'Create the Str->Hex array
For a = 0 To 255
m_ByteToHex(a, 0) = HexBytes(a * 2)
m_ByteToHex(a, 1) = HexBytes(a * 2 + 1)
Next

'Create the Str->Hex array
For a = 0 To 255
m_HexToByte(m_ByteToHex(a, 0), m_ByteToHex(a, 1)) = a
Next

End Sub
 
A

Arne Vajhøj

Jim said:
I'm slowly switching over from VB6 to C# and need help converting an
encrypted RC4 string to hex. The function below works perfectly within my
vb6 app but I'm having difficulty finding a replacement written in C#.
Converting to C# is beyond my skillset at the moment. Where can I find the
same function in C#?

There are plenty of ways to generate string with hex from
byte array.

See below for 6 different implementations !

All of them are significant shorter than your VB6 code.

They don't have the separator but they should all
be easy to modify to use a separator.

Arne

=========================================

public class HexFun
{
private static char[] HEXDIGITS = "0123456789ABCDEF".ToCharArray();
public static string ToHex1(byte[] b)
{
return BitConverter.ToString(b).Replace("-", "");
}
public static string ToHex2(byte[] b)
{
StringBuilder sb = new StringBuilder(2 * b.Length);
for(int i = 0; i < b.Length; i++)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
public static string ToHex3(byte[] b)
{
StringBuilder sb = new StringBuilder(2 * b.Length);
for(int i = 0; i < b.Length; i++)
{
sb.Append(HEXDIGITS[b / 16]);
sb.Append(HEXDIGITS[b % 16]);
}
return sb.ToString();
}
public static string ToHex4(byte[] b)
{
StringBuilder sb = new StringBuilder(2 * b.Length);
for(int i = 0; i < b.Length; i++)
{
sb.Append(HEXDIGITS[b >> 4]);
sb.Append(HEXDIGITS[b & 0x0F]);
}
return sb.ToString();
}
public static string ToHex5(byte[] b)
{
char[] res = new char[2 * b.Length];
for(int i = 0; i < b.Length; i++)
{
res[2 * i] = HEXDIGITS[b >> 4];
res[2 * i + 1] = HEXDIGITS[b & 0x0F];
}
return new string(res);
}
private static char[] UH;
private static char[] LH;
static HexFun()
{
UH = new char[256];
LH = new char[256];
for(int i = 0; i < 256; i++)
{
UH = HEXDIGITS[i / 16];
LH = HEXDIGITS[i % 16];
}
}
public static string ToHex6(byte[] b)
{
char[] res = new char[2 * b.Length];
for(int i = 0; i < b.Length; i++)
{
res[2 * i] = UH[b];
res[2 * i + 1] = LH[b];
}
return new string(res);
}
}
 

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