Skip to content

Commit 11f43a9

Browse files
committed
add better xml validation method
1 parent 50b5e32 commit 11f43a9

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

SPHDecode/Implementations/Cryptography.cs

+20-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO.Compression;
44
using System.Security.Cryptography;
55
using System.Text;
6+
using System.Xml;
67

78
namespace SPHDecode.Implementations
89
{
@@ -48,7 +49,7 @@ public static string Decrypt(byte[] clearText)
4849
if (response.EndsWith("\0"))
4950
response = response.Substring(0, response.Length - 1);
5051

51-
if (IsXML(response))
52+
if (IsValidXML(response))
5253
{
5354
return response;
5455
}
@@ -64,14 +65,14 @@ public static byte[] Enecrypt(string data)
6465
{
6566
byte[] response = null;
6667

67-
if (IsXML(data).Equals(false))
68+
if (data.EndsWith("\0").Equals(false))
69+
data = string.Concat(data, "\0");
70+
71+
if (IsValidXML(data).Equals(false))
6872
{
6973
// TODO: show error message
7074
}
7175

72-
if (data.EndsWith("\0").Equals(false))
73-
data = string.Concat(data, "\0");
74-
7576
byte[] clearText = Encoding.UTF8.GetBytes(data);
7677

7778
try
@@ -108,14 +109,23 @@ public static byte[] Enecrypt(string data)
108109
return response;
109110
}
110111

111-
public static bool IsXML(string xml)
112+
private static bool IsValidXML(string value)
112113
{
113-
string xmlHeader = "<?xml version=\"1.0\" ?>";
114+
if (string.IsNullOrWhiteSpace(value))
115+
return false;
114116

115-
if (xml.Substring(0, xmlHeader.Length).Equals(xmlHeader))
116-
return true;
117+
try
118+
{
119+
XmlDocument xmlDoc = new XmlDocument();
117120

118-
return false;
121+
xmlDoc.LoadXml(value);
122+
123+
return true;
124+
}
125+
catch (XmlException)
126+
{
127+
return false;
128+
}
119129
}
120130

121131
public static string DecompressData(byte[] data)

0 commit comments

Comments
 (0)