Luig Posted June 20, 2010 Share Posted June 20, 2010 I am trying to convert 2 functions from c# source which are use to encrypt and decrypt packets. This is the code for the 2 functions. /// <summary> /// Encrypts the given packet /// </summary> /// <param name="p">The packet to encrypt</param> public static Packet Encrypt(Packet p) { byte[] buffer = p.GetBuffer(); buffer[0] = (byte)((int)0x76 ^ (int)buffer[0]); for (int i = 1; i != p.Length; i++) buffer[i] = (byte)(buffer[i] ^ buffer[i - 1]); //Packet length byte[] Raw = buffer; byte[] NewRaw = new byte[buffer.Length + 4]; byte[] Length = BitConverter.GetBytes(buffer.Length + 4); Array.Copy(Length, NewRaw, 2); NewRaw[2] = 0x01; NewRaw[3] = 0x00; Array.Copy(Raw, 0, NewRaw, 4, buffer.Length); Raw = new byte[NewRaw.Length]; Array.Copy(NewRaw, Raw, NewRaw.Length); return new Packet(NewRaw); } expandcollapse popup/// <summary> /// Decrypts the given packet /// </summary> /// <param name="p">The packet to decrypt</param> public static Packet Decrypt(Packet p) { byte[] data = p.GetBuffer(); int i = data.Length - 4; byte[] bs = new byte[i]; Array.Copy(data, 4, bs, 0, i); byte b2 = bs[0]; byte b3 = 0; bs[0] = (byte)(bs[0] ^ 118); for (int k = 1; k < data.Length - 4; k++) { byte b1 = b2; b2 = bs[k]; b3 = b2; b3 ^= b1; bs[k] = b3; } return new Packet(bs); /*int i = data.Length - 4; byte[] bs = new byte[i]; Array.Copy(data, 4, bs, 0, i); byte DL = bs[0]; bs[0] = (byte)((int)bs[0] ^ (int)0x76); for (int c = 1; c != i; c++) { byte AL = bs[c]; bs[c] = (byte)(bs[c] ^ DL); DL = AL; } Packet n = new Packet(bs); return n;*/ } The whole source for this section is: expandcollapse popup/* This file is part of LaLore. LaLore is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. LaLore is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with LaLore. If not, see <http://www.gnu.org/licenses/>. */ using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace LaLore { class Packet { private byte[] BufferData; public int Index = 0; public int Length = 0; /// <summary> /// Creates a new buffer /// </summary> /// <param name="Size">Size of buffer</param> public Packet(uint Size) { try { BufferData = new byte[Size]; } catch { BufferData = new byte[Size * 2]; //whatever } Length = BufferData.GetLength(0); } /// <summary> /// Creates a new buffer /// </summary> /// <param name="ArrayOfBytes">The byte array the buffer should use</param> public Packet(byte[] ArrayOfBytes) { BufferData = new byte[ArrayOfBytes.Length]; ArrayOfBytes.CopyTo(BufferData, 0); Length = BufferData.Length; } /// <summary> /// Resizes the buffer /// </summary> /// <param name="Deltasize"></param> private void Resize(int Deltasize) { byte[] newbuffer = new byte[Length + Deltasize]; BufferData.CopyTo(newbuffer, 0); BufferData = newbuffer; Length = BufferData.Length; } /// <summary> /// Returns the buffer data /// </summary> /// <returns>Buffer data</returns> public byte[] GetBuffer() { return BufferData; } /// <summary> /// Returns the buffer data in a byte array /// </summary> /// <returns>The buffer data</returns> public byte[] GetWrittenBuffer() { try { if (Index > Length) { byte[] result = new byte[Index - 1]; System.Buffer.BlockCopy(BufferData, 0, result, 0, Index - 1); return result; } else { byte[] result = new byte[Index]; System.Buffer.BlockCopy(BufferData, 0, result, 0, Index); return result; } } catch (Exception e) { Console.WriteLine(e.ToString()); return new byte[1]; } } /// <summary> /// Sets the index to the given position /// </summary> /// <param name="nIndex">New index</param> /// <returns>True if the operation completed</returns> public bool setIndex(int nIndex) { if (Index > Length) return false; Index = nIndex; return true; } /// <summary> /// Sets the index to current pos + new index /// </summary> /// <param name="nIndex">New index</param> /// <returns>True if operation completed</returns> public bool addIndex(int nIndex) { return setIndex(Index + nIndex); } /// <summary> /// Sets the index to 0 /// </summary> public void resetIndex() { setIndex(0); } //Write methods /// <summary> /// Writes a byte to the buffer /// </summary> /// <param name="Byte">The byte to write</param> public void WriteByte(byte Byte) { if (Index >= Length) Resize((Index - Length) + 1); BufferData[Index] = Byte; ++Index; } /// <summary> /// Writes a byte to the buffer /// </summary> /// <param name="Byte">The byte to write</param> public void WriteByte(int Int) { byte Byte = (byte)Int; if (Index >= Length) Resize((Index - Length) + 1); BufferData[Index] = Byte; ++Index; } /// <summary> /// Writes a short (Int16) to the buffer /// </summary> /// <param name="ShortInteger">The short to write</param> public void WriteShort(short ShortInteger) { if (Index + 2 >= Length) Resize((Index - Length) + 2); WriteByte((byte)(ShortInteger & 0xFF)); WriteByte((byte)((((ushort)ShortInteger) >> 8) & 0xFF)); } /// <summary> /// Writes a short (Int16) to the buffer /// </summary> /// <param name="ShortInteger">The short to write</param> public void WriteShort(int Int) { short ShortInteger = (short)Int; if (Index + 2 >= Length) Resize((Index - Length) + 2); WriteByte((byte)(ShortInteger & 0xFF)); WriteByte((byte)((((ushort)ShortInteger) >> 8) & 0xFF)); } /// <summary> /// Writes an integer to the buffer /// </summary> /// <param name="Integer">The int you want to write</param> public void WriteInt(int Integer) { if (Index + 4 >= Length) Resize((Index - Length) + 4); WriteByte((byte)(Integer & 0xFF)); WriteByte((byte)((((uint)Integer) >> 8) & 0xFF)); WriteByte((byte)((((uint)Integer) >> 16) & 0xFF)); WriteByte((byte)((((uint)Integer) >> 24) & 0xFF)); } public void WriteLong(long Integer) { WriteByte((byte)(Integer & 0xFF)); WriteByte((byte)((((ulong)Integer) >> 8) & 0xFF)); WriteByte((byte)((((ulong)Integer) >> 16) & 0xFF)); WriteByte((byte)((((ulong)Integer) >> 24) & 0xFF)); WriteByte((byte)((((ulong)Integer) >> 32) & 0xFF)); WriteByte((byte)((((ulong)Integer) >> 40) & 0xFF)); WriteByte((byte)((((ulong)Integer) >> 48) & 0xFF)); WriteByte((byte)((((ulong)Integer) >> 56) & 0xFF)); } /// <summary> /// Writes a byte n number of times to the buffer /// </summary> /// <param name="Pad">The byte you want to pad</param> /// <param name="nLength">The number of bytes to write</param> public void BytePad(byte Pad, int nLength) { if ((Index + nLength) >= Length) Resize(nLength); for (int i = 0; i < nLength; i++) { BufferData[Index + i] = Pad; } Index += nLength; } /// <summary> /// Writes a string to the buffer /// </summary> /// <param name="String">The string you want to write</param> public void WriteString(string String) { if ((Index + String.Length) >= Length) Resize(String.Length); for (int i = 0; i < String.Length; i++) { BufferData[Index + i] = (byte)String[i]; } Index += String.Length; } /// <summary> /// Writes a string preceeded by a short containing its length /// </summary> /// <param name="String">The string you want to write</param> public void WriteStringLen(string String) { if ((Index + String.Length + 2) >= Length) Resize(String.Length + 2); WriteShort((short)String.Length); WriteString(String); } /// <summary> /// Writes a padded left string to the buffer /// </summary> /// <param name="String">The string you want to write</param> /// <param name="Pad">The char you want to pad</param> /// <param name="PadLength">The number of chars you want to pad</param> public void WriteStringPaddedLeft(string String, char Pad, int PadLength) { String.PadLeft(PadLength, Pad); if ((Index + String.Length) >= Length) Resize(String.Length); WriteString(String); } /// <summary> /// Writes a padded right string to the buffer /// </summary> /// <param name="String">The string you want to write</param> /// <param name="Pad">The char you want to pad</param> /// <param name="PadLength">The number of chars you want to pad</param> public void WriteStringPaddedRight(string String, char Pad, int PadLength) { String.PadRight(PadLength, Pad); if ((Index + String.Length) >= Length) Resize(String.Length); WriteString(String); } /// <summary> /// Writes an array of bytes to the buffer /// </summary> /// <param name="Array">The array you want to write</param> public void WriteArray(byte[] Array) { if ((Index + Array.Length) >= Length) Resize(Array.Length); for (int i = 0; i < Array.Length; i++) { BufferData[Index + i] = Array[i]; } Index += Array.Length; } /// <summary> /// Write a hex string to the buffer /// </summary> /// <param name="HexString">The string containing the hex chain</param> public void WriteHexString(string HexString) { int _out; byte[] _byte = HexEncoding.GetBytes(HexString, out _out); WriteArray(_byte); } //Read methods /// <summary> /// Reads a byte from the buffer /// </summary> /// <returns>The read byte or 0 if failed</returns> public byte ReadByte() { try { byte result = BufferData[Index]; ++Index; return result; } catch (Exception e) { Console.WriteLine(e.ToString()); ++Index; return 0; } } /// <summary> /// Reads a byte at a certain position in the buffer /// </summary> /// <param name="pos">The position to read from</param> /// <returns>The read byte or 0 if failed</returns> public byte ReadByte(int pos) { int TempIndex = Index; Index = pos; byte result = ReadByte(); Index = TempIndex; return result; } /// <summary> /// Reads an unsigned short from the buffer /// </summary> /// <returns>The read ushort</returns> public ushort ReadUShort() { try { ushort result = (ushort)((BufferData[Index]) + (BufferData[Index + 1] * 256)); Index += 2; return result; } catch (Exception e) { Console.WriteLine(e.ToString()); Index += 2; return 0; } } /// <summary> /// Reads a short from the buffer /// </summary> /// <returns>The read short</returns> public short ReadShort() { int byte1, byte2; byte1 = ReadByte(); byte2 = ReadByte(); return (short)((byte2 << 8) + byte1); } /// <summary> /// Reads a short from the buffer at the given position /// </summary> /// <param name="pos">Position to read at</param> /// <returns>The read short</returns> public short ReadShort(int pos) { int TempIndex = Index; Index = pos; short result = ReadShort(); Index = TempIndex; return result; } /// <summary> /// Reads an integer from the buffer /// </summary> /// <returns>The read int</returns> public int ReadInt() { int byte1 = ReadByte(); int byte2 = ReadByte(); int byte3 = ReadByte(); int byte4 = ReadByte(); return (byte4 << 24) + (byte3 << 16) + (byte2 << 8) + byte1; } /// <summary> /// Reads an unsigned int from the buffer at the given position /// </summary> /// <param name="Pos">The position to read at</param> /// <returns>The read uint</returns> public int ReadInt(int Pos) { int TempIndex = Index; Index = Pos; int result = ReadInt(); Index = TempIndex; return result; } /// <summary> /// Reads a string that is as long as the provided length from the buffer /// </summary> /// <param name="_length">Length of the string to be read</param> /// <returns>The read string</returns> public string ReadString(int _length) { try { char[] newbuf = new char[_length]; for (int i = 0; i < _length; i++) { newbuf[i] = (char)BufferData[Index + i]; } Index += _length; return new string(newbuf); } catch (Exception e) { Console.WriteLine(e.ToString()); return string.Empty; } } /// <summary> /// Reads a string that is as long as the provided length from the buffer /// </summary> /// <param name="_length">Length of the string to be read</param> /// <returns>The read string</returns> public string ReadStringFrom0(int _length) { try { ArrayList a = new ArrayList(); for (int i = 0; i < _length; i++) { if(BufferData[Index + i] != 0x00) a.Add((char)BufferData[Index + i]); } Index += _length; char[] newbuf = new char[a.Count]; for (int i = 0; i < a.Count; i++) newbuf[i] = (char)a[i]; return new string(newbuf); } catch (Exception e) { Console.WriteLine(e.ToString()); return string.Empty; } } /// <summary> /// Reads a string using the short length preceding the string from the buffer /// </summary> /// <returns>The read string</returns> public string ReadStringFromLength() { int _length = ReadShort(); return ReadString(_length); } /// <summary> /// Reads a string using the short length preceding the string from the buffer at the given position /// </summary> /// <param name="Pos">The position to read at</param> /// <returns>The read string</returns> public string ReadStringFromLength(int Pos) { int TempIndex = Index; Index = Pos; string result = ReadStringFromLength(); Index = TempIndex; return result; } /// <summary> /// Reads a string that is as long as the provided length from the buffer at the given position /// </summary> /// <param name="Pos">The position to read at</param> /// <param name="nLength">The length of the string to be read</param> /// <returns>The read string</returns> public string ReadStringSpecify(int Pos, int nLength) { int TempIndex = Index; Index = Pos; string result = ReadString(nLength); Index = TempIndex; return result; } /// <summary> /// Pads the packet /// </summary> /// <param name="b"></param> /// <param name="amount"></param> public void Pad(byte b, int amount) { for (int i = 0; i < amount; i++) WriteByte(b); } /// <summary> /// Encrypts the given packet /// </summary> /// <param name="p">The packet to encrypt</param> public static Packet Encrypt(Packet p) { byte[] buffer = p.GetBuffer(); buffer[0] = (byte)((int)0x76 ^ (int)buffer[0]); for (int i = 1; i != p.Length; i++) buffer[i] = (byte)(buffer[i] ^ buffer[i - 1]); //Packet length byte[] Raw = buffer; byte[] NewRaw = new byte[buffer.Length + 4]; byte[] Length = BitConverter.GetBytes(buffer.Length + 4); Array.Copy(Length, NewRaw, 2); NewRaw[2] = 0x01; NewRaw[3] = 0x00; Array.Copy(Raw, 0, NewRaw, 4, buffer.Length); Raw = new byte[NewRaw.Length]; Array.Copy(NewRaw, Raw, NewRaw.Length); return new Packet(NewRaw); } /// <summary> /// Decrypts the given packet /// </summary> /// <param name="p">The packet to decrypt</param> public static Packet Decrypt(Packet p) { byte[] data = p.GetBuffer(); int i = data.Length - 4; byte[] bs = new byte[i]; Array.Copy(data, 4, bs, 0, i); byte b2 = bs[0]; byte b3 = 0; bs[0] = (byte)(bs[0] ^ 118); for (int k = 1; k < data.Length - 4; k++) { byte b1 = b2; b2 = bs[k]; b3 = b2; b3 ^= b1; bs[k] = b3; } return new Packet(bs); /*int i = data.Length - 4; byte[] bs = new byte[i]; Array.Copy(data, 4, bs, 0, i); byte DL = bs[0]; bs[0] = (byte)((int)bs[0] ^ (int)0x76); for (int c = 1; c != i; c++) { byte AL = bs[c]; bs[c] = (byte)(bs[c] ^ DL); DL = AL; } Packet n = new Packet(bs); return n;*/ } public byte[] ReadBytes(int amt) { byte[] buffer = new byte[amt]; Array.Copy(this.GetBuffer(), Index, buffer, 0, amt); Index += amt; return buffer; } public void Skip(int amt) { Index += amt; } public void Skip() { Index += 1; } } /// <summary> /// Hex encoding class /// </summary> class HexEncoding { /* External code from http://www.codeproject.com/KB/recipes/hexencoding.aspx */ /* Author = neilck http://www.codeproject.com/script/Membership/Profiles.aspx?mid=375133 */ public static bool IsHexDigit(Char c) { int numChar; int numA = Convert.ToInt32('A'); int num1 = Convert.ToInt32('0'); c = Char.ToUpper(c); numChar = Convert.ToInt32(c); if (numChar >= numA && numChar < (numA + 6)) return true; if (numChar >= num1 && numChar < (num1 + 10)) return true; return false; } private static byte HexToByte(string hex) { if (hex.Length > 2 || hex.Length <= 0) throw new ArgumentException("hex must be 1 or 2 characters in length"); byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber); return newByte; } public static byte[] GetBytes(string hexString, out int discarded) { discarded = 0; string newString = string.Empty; char c; // remove all none A-F, 0-9, characters for (int i = 0; i < hexString.Length; i++) { c = hexString[i]; if (IsHexDigit(c)) newString += c; else discarded++; } // if odd number of characters, discard last character if (newString.Length % 2 != 0) { discarded++; newString = newString.Substring(0, newString.Length - 1); } int byteLength = newString.Length / 2; byte[] bytes = new byte[byteLength]; string hex; int j = 0; for (int i = 0; i < bytes.Length; i++) { hex = new String(new Char[] { newString[j], newString[j + 1] }); bytes[i] = HexToByte(hex); j = j + 2; } return bytes; } /* End external code */ } } I have no clue where to start. I am trying my best and will post my progress but suggestions or guidance would help me a lot. Link to comment Share on other sites More sharing options...
Mat Posted June 20, 2010 Share Posted June 20, 2010 Heres a start. I've done everything except the hex class and the encrypt/decrypt expandcollapse popup#cs This file is part of LaLore. LaLore is free software: you can redistribute it and/or modIfy it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. LaLore is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with LaLore. If not, see <http://www.gnu.org/licenses/>. #ce Global $BufferData[1] Global $Index = 0 Global $Length = 0 ; Creates a new buffer ; Size - Size of buffer Func Packet($Size) ReDim $BufferData[$Size] $Length = $Size EndFunc ; Creates a new buffer ; ArrayOfBytes - The byte array the buffer should use Func Packet($ArrayOfBytes) $BufferData = $ArrayOfBytes $Length = UBound($BufferData) EndFunc ; Resizes the buffer ; Deltasize - Func Resize($Deltasize) ReDim $BufferData[$Length + $Deltasize] $Length += $Deltasize EndFunc ; Returns the buffer data ; Returns - Buffer data Func GetBuffer() return $BufferData EndFunc ; Returns the buffer data in a byte array ; Returns - The buffer data Func GetWrittenBuffer() Local $result = $BufferData If ($Index > $Length) Then ReDim $result[$Index - 1] Else ReDim $result[$Index] EndIf Return $result EndFunc ; Sets the index to the given position ; nIndex - New index ; Returns - True If the operation completed Func setIndex($nIndex) If ($Index > $Length) Then Return False $Index = $nIndex Return True EndFunc ; Sets the index to current pos + new index ; nIndex - New index ; Returns - True If operation completed Func addIndex($nIndex) return setIndex($Index + $nIndex) EndFunc ; Sets the index to 0 Func resetIndex() setIndex(0) EndFunc ; Writes a byte to the buffer ; Byte - The byte to write Func WriteByte($Byte) If ($Index >= $Length) Then Resize(($Index - $Length) + 1) $BufferData[$Index] = $Byte $Index += 1 EndFunc ; Writes a short (Int16) to the buffer ; ShortInteger - The short to write Func WriteShort($ShortInteger) If ($Index + 2 >= $Length) Then Resize(($Index - $Length) + 2) WriteByte(BitAND(ShortInteger, 0xFF)) WriteByte(BitAND(BitShift($ShortInteger, 8), 0xFF)) EndFunc ; Writes an integer to the buffer ; Integer - The int you want to write Func WriteInt($Integer) If ($Index + 4 >= $Length) Then Resize(($Index - $Length) + 4) WriteByte(BitAND($Integer, 0xFF)) WriteByte(BitAND(BitShift($Integer, 8), 0xFF)) WriteByte(BitAND(BitShift($Integer, 16), 0xFF)) WriteByte(BitAND(BitShift($Integer, 24), 0xFF)) EndFunc Func WriteLong($Integer) WriteByte(BitAND($Integer, 0xFF)) WriteByte(BitAND(BitShift($Integer, 8), 0xFF)) WriteByte(BitAND(BitShift($Integer, 16), 0xFF)) WriteByte(BitAND(BitShift($Integer, 24), 0xFF)) WriteByte(BitAND(BitShift($Integer, 32), 0xFF)) WriteByte(BitAND(BitShift($Integer, 40), 0xFF)) WriteByte(BitAND(BitShift($Integer, 48), 0xFF)) WriteByte(BitAND(BitShift($Integer, 56), 0xFF)) EndFunc ; Writes a byte n number of times to the buffer ; Pad - The byte you want to pad ; nLength - The number of bytes to write Func BytePad($Pad, $nLength) If (($Index + $nLength) >= $Length) Then Resize($nLength) For $i = 0 To $nLength - 1 $BufferData[$Index + $i] = $Pad Next $Index += $nLength EndFunc ; Writes a string to the buffer ; String - The string you want to write Func WriteString($String) If (($Index + StringLen($String)) >= $Length) Then Resize(StringLen($String)) For $i = 0 To StringLen($String) - 1 $BufferData[$Index + $i] = Asc(StringMid($String, $i, 1)) Next $Index += StringLen($String) EndFunc ; Writes a string preceeded by a short containing its length ; String - The string you want to write Func WriteStringLen($String) If (($Index + StringLen($String) + 2) >= $Length) Then Resize(StringLen($String) + 2) WriteShort(StringLen($String)) WriteString($String) EndFunc ; Writes a padded left string to the buffer ; String - The string you want to write ; Pad - The char you want to pad ; PadLength - The number of chars you want to pad Func WriteStringPaddedLeft($String, $Pad, $PadLength) $String = _StringRepeat($Pad, $PadLength) & $String If (($Index + StringLen($String)) >= $Length) Then Resize(StringLen($String)) WriteString($String) EndFunc ; Writes a padded right string to the buffer ; String - The string you want to write ; Pad - The char you want to pad ; PadLength - The number of chars you want to pad Func WriteStringPaddedRight($String, $Pad, $PadLength) $String &= _StringRepeat($Pad, $PadLength) If (($Index + StringLen($String)) >= $Length) Then Resize(StringLen($String)) WriteString($String) EndFunc ; Writes an array of bytes to the buffer ; Array - The array you want to write Func WriteArray($Array) If (($Index + UBound($Array)) >= $Length) Then Resize(UBound($Array)) For $i = 0 To UBound($Array) - 1 $BufferData[$Index + $i] = $Array[$i] Next $Index += UBound($Array) EndFunc ; Write a hex string to the buffer ; HexString - The string containing the hex chain Func WriteHexString($HexString) Local $_byte = HexEncodingGetBytes($HexString) WriteArray($_byte) EndFunc ; Reads a byte from the buffer ; Returns - The read byte or 0 If failed Func ReadByte() Local $result = $BufferData[$Index]; $Index += 1 return $result EndFunc ; Reads a byte at a certain position in the buffer ; pos - The position to read from ; Returns - The read byte or 0 If failed Func ReadByteAtPos($pos) Local $TempIndex = $Index $Index = $pos Local $result = ReadByte() $Index = $TempIndex return $result EndFunc ; Reads an unsigned short from the buffer ; Returns - The read ushort Func ReadUShort() Local $result = $BufferData[$Index] + ($BufferData[$Index + 1] * 256) $Index += 2 return $result EndFunc ; Reads a short from the buffer ; Returns - The read short Func ReadShort() Local $byte1, $byte2 $byte1 = ReadByte() $byte2 = ReadByte() return BitShift($byte2, -8) + $byte1 EndFunc ; Reads a short from the buffer at the given position ; pos - Position to read at ; Returns - The read short Func ReadShortAtPos($pos) Local $TempIndex = $Index $Index = $pos Local $result = ReadShort() $Index = $TempIndex Return $result EndFunc ; Reads an integer from the buffer ; Returns - The read int Func ReadInt() Local $byte1 = ReadByte() Local $byte2 = ReadByte() Local $byte3 = ReadByte() Local $byte4 = ReadByte() Return BitShift($byte4, -24) + BitShift($byte3, -16) + BitShift($byte2, -8) + $byte1 EndFunc ; Reads an unsigned int from the buffer at the given position ; Pos - The position to read at ; Returns - The read uint Func ReadIntAtPos($Pos) Local $TempIndex = $Index $Index = $Pos Local $result = ReadInt() $Index = $TempIndex Return $result EndFunc ; Reads a string that is as long as the provided length from the buffer ; _length - Length of the string to be read ; Returns - The read string Func ReadString($_length) Local $ret For $i = 0 To $_length - 1 $ret &= Chr($BufferData[$Index + $i]) Next $Index += $_length Return $ret EndFunc ; Reads a string that is as long as the provided length from the buffer ; _length - Length of the string to be read ; Returns - The read string Func ReadStringFrom0($_length) Local $ret For $i = 0 To $_length - 1 If ($BufferData[$Index + $i] <> 0) Then $ret &= Chr($BufferData[$Index + $i]) Next $Index += $_length Return $ret EndFunc ; Reads a string using the short length preceding the string from the buffer ; Returns - The read string Func ReadStringFromLength() Local $_length = ReadShort() Return ReadString($_length) EndFunc ; Reads a string using the short length preceding the string from the buffer at the given position ; Pos - The position to read at ; Returns - The read string Func ReadStringFromLengthAtPos($Pos) Local $TempIndex = $Index $Index = $Pos Local $result = ReadStringFromLength() $Index = $TempIndex Return $result EndFunc ; Reads a string that is as long as the provided length from the buffer at the given position ; Pos - The position to read at ; nLength - The length of the string to be read ; Returns - The read string Func ReadStringSpecIfy($Pos, $nLength) Local $TempIndex = $Index $Index = $Pos Local $result = ReadString($nLength) $Index = $TempIndex Return $result EndFunc ; Pads the packet ; b - ; amount - Func Pad($b, $amount) For $i = 0 To $amount - 1 WriteByte($b) Next EndFunc Func ReadBytes($amt) Local $buffer[$amt] Array.Copy($BufferData, $Index, $buffer, 0, $amt) $Index += $amt return $buffer EndFunc Func Skip($amt) $Index += $amt EndFunc Func Skip1() Index += 1; EndFunc Due to the fact AutoIt does not have classes, there is only one class. Mat AutoIt Project Listing Link to comment Share on other sites More sharing options...
Luig Posted June 22, 2010 Author Share Posted June 22, 2010 Thanks for doing all that, I also found another udf in some foreign site that does the same as this but I am making my own basing it on these 2. Now I still don't understand how the encryption and decryption routines work. I don't understand this: buffer[0] = (byte)((int)0x76 ^ (int)buffer[0]); I dont understand the ^ operator. If someone could explain it to me that would be a great help. It also appears here: b3 ^= b1; In the autoit docs it says its a mathematical operator. Raises a number to the power. e.g. 2 ^ 4 (equals 16) If someone can clear this up or better yet port it I would be grateful. Link to comment Share on other sites More sharing options...
Mat Posted June 22, 2010 Share Posted June 22, 2010 (edited) Its BitNOT. My bad, its BitXOR... Edited June 22, 2010 by Mat AutoIt Project Listing Link to comment Share on other sites More sharing options...
Luig Posted June 22, 2010 Author Share Posted June 22, 2010 Ok now I think I ported the Array.Copy correctly and will try to port the encrypt function first.Heres how I made the array copy function although I am going to add an array size check to re size the array if its to small.Func _Array_Copy(ByRef $Source, $SourceIndex, ByRef $DestArray, $DestIndex, $Length) Local $TempIndex = $DestIndex For $i = $SourceIndex to ($Length + $SourceIndex) - 1 $DestArray[$TempIndex] = $SourceIndex[$i] $TempIndex += 1 Next EndFuncI got the info for the function here. Link to comment Share on other sites More sharing options...
Luig Posted June 22, 2010 Author Share Posted June 22, 2010 Ok I ported the decrypt function like so: Func DecryptPacket($Packet) Local $Data = $Packet, $b1, $b2, $b3 Local $i = UBound($Data) - 4 Dim $bs[$i] _Array_Copy($Data, 4, $bs, 0, $i) $b2 = $bs[0] $b3 = 0 $bs[0] = BitXOR($TempPacket[0], 0x76) ; decimal 118 = hex 0x76 For $k = 1 to $i $b1 = $b2 $b2 = $bs[$k] $b3 = $b2 $b3 = BitXOR($b3, $b) $bs[$k] = $b3 Next Return $bs EndFunc Am I right, I'll try to decrypt a packet right now though. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now