/// <summary> /// Creates a new instance of a Session /// </summary> /// <param name="socket"> Socket connection of the session </param> /// <param name="recv"> Recive crypto of the session </param> /// <param name="send"> Send crypto of the session </param> public Session(Socket socket, MapleCrypto recv, MapleCrypto send) { this.Socket = socket; this.ReceiveCrypto = recv; this.SendCrypto = send; this.Client = new MapleClient(this); }
/// <summary> /// Encrypt data using MapleStory's AES method /// </summary> /// <param name="IV"> IV to use for encryption </param> /// <param name="data"> data to encrypt </param> /// <param name="length"> length of data </param> /// <param name="key"> the AES key to use </param> /// <returns> Crypted data </returns> public static byte[] AESCrypt(byte[] IV, byte[] data, int length, byte[] key) { AesManaged crypto = new AesManaged(); crypto.KeySize = 256; //in bits crypto.Key = key; crypto.Mode = CipherMode.ECB; // Should be OFB, but this works too MemoryStream memStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memStream, crypto.CreateEncryptor(), CryptoStreamMode.Write); int remaining = length; int llength = 0x5B0; int start = 0; while (remaining > 0) { byte[] myIV = MapleCrypto.MultiplyBytes(IV, 4, 4); if (remaining < llength) { llength = remaining; } for (int x = start; x < (start + llength); x++) { if ((x - start) % myIV.Length == 0) { cryptoStream.Write(myIV, 0, myIV.Length); byte[] newIV = memStream.ToArray(); Array.Copy(newIV, myIV, myIV.Length); memStream.Position = 0; } data[x] ^= myIV[(x - start) % myIV.Length]; } start += llength; remaining -= llength; llength = 0x5B4; } try { cryptoStream.Dispose(); memStream.Dispose(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } return(data); }
/// <summary> /// Client connected handler /// </summary> /// <param name="iar"> The IAsyncResult </param> private void OnClientConnect(IAsyncResult iar) { try { Socket socket = Listener.EndAccept(iar); byte[] ivRecv = {70, 114, 122, 82}; byte[] ivSend = {82, 48, 120, 115}; MapleCrypto recvCrypto = new MapleCrypto(ivRecv); MapleCrypto sendCrypto = new MapleCrypto(ivSend); Session session = new Session(socket, recvCrypto, sendCrypto); session.Begin(ivRecv, ivSend); Listener.BeginAccept(OnClientConnect, null); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }