3
3
using System . IO . Compression ;
4
4
using System . Security . Cryptography ;
5
5
using System . Text ;
6
+ using System . Xml ;
6
7
7
8
namespace SPHDecode . Implementations
8
9
{
@@ -48,7 +49,7 @@ public static string Decrypt(byte[] clearText)
48
49
if ( response . EndsWith ( "\0 " ) )
49
50
response = response . Substring ( 0 , response . Length - 1 ) ;
50
51
51
- if ( IsXML ( response ) )
52
+ if ( IsValidXML ( response ) )
52
53
{
53
54
return response ;
54
55
}
@@ -64,14 +65,14 @@ public static byte[] Enecrypt(string data)
64
65
{
65
66
byte [ ] response = null ;
66
67
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 ) )
68
72
{
69
73
// TODO: show error message
70
74
}
71
75
72
- if ( data . EndsWith ( "\0 " ) . Equals ( false ) )
73
- data = string . Concat ( data , "\0 " ) ;
74
-
75
76
byte [ ] clearText = Encoding . UTF8 . GetBytes ( data ) ;
76
77
77
78
try
@@ -108,14 +109,23 @@ public static byte[] Enecrypt(string data)
108
109
return response ;
109
110
}
110
111
111
- public static bool IsXML ( string xml )
112
+ private static bool IsValidXML ( string value )
112
113
{
113
- string xmlHeader = "<?xml version=\" 1.0\" ?>" ;
114
+ if ( string . IsNullOrWhiteSpace ( value ) )
115
+ return false ;
114
116
115
- if ( xml . Substring ( 0 , xmlHeader . Length ) . Equals ( xmlHeader ) )
116
- return true ;
117
+ try
118
+ {
119
+ XmlDocument xmlDoc = new XmlDocument ( ) ;
117
120
118
- return false ;
121
+ xmlDoc . LoadXml ( value ) ;
122
+
123
+ return true ;
124
+ }
125
+ catch ( XmlException )
126
+ {
127
+ return false ;
128
+ }
119
129
}
120
130
121
131
public static string DecompressData ( byte [ ] data )
0 commit comments