Skip to content

Commit

Permalink
move some stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
Stricted committed Jan 20, 2016
1 parent 6c34e91 commit 20419cc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 72 deletions.
100 changes: 28 additions & 72 deletions SPHDecode/Implementations/Cryptography.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,9 @@ public static string Decrypt(byte[] clearText)

try
{
Aes encryptor = Aes.Create();
byte[] data = AESHelper(clearText, true);

if (Object.Equals(encryptor, null))
{
return null;
}

encryptor.KeySize = 256;
encryptor.BlockSize = 128;
encryptor.Mode = CipherMode.CBC;
encryptor.Padding = PaddingMode.Zeros;
encryptor.Key = KEY;
encryptor.IV = IV;

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write);

cs.Write(clearText, 0, clearText.Length);
cs.Close();

response = DecompressData(ms.ToArray());
response = GZip.DecompressData(data);
}
catch (Exception ex)
{
Expand All @@ -51,7 +33,7 @@ public static string Decrypt(byte[] clearText)
if (response.EndsWith("\0"))
response = response.Substring(0, response.Length - 1);

if (IsValidXML(response).Equals(false))
if (util.IsValidXML(response).Equals(false))
{
MessageBox.Show("Not a valid config file...", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error);
return string.Empty;
Expand All @@ -64,7 +46,7 @@ public static byte[] Encrypt(string data)
{
byte[] response = null;

if (IsValidXML(data).Equals(false))
if (util.IsValidXML(data).Equals(false))
{
MessageBox.Show("Not a valid config file...", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
Expand All @@ -77,28 +59,9 @@ public static byte[] Encrypt(string data)

try
{
byte[] comp = CompressData(clearText);
Aes encryptor = Aes.Create();

if (Object.Equals(encryptor, null))
{
return null;
}

encryptor.KeySize = 256;
encryptor.BlockSize = 128;
encryptor.Mode = CipherMode.CBC;
encryptor.Padding = PaddingMode.Zeros;
encryptor.Key = KEY;
encryptor.IV = IV;
byte[] comp = GZip.CompressData(clearText);

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write);

cs.Write(comp, 0, comp.Length);
cs.Close();

response = ms.ToArray();
response = AESHelper(comp);
}
catch (Exception ex)
{
Expand All @@ -110,45 +73,38 @@ public static byte[] Encrypt(string data)
return response;
}

private static bool IsValidXML(string value)
private static byte[] AESHelper (byte[] data, bool decrypt = false)
{
if (string.IsNullOrWhiteSpace(value))
return false;

if (value.EndsWith("\0"))
value = value.Substring(0, value.Length - 1);
Aes encryptor = Aes.Create();

try
if (Object.Equals(encryptor, null))
{
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(value);

return true;
return null;
}
catch (XmlException)

encryptor.KeySize = 256;
encryptor.BlockSize = 128;
encryptor.Mode = CipherMode.CBC;
encryptor.Padding = PaddingMode.Zeros;
encryptor.Key = KEY;
encryptor.IV = IV;

MemoryStream ms = new MemoryStream();
CryptoStream cs;
if (decrypt)
{
return false;
cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write);
}
else {
cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write);
}
}

public static string DecompressData(byte[] data)
{
MemoryStream stream = new MemoryStream(data, 2, data.Length - 2);
DeflateStream inflater = new DeflateStream(stream, CompressionMode.Decompress);
StreamReader streamReader = new StreamReader(inflater);
cs.Write(data, 0, data.Length);
cs.Close();

return streamReader.ReadToEnd();
return ms.ToArray();
}

public static byte[] CompressData(byte[] data)
{
MemoryStream ms = new MemoryStream();
Stream s = new zlib.ZOutputStream(ms, 9);
s.Write(data, 0, data.Length);
s.Close();

return ms.ToArray();
}
}
}
28 changes: 28 additions & 0 deletions SPHDecode/Implementations/GZip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.IO;
using System.IO.Compression;
using zlib;

namespace SPHDecode.Implementations
{
class GZip
{
public static string DecompressData(byte[] data)
{
MemoryStream stream = new MemoryStream(data, 2, data.Length - 2);
DeflateStream inflater = new DeflateStream(stream, CompressionMode.Decompress);
StreamReader streamReader = new StreamReader(inflater);

return streamReader.ReadToEnd();
}

public static byte[] CompressData(byte[] data)
{
MemoryStream ms = new MemoryStream();
Stream s = new ZOutputStream(ms, 9);
s.Write(data, 0, data.Length);
s.Close();

return ms.ToArray();
}
}
}
30 changes: 30 additions & 0 deletions SPHDecode/Implementations/util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Xml;

namespace SPHDecode.Implementations
{
class util
{
public static bool IsValidXML(string value)
{
if (string.IsNullOrWhiteSpace(value))
return false;

if (value.EndsWith("\0"))
value = value.Substring(0, value.Length - 1);

try
{
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(value);

return true;
}
catch (XmlException ex)
{
LogManager.WriteToLog(ex.Message);
return false;
}
}
}
}
2 changes: 2 additions & 0 deletions SPHDecode/SPHDecode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@
</Compile>
<Compile Include="Implementations\Cryptography.cs" />
<Compile Include="Implementations\DelegateCommand.cs" />
<Compile Include="Implementations\GZip.cs" />
<Compile Include="Implementations\LogManager.cs" />
<Compile Include="Implementations\SuperViewModel.cs" />
<Compile Include="Implementations\util.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down

0 comments on commit 20419cc

Please sign in to comment.