From b6c22aa65514bd80a7a94f6a7862b22febfe61fa Mon Sep 17 00:00:00 2001 From: Stricted Date: Wed, 27 Jan 2016 00:42:10 +0100 Subject: [PATCH] fix everyting --- SPHDecode.sln | 6 ++ SPHDecode/Implementations/Cryptography.cs | 34 +++---- SPHDecode/Implementations/GZip.cs | 28 ------ SPHDecode/Implementations/LogManager.cs | 2 +- SPHDecode/Implementations/Zlib.cs | 29 ++++++ SPHDecode/MainWindow.xaml | 24 ++--- SPHDecode/MainWindow.xaml.cs | 5 - SPHDecode/Model/MainWindowModel.cs | 112 ++++------------------ SPHDecode/SPHDecode.csproj | 30 +++++- SPHDecode/packages.config | 2 +- 10 files changed, 103 insertions(+), 169 deletions(-) delete mode 100644 SPHDecode/Implementations/GZip.cs create mode 100644 SPHDecode/Implementations/Zlib.cs diff --git a/SPHDecode.sln b/SPHDecode.sln index 2e8e837..60094df 100644 --- a/SPHDecode.sln +++ b/SPHDecode.sln @@ -8,13 +8,19 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Debug|x86.ActiveCfg = Debug|x86 + {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Debug|x86.Build.0 = Debug|x86 {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Release|Any CPU.ActiveCfg = Release|Any CPU {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Release|Any CPU.Build.0 = Release|Any CPU + {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Release|x86.ActiveCfg = Release|x86 + {40CB00AF-57F7-4C85-9894-EAB44DC124B4}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SPHDecode/Implementations/Cryptography.cs b/SPHDecode/Implementations/Cryptography.cs index 473a291..aadfc5b 100644 --- a/SPHDecode/Implementations/Cryptography.cs +++ b/SPHDecode/Implementations/Cryptography.cs @@ -1,10 +1,8 @@ using System; using System.IO; -using System.IO.Compression; using System.Security.Cryptography; using System.Text; using System.Windows; -using System.Xml; namespace SPHDecode.Implementations { @@ -13,59 +11,53 @@ public static class Cryptography private static byte[] KEY = new byte[] { 22, 39, 41, 141, 146, 199, 249, 4, 22, 135, 33, 125, 42, 121, 133, 198, 243, 104, 188, 35, 46, 48, 11, 1, 142, 200, 248, 130, 113, 81, 73, 62 }; // "1627298D92C7F9041687217D2A7985C6F368BC232E300B018EC8F8827151493E" private static byte[] IV = new byte[] { 89, 48, 127, 77, 236, 78, 199, 214, 97, 87, 151, 33, 145, 150, 117, 0 }; // "59307F4DEC4EC7D66157972191967500" - public static string Decrypt(byte[] clearText) + public static byte[] Decrypt(byte[] clearText) { - string response = string.Empty; + byte[] response; try { byte[] data = AESHelper(clearText, true); - response = GZip.DecompressData(data); + response = Zlib.DecompressData(data); } catch (Exception ex) { - LogManager.WriteToLog(ex.Message); + LogManager.WriteToLog(ex.ToString()); MessageBox.Show("unable to decrypt config file", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); return null; } - - if (response.EndsWith("\0")) - response = response.Substring(0, response.Length - 1); - - if (util.IsValidXML(response).Equals(false)) + + if (util.IsValidXML(Encoding.UTF8.GetString(response)).Equals(false)) { MessageBox.Show("Not a valid config file...", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); - return string.Empty; + return null; } - + return response; } - public static byte[] Encrypt(string data) + public static byte[] Encrypt(byte[] data) { byte[] response = null; - if (util.IsValidXML(data).Equals(false)) + if (util.IsValidXML(Encoding.UTF8.GetString(data)).Equals(false)) { MessageBox.Show("Not a valid config file...", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); return null; } - if (data.EndsWith("\0").Equals(false)) - data = string.Concat(data, "\0"); - - byte[] clearText = Encoding.UTF8.GetBytes(data); + byte[] clearText = data; try { - byte[] comp = GZip.CompressData(clearText); + byte[] comp = Zlib.CompressData(clearText); response = AESHelper(comp); } catch (Exception ex) { - LogManager.WriteToLog(ex.Message); + LogManager.WriteToLog(ex.ToString()); MessageBox.Show("unable to encrypt config file", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error); return null; } diff --git a/SPHDecode/Implementations/GZip.cs b/SPHDecode/Implementations/GZip.cs deleted file mode 100644 index ff47ce2..0000000 --- a/SPHDecode/Implementations/GZip.cs +++ /dev/null @@ -1,28 +0,0 @@ -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/LogManager.cs b/SPHDecode/Implementations/LogManager.cs index 2028cc7..56566ad 100644 --- a/SPHDecode/Implementations/LogManager.cs +++ b/SPHDecode/Implementations/LogManager.cs @@ -22,7 +22,7 @@ public static void WriteToLog(string value) catch (Exception ex) { // nothing - Console.WriteLine(ex.Message); + Console.WriteLine(ex.ToString()); } }).Start(); } diff --git a/SPHDecode/Implementations/Zlib.cs b/SPHDecode/Implementations/Zlib.cs new file mode 100644 index 0000000..9aa2dd4 --- /dev/null +++ b/SPHDecode/Implementations/Zlib.cs @@ -0,0 +1,29 @@ +using System.IO; +using System.IO.Compression; + +namespace SPHDecode.Implementations +{ + class Zlib + { + public static byte[] DecompressData(byte[] data) + { + MemoryStream ms = new MemoryStream(); + MemoryStream stream = new MemoryStream(data, 2, data.Length - 2); + DeflateStream s = new DeflateStream(stream, CompressionMode.Decompress); + s.CopyTo(ms); + return ms.ToArray(); + + } + + public static byte[] CompressData(byte[] data) + { + MemoryStream ms = new MemoryStream(); + Ionic.Zlib.ZlibStream s = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, Ionic.Zlib.CompressionLevel.Level9); + + s.Write(data, 0, data.Length); + s.Close(); + + return ms.ToArray(); + } + } +} diff --git a/SPHDecode/MainWindow.xaml b/SPHDecode/MainWindow.xaml index 65b6054..35f1d4a 100644 --- a/SPHDecode/MainWindow.xaml +++ b/SPHDecode/MainWindow.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SPHDecode" mc:Ignorable="d" - Title="SPHDecode" Height="160" Width="405"> + Title="SPHDecode" Height="145" Width="405"> @@ -15,25 +15,15 @@