Example #1
0
        public static void EncryptionKeyRequest(MinecraftClient client, IPacket _packet)
        {
            var packet = (EncryptionKeyRequestPacket)_packet;
            var random = RandomNumberGenerator.Create();

            client.SharedSecret = new byte[16];
            random.GetBytes(client.SharedSecret); // Generate a secure AES key

            if (packet.ServerId != "-")           // Online mode
            {
                // Authenticate with minecraft.net
                if (!client.Session.OnlineMode)
                {
                    throw new AuthenticationException("Server is in online mode, but client is in offline mode.");
                }
                var data = Encoding.ASCII.GetBytes(packet.ServerId)
                           .Concat(client.SharedSecret)
                           .Concat(packet.PublicKey).ToArray();
                var    hash      = Cryptography.JavaHexDigest(data);
                var    webClient = new WebClient();
                string result    = webClient.DownloadString("http://session.minecraft.net/game/joinserver.jsp?user="******"&sessionId=" + Uri.EscapeUriString(client.Session.SessionId) +
                                                            "&serverId=" + Uri.EscapeUriString(hash));
                if (result != "OK")
                {
                    // TODO
                }
            }

            var parser = new AsnKeyParser(packet.PublicKey);
            var key    = parser.ParseRSAPublicKey();

            // Encrypt shared secret and verification token
            var crypto = new RSACryptoServiceProvider();

            crypto.ImportParameters(key);
            var encryptedSharedSecret = crypto.Encrypt(client.SharedSecret, false);
            var encryptedVerification = crypto.Encrypt(packet.VerificationToken, false);
            var response = new EncryptionKeyResponsePacket(encryptedSharedSecret, encryptedVerification);

            client.SendPacket(response);
            client.FlushPackets();
            client.NetworkManager.BaseStream = new AesStream(client.NetworkStream, client.SharedSecret);
        }
Example #2
0
        public static void EncryptionKeyRequest(MinecraftClient client, IPacket _packet)
        {
            var packet = (EncryptionKeyRequestPacket)_packet;
            var random = RandomNumberGenerator.Create();
            client.SharedSecret = new byte[16];
            random.GetBytes(client.SharedSecret); // Generate a secure AES key

            if (packet.ServerId != "-") // Online mode
            {
                // Authenticate with minecraft.net
                if (!client.Session.OnlineMode)
                    throw new AuthenticationException("Server is in online mode, but client is in offline mode.");
                var data = Encoding.ASCII.GetBytes(packet.ServerId)
                    .Concat(client.SharedSecret)
                    .Concat(packet.PublicKey).ToArray();
                var hash = Cryptography.JavaHexDigest(data);
                var webClient = new WebClient();
                string result = webClient.DownloadString("http://session.minecraft.net/game/joinserver.jsp?user="******"&sessionId=" + Uri.EscapeUriString(client.Session.SessionId) +
                    "&serverId=" + Uri.EscapeUriString(hash));
                if (result != "OK")
                {
                    // TODO
                }
            }

            var parser = new AsnKeyParser(packet.PublicKey);
            var key = parser.ParseRSAPublicKey();

            // Encrypt shared secret and verification token
            var crypto = new RSACryptoServiceProvider();
            crypto.ImportParameters(key);
            var encryptedSharedSecret = crypto.Encrypt(client.SharedSecret, false);
            var encryptedVerification = crypto.Encrypt(packet.VerificationToken, false);
            var response = new EncryptionKeyResponsePacket(encryptedSharedSecret, encryptedVerification);
            client.SendPacket(response);
            client.FlushPackets();
            client.NetworkManager.BaseStream = new AesStream(client.NetworkStream, client.SharedSecret);
        }