diff --git a/SPHDecode/Implementations/Cryptography.cs b/SPHDecode/Implementations/Cryptography.cs index 4c930d9..86950f5 100644 --- a/SPHDecode/Implementations/Cryptography.cs +++ b/SPHDecode/Implementations/Cryptography.cs @@ -3,6 +3,7 @@ using System.IO.Compression; using System.Security.Cryptography; using System.Text; +using System.Xml; namespace SPHDecode.Implementations { @@ -48,7 +49,7 @@ public static string Decrypt(byte[] clearText) if (response.EndsWith("\0")) response = response.Substring(0, response.Length - 1); - if (IsXML(response)) + if (IsValidXML(response)) { return response; } @@ -64,14 +65,14 @@ public static byte[] Enecrypt(string data) { byte[] response = null; - if (IsXML(data).Equals(false)) + if (data.EndsWith("\0").Equals(false)) + data = string.Concat(data, "\0"); + + if (IsValidXML(data).Equals(false)) { // TODO: show error message } - if (data.EndsWith("\0").Equals(false)) - data = string.Concat(data, "\0"); - byte[] clearText = Encoding.UTF8.GetBytes(data); try @@ -108,14 +109,23 @@ public static byte[] Enecrypt(string data) return response; } - public static bool IsXML(string xml) + private static bool IsValidXML(string value) { - string xmlHeader = ""; + if (string.IsNullOrWhiteSpace(value)) + return false; - if (xml.Substring(0, xmlHeader.Length).Equals(xmlHeader)) - return true; + try + { + XmlDocument xmlDoc = new XmlDocument(); - return false; + xmlDoc.LoadXml(value); + + return true; + } + catch (XmlException) + { + return false; + } } public static string DecompressData(byte[] data)