Advertisement
Guest User

Decode / Encode MythicRC4Packet

a guest
Dec 7th, 2010
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1.         public static byte[] DecodeMythicRC4Packet(byte[] buf, byte[] sbox)
  2.         {
  3.             if (buf == null) return null;
  4.             if (sbox == null) return null;
  5.             byte[] tmpsbox = new byte[256];
  6.             Array.Copy(sbox, 0, tmpsbox, 0, sbox.Length);
  7.             byte i = 0;
  8.             byte j = 0;
  9.             ushort len = (ushort)((buf[0] << 8) | buf[1] + 10); //+10 byte for packet#,session,param,code,checksum
  10.             int k;
  11.             for (k = (len / 2) + 2; k < len + 2; k++)
  12.             {
  13.                 i++;
  14.                 byte tmp = tmpsbox[i];
  15.                 j += tmp;
  16.                 tmpsbox[i] = tmpsbox[j];
  17.                 tmpsbox[j] = tmp;
  18.                 byte xorKey = tmpsbox[(byte)(tmpsbox[i] + tmpsbox[j])];
  19.                 buf[k] ^= xorKey;
  20.                 j += buf[k];
  21.             }
  22.             for (k = 2; k < (len / 2) + 2; k++)
  23.             {
  24.                 i++;
  25.                 byte tmp = tmpsbox[i];
  26.                 j += tmp;
  27.                 tmpsbox[i] = tmpsbox[j];
  28.                 tmpsbox[j] = tmp;
  29.                 byte xorKey = tmpsbox[(byte)(tmpsbox[i] + tmpsbox[j])];
  30.                 buf[k] ^= xorKey;
  31.                 j += buf[k];
  32.             }
  33.             return buf;
  34.         }
  35.  
  36.         //From DOL
  37.         public static byte[] EncodeMythicRC4Packet(byte[] buf, byte[] sbox, bool udpPacket)
  38.         {
  39.             if (buf == null) return null;
  40.             if (sbox == null) return null;
  41.             byte[] tmpsbox = new byte[256];
  42.             Array.Copy(sbox, 0, tmpsbox, 0, sbox.Length);
  43.             byte i = 0;
  44.             byte j = 0;
  45.             ushort len = (ushort)((buf[0] << 8) | buf[1]);
  46.             len += 1; // +1 byte for packet code
  47.             if (udpPacket)
  48.                 len += 2; //+2 byte for packet-count
  49.  
  50.             int k;
  51.             for (k = (len / 2) + 2; k < len + 2; k++)
  52.             {
  53.                 i++;
  54.                 byte tmp = tmpsbox[i];
  55.                 j += tmp;
  56.                 tmpsbox[i] = tmpsbox[j];
  57.                 tmpsbox[j] = tmp;
  58.                 byte xorKey = tmpsbox[(byte)(tmpsbox[i] + tmpsbox[j])];
  59.                 j += buf[k];
  60.                 buf[k] ^= xorKey;
  61.             }
  62.             for (k = 2; k < (len / 2) + 2; k++)
  63.             {
  64.                 i++;
  65.                 byte tmp = tmpsbox[i];
  66.                 j += tmp;
  67.                 tmpsbox[i] = tmpsbox[j];
  68.                 tmpsbox[j] = tmp;
  69.                 byte xorKey = tmpsbox[(byte)(tmpsbox[i] + tmpsbox[j])];
  70.                 j += buf[k];
  71.                 buf[k] ^= xorKey;
  72.             }
  73.             return buf;
  74.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement