473,394 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

RC4 StrToHex function

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
Oct 30 '08 #1
2 4366
On Wed, 29 Oct 2008 17:29:44 -0700, Jim <no********@nodomainname.com>
wrote:
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#?
I'm not convinced that you really want an exact duplicate of that
implementation anyway.

If I understand the code correctly, it is basically taking an array of
bytes, passed as a string, and returning a new string that is the
hexadecimal representation of the input array, with each byte separated by
some arbitrary string passed in.

Interestingly, unless I've misunderstood the code, it looks to me as
though while the code accounts for a multi-character separator, it will
only ever use the first character passed in. That is, you could pass in
something like "-*-" and you would indeed get three characters between
each byte as text, but the characters would always be "-".

Anyway, ignoring that one point, a simple C# version of that functionality
might be something like this:

string StrToHex(byte[] rgb, string strSeparator)
{
StringBuilder sb = new StringBuilder((2 + strSeparator.Length) *
rgb.Length);

foreach (byte b in rgb)
{
sb.AppendFormat("{0:X2}{1}", b, strSeparator);
}

return sb.ToString(0, sb.Length - strSeparator.Length);
}

Using the StringBuilder is useful because it allows you to preallocate the
storage needed for the string. The method just creates the StringBuilder
with the needed space, then appends to the current string the formatted
byte value along with the separator, and then returns the final result
minus the last separator that had been appended.

Pete
Oct 30 '08 #2
Jim wrote:
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[i].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[i] / 16]);
sb.Append(HEXDIGITS[b[i] % 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[i] >4]);
sb.Append(HEXDIGITS[b[i] & 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[i] >4];
res[2 * i + 1] = HEXDIGITS[b[i] & 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[i] = HEXDIGITS[i / 16];
LH[i] = 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[i]];
res[2 * i + 1] = LH[b[i]];
}
return new string(res);
}
}
Oct 30 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.