From 20419cc6d288c90c89ee7daad630470bc9bc2d55 Mon Sep 17 00:00:00 2001 From: Stricted Date: Wed, 20 Jan 2016 12:30:18 +0100 Subject: [PATCH] move some stuff around --- SPHDecode/Implementations/Cryptography.cs | 100 ++++++---------------- SPHDecode/Implementations/GZip.cs | 28 ++++++ SPHDecode/Implementations/util.cs | 30 +++++++ SPHDecode/SPHDecode.csproj | 2 + 4 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 SPHDecode/Implementations/GZip.cs create mode 100644 SPHDecode/Implementations/util.cs diff --git a/SPHDecode/Implementations/Cryptography.cs b/SPHDecode/Implementations/Cryptography.cs index eb122af..ca3d675 100644 --- a/SPHDecode/Implementations/Cryptography.cs +++ b/SPHDecode/Implementations/Cryptography.cs @@ -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) { @@ -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; @@ -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; @@ -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) { @@ -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(); - } } } diff --git a/SPHDecode/Implementations/GZip.cs b/SPHDecode/Implementations/GZip.cs new file mode 100644 index 0000000..ff47ce2 --- /dev/null +++ b/SPHDecode/Implementations/GZip.cs @@ -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(); + } + } +} diff --git a/SPHDecode/Implementations/util.cs b/SPHDecode/Implementations/util.cs new file mode 100644 index 0000000..02e7520 --- /dev/null +++ b/SPHDecode/Implementations/util.cs @@ -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; + } + } + } +} diff --git a/SPHDecode/SPHDecode.csproj b/SPHDecode/SPHDecode.csproj index 6e3ac3b..3db859c 100644 --- a/SPHDecode/SPHDecode.csproj +++ b/SPHDecode/SPHDecode.csproj @@ -69,8 +69,10 @@ + + MainWindow.xaml Code