Skip to content

Commit

Permalink
add better xml validation method
Browse files Browse the repository at this point in the history
  • Loading branch information
Stricted committed Jan 19, 2016
1 parent 50b5e32 commit 11f43a9
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions SPHDecode/Implementations/Cryptography.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using System.Xml;

namespace SPHDecode.Implementations
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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 = "<?xml version=\"1.0\" ?>";
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)
Expand Down

0 comments on commit 11f43a9

Please sign in to comment.