[Mono-devel-list] Can you help me in this conversion from VB to C# ?

Alessandro Torrisi alessandro.torrisi at eurone.it
Mon Sep 20 19:08:33 EDT 2004


Hi ! Some times ago I posted a message here and someone of you helped me to 
convert a function from VB.NET to C#... well I said at that time that 
everything went ok ! Well I was wrong...this function is at the base of the 
login algorythm for a Direct Connect Server and at that time I tryied the new 
C# function on a server, on which the key check was not enabled !!! So I 
return here...with my function...I put here all the functions, the first is 
mine, I think the most un-correct...then a C# function given by someone of 
you...excuse me I don't remember the name (could be Rafael Teixeira ?) ! and 
the latest the perfect function in VB.NET...

Where are the errors ? I'm going mad ! Please help me...Alex...

***************************************************************************************************************************

public static string LockToKey(string Lock) {
  string LockToKey;
  int h, j, n;
  n = 5;
  
  h = Lock.IndexOf(" ");
  if (h!=-1) {
   Lock = Lock.Substring(0,h-1);
  }
  
  h = Lock.ToCharArray()[0] ^ Lock.ToCharArray()[Lock.Length-1] ^ 
Lock.ToCharArray()[Lock.Length-2] ^ n;
  h = (h/16) ^ (h*16);
  
  while (h>255) {
   h = h - 256;
  }
  
  switch (h) {
   case 0:
    LockToKey = "/%DCN000%/";
    break;
   case 5:
    LockToKey = "/%DCN005%/";
    break;
   case 36:
    LockToKey = "/%DCN036%/";
    break;
   case 96:
    LockToKey = "/%DCN096%/";
    break;
   case 124:
    LockToKey = "/%DCN124%/";
    break;
   case 126:
    LockToKey = "/%DCN126%/";
    break;
   default:
    LockToKey = Convert.ToString((char)h);
    break;
  }
  
  for (j=2;j<=Lock.Length;j++) {
   h = Lock.ToCharArray()[j] ^ Lock.ToCharArray()[j-1];
   h = (h/16) ^ (h*16);
   
   while (h>255) {
    h = h - 256;
   }
   

   switch (h) {
    case 0:
     LockToKey = LockToKey + "/%DCN000%/";
     break;
    case 5:
     LockToKey = LockToKey + "/%DCN005%/";
     break;
    case 36:
     LockToKey = LockToKey + "/%DCN036%/";
     break;
    case 96:
     LockToKey = LockToKey + "/%DCN096%/";
     break;
    case 124:
     LockToKey = LockToKey + "/%DCN124%/";
     break;
    case 126:
     LockToKey = LockToKey + "/%DCN126%/";
     break;
    default:
     LockToKey = LockToKey + Convert.ToString((char)h);
     break;
   }
   
   
  }
   
  return LockToKey;
 }

******************************************************************************************************************************
 private static int Xor(params char[] values)
 {
         int h = (int)values[0];
         for (int j = 1; j < values.Length; j++)
                 h ^= (int)values[j];
         return h;
 }

 private static string AssemblePart(int h)
 {
     h = ((h / 16) ^ (h * 16)) & 255;
     switch (h)
     {
      case 0: case 5: case 36: case 96: case 124: case 126:
        return String.Format("/%DCN{0:000}%/", h);
       default:
           System.Text.StringBuilder sb = new System.Text.StringBuilder(1);
           sb.Append((char)h);
           return sb.ToString();
     }
 }

 public static string LockToKey(string Lck)
 {
  Lck = Lck.Split(' ')[0];

  string result = AssemblePart(Xor(Lck[0], 
Lck[Lck.Length-1],Lck[Lck.Length-2], (char)5));

  for(int j = 1; j < Lck.Length; j++)
         result = result + AssemblePart(Xor(Lck[j], Lck[j - 1]));

  return result;
 }

*******************************************************************************************************************************
Public Function LockToKey(ByVal Lck As String) As String
        Dim h As Integer, j As Integer, n As Integer
        n = 5

        h = InStr(1, Lck, " ")
        If h Then Lck = Left$(Lck, h - 1)

        h = Asc(Lck) Xor Asc(Right$(Lck, 1)) Xor Asc(Right$(Lck, 2)) Xor n
        h = (h \ 16) Xor (h * 16)

        Do While h > 255
            h = h - 256
        Loop

        Select Case h
            Case 0, 5, 36, 96, 124, 126
                LockToKey = "/%DCN" & Right$("00" & CStr(h), 3) & "%/"
            Case Else
                LockToKey = Chr(h)
        End Select

        For j = 2 To Len(Lck)
            h = Asc(Mid$(Lck, j, 1)) Xor Asc(Mid$(Lck, j - 1, 1))
            h = (h \ 16) Xor (h * 16)

            Do While h > 255
                h = h - 256
            Loop

            Select Case h
                Case 0, 5, 36, 96, 124, 126
                    LockToKey = LockToKey & "/%DCN" & Right$("00" & CStr(h), 
3) & "%/"
                Case Else
                    LockToKey = LockToKey & Chr(h)
            End Select
        Next
    End Function

*****************************************************************************************************************************

That's all...please help me !!!



More information about the Mono-devel-list mailing list