From 84ba8fd0aea38bb49e6aab73028a9ebabcd0b889 Mon Sep 17 00:00:00 2001 From: ORelio Date: Mon, 2 Jun 2014 11:21:05 +0200 Subject: [PATCH] Fix special chars in minecraft passwords Bug report by TNT-UP --- MinecraftClient/Protocol/ProtocolHandler.cs | 27 ++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs index 41427e98..503614c4 100644 --- a/MinecraftClient/Protocol/ProtocolHandler.cs +++ b/MinecraftClient/Protocol/ProtocolHandler.cs @@ -100,7 +100,7 @@ namespace MinecraftClient.Protocol try { string result = ""; - string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + user + "\", \"password\": \"" + pass + "\" }"; + string json_request = "{\"agent\": { \"name\": \"Minecraft\", \"version\": 1 }, \"username\": \"" + jsonEncode(user) + "\", \"password\": \"" + jsonEncode(pass) + "\" }"; int code = doHTTPSPost("authserver.mojang.com", "/authenticate", json_request, ref result); if (code == 200) { @@ -204,5 +204,30 @@ namespace MinecraftClient.Protocol } else return 520; //Web server is returning an unknown error } + + /// + /// Encode a string to a json string. + /// Will convert special chars to \u0000 unicode escape sequences. + /// + /// Source text + /// Encoded text + + private static string jsonEncode(string text) + { + StringBuilder result = new StringBuilder(); + foreach (char c in text) + { + if (char.IsLetterOrDigit(c)) + { + result.Append(c); + } + else + { + result.Append("\\u"); + result.Append(((int)c).ToString("x4")); + } + } + return result.ToString(); + } } }