From 295dfe717e4ca7a753bb3199d7f8a0341731d4b1 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Mon, 17 Aug 2015 11:01:28 -0700 Subject: [PATCH] Fixed issues with passwords containing unicode special characters. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The main fix is the change to ProtocolHandler's jsonEncode method. Previously, it used 'char.IsLetterOrDigit' to see if it needed to be escaped, but some chars, such as "Ð", count as a letter but still need to be escaped. The fix is to check if it's in the right range, rather than using that method. There's also some changes to those methods for performance and clarity reasons. Most of this is using a StringBuilder rather than appending to the string. Not too important, but it makes things clearer. --- MinecraftClient/ConsoleIO.cs | 20 +++++++++----------- MinecraftClient/Protocol/ProtocolHandler.cs | 8 +++++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/MinecraftClient/ConsoleIO.cs b/MinecraftClient/ConsoleIO.cs index 34bb7a35..07c67719 100644 --- a/MinecraftClient/ConsoleIO.cs +++ b/MinecraftClient/ConsoleIO.cs @@ -45,22 +45,18 @@ namespace MinecraftClient public static string ReadPassword() { - string password = ""; - ConsoleKeyInfo k = new ConsoleKeyInfo(); - while (k.Key != ConsoleKey.Enter) + StringBuilder password = new StringBuilder(); + + ConsoleKeyInfo k; + while ((k = Console.ReadKey(true)).Key != ConsoleKey.Enter) { - k = Console.ReadKey(true); switch (k.Key) { - case ConsoleKey.Enter: - Console.Write('\n'); - return password; - case ConsoleKey.Backspace: if (password.Length > 0) { Console.Write("\b \b"); - password = password.Substring(0, password.Length - 1); + password.Remove(password.Length - 1, 1); } break; @@ -79,12 +75,14 @@ namespace MinecraftClient if (k.KeyChar != 0) { Console.Write('*'); - password += k.KeyChar; + password.Append(k.KeyChar); } break; } } - return password; + + Console.WriteLine(); + return password.ToString(); } /// diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs index 11a0873f..624d6a7f 100644 --- a/MinecraftClient/Protocol/ProtocolHandler.cs +++ b/MinecraftClient/Protocol/ProtocolHandler.cs @@ -285,16 +285,18 @@ namespace MinecraftClient.Protocol StringBuilder result = new StringBuilder(); foreach (char c in text) { - if (char.IsLetterOrDigit(c)) + if ((c >= '0' && c <= '9') || + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z')) { result.Append(c); } else { - result.Append("\\u"); - result.Append(((int)c).ToString("x4")); + result.AppendFormat(@"\u{0:x4}", (int)c); } } + return result.ToString(); } }