RC4 StrToHex function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim

    RC4 StrToHex function

    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(Byte Size - 1)
    Call FillMemory(Byte Return(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(Byt eArray(a), 0)
    ByteReturn(Pos + 1) = m_ByteToHex(Byt eArray(a), 1)
    Pos = Pos + PosAdd
    Next

    'Convert the bytearray to a string
    StrToHex = StrConv(ByteRet urn(), 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(HexStri ng, 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_B yteToHex(a, 0), m_ByteToHex(a, 1)) = a
    Next

    End Sub


  • Peter Duniho

    #2
    Re: RC4 StrToHex function

    On Wed, 29 Oct 2008 17:29:44 -0700, Jim <no_address@nod omainname.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.Le ngth) *
    rgb.Length);

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

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

    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

    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: RC4 StrToHex function

      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 = "0123456789ABCD EF".ToCharArray ();
      public static string ToHex1(byte[] b)
      {
      return BitConverter.To String(b).Repla ce("-", "");
      }
      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(HEXDI GITS[b[i] / 16]);
      sb.Append(HEXDI GITS[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(HEXDI GITS[b[i] >4]);
      sb.Append(HEXDI GITS[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);
      }
      }

      Comment

      Working...