Skip to content

Commit

Permalink
fix everyting
Browse files Browse the repository at this point in the history
  • Loading branch information
Stricted committed Jan 26, 2016
1 parent b6a122e commit b6c22aa
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 169 deletions.
6 changes: 6 additions & 0 deletions SPHDecode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 13 additions & 21 deletions SPHDecode/Implementations/Cryptography.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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;
}
Expand Down
28 changes: 0 additions & 28 deletions SPHDecode/Implementations/GZip.cs

This file was deleted.

2 changes: 1 addition & 1 deletion SPHDecode/Implementations/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void WriteToLog(string value)
catch (Exception ex)
{
// nothing
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ToString());
}
}).Start();
}
Expand Down
29 changes: 29 additions & 0 deletions SPHDecode/Implementations/Zlib.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
24 changes: 7 additions & 17 deletions SPHDecode/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Grid DataContext="{StaticResource MainWindowModel}">
<StackPanel HorizontalAlignment="Left" Width="375" Margin="10,10,0,0" VerticalAlignment="Top">
<StackPanel.Resources>
Expand All @@ -15,25 +15,15 @@
</StackPanel.Resources>
<DockPanel>
<Button Command="{Binding Path=srcFileDialog}" Content="Source File" Width="70" />
<TextBox Text="{Binding Path=srcFile}" Width="270" Margin="0,5,0,5" IsReadOnly="True"/>
<Button Command="{Binding Path=LoadConfig}" Content="Load"/>
<TextBox Text="{Binding Path=srcFile}" Width="300" Margin="0,5,0,5" IsReadOnly="True"/>
</DockPanel>
<StackPanel Orientation="Horizontal">
<StackPanel Width="187">
<TextBlock Text="Telnet" />
<TextBlock Text="Username" />
<TextBlock Text="Passwort" />
</StackPanel>
<StackPanel Width="187">
<CheckBox IsChecked="{Binding Path=telnet,Mode=TwoWay}"/>
<TextBox Text="{Binding Path=username,Mode=TwoWay}" />
<TextBox Text="{Binding Path=password,Mode=TwoWay}"/>
</StackPanel>
</StackPanel>
<DockPanel>
<Button Command="{Binding Path=dstFileDialog}" Content="Save File" Width="70" />
<TextBox Text="{Binding Path=dstFile}" Width="270" Margin="0,5,0,5" IsReadOnly="True"/>
<Button Command="{Binding Path=SaveConfig}" Content="save"/>
<TextBox Text="{Binding Path=dstFile}" Width="300" Margin="0,5,0,5" IsReadOnly="True"/>
</DockPanel>
<DockPanel>
<Button Width="185" Command="{Binding Path=decrypt}" Content="decrypt"/>
<Button Width="185" Command="{Binding Path=encrypt}" Content="encrypt"/>
</DockPanel>
</StackPanel>
</Grid>
Expand Down
5 changes: 0 additions & 5 deletions SPHDecode/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ public MainWindow()
{
InitializeComponent();
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{

}
}
}
112 changes: 21 additions & 91 deletions SPHDecode/Model/MainWindowModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Xml;

namespace SPHDecode.Model
{
class MainWindowModel : SuperViewModel
{
private string _srcFile;
private string _dstFile;
private bool _telnet;
private string _username;
private string _password;
private string _config;
private DelegateCommand _srcFileDialog;
private DelegateCommand _dstFileDialog;
private DelegateCommand _loadConfig;
private DelegateCommand _saveConfig;

private DelegateCommand _encrypt;
private DelegateCommand _decrypt;

public string srcFile
{
Expand All @@ -33,30 +27,6 @@ public string dstFile
set { SetProperty(ref _dstFile, value); }
}

public bool telnet
{
get { return _telnet; }
set { SetProperty(ref _telnet, value); }
}

public string username
{
get { return _username; }
set { SetProperty(ref _username, value); }
}

public string password
{
get { return _password; }
set { SetProperty(ref _password, value); }
}

public string config
{
get { return _config; }
set { SetProperty(ref _config, value); }
}

public DelegateCommand srcFileDialog
{
get { return _srcFileDialog; }
Expand All @@ -69,16 +39,16 @@ public DelegateCommand dstFileDialog
set { SetProperty(ref _dstFileDialog, value); }
}

public DelegateCommand LoadConfig
public DelegateCommand encrypt
{
get { return _loadConfig; }
set { SetProperty(ref _loadConfig, value); }
get { return _encrypt; }
set { SetProperty(ref _encrypt, value); }
}

public DelegateCommand SaveConfig
public DelegateCommand decrypt
{
get { return _saveConfig; }
set { SetProperty(ref _saveConfig, value); }
get { return _decrypt; }
set { SetProperty(ref _decrypt, value); }
}

private void OnsrcFileDialogExecute()
Expand All @@ -99,76 +69,36 @@ private void OndstFileDialogExecute()
saveFileDialog = null;
}

private void OnLoadConfigExecute()
private void OnencryptExecute()
{
if (string.IsNullOrWhiteSpace(srcFile))
{
LogManager.WriteToLog("no input file specified");
MessageBox.Show("no input file specified", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

byte[] orig = File.ReadAllBytes(srcFile);
config = Cryptography.Decrypt(orig);

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(config);
byte[] encode = Cryptography.Encrypt(orig);

XmlNode node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/DeviceInfo/X_ServiceManage");
string tel = node.Attributes["TelnetEnable"]?.InnerText;

telnet = false;
if (tel.Equals("1"))
if (Object.Equals(encode, null).Equals(false))
{
telnet = true;
File.WriteAllBytes(dstFile, encode);
MessageBox.Show("config encrypted successfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
}

node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/UserInterface/X_Cli/UserInfo").FirstChild;
username = node.Attributes["Username"]?.InnerText;
password = node.Attributes["Userpassword"]?.InnerText;
}

private void OnSaveConfigExecute()
private void OndecryptExecute()
{
if (string.IsNullOrWhiteSpace(dstFile))
{
LogManager.WriteToLog("no output file specified");
MessageBox.Show("no output file specified", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(config);

XmlNode node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/DeviceInfo/X_ServiceManage");
string tel = node.Attributes["TelnetEnable"]?.InnerText;

node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/UserInterface/X_Cli/UserInfo").FirstChild;
string user = node.Attributes["Username"]?.InnerText;
string pass = node.Attributes["Userpassword"]?.InnerText;

config = config.Replace(
string.Concat("<X_ServiceManage TelnetEnable=\"", tel, "\" TelnetPort=\"23\" KeyEquipMode=\"0\"/>"),
string.Concat("<X_ServiceManage TelnetEnable=\"", (telnet.Equals(false) ? 0 : 1), "\" TelnetPort=\"23\" KeyEquipMode=\"0\"/>"));

config = config.Replace(
string.Concat("<UserInfoInstance InstanceID=\"1\" Username=\"", user, "\" Userpassword=\"", pass, "\" Userlevel=\"0\" Timestamp=\"0000-00-00 00:00:00\" Size=\"0\"/>"),
string.Concat("<UserInfoInstance InstanceID=\"1\" Username=\"", username, "\" Userpassword=\"", password, "\" Userlevel=\"0\" Timestamp=\"0000 - 00 - 00 00:00:00\" Size=\"0\"/>") );
byte[] orig = File.ReadAllBytes(srcFile);
byte[] decode = Cryptography.Decrypt(orig);

byte[] encode = Cryptography.Encrypt(config);
if (Object.Equals(encode, null).Equals(false))
if (Object.Equals(decode, null).Equals(false))
{
File.WriteAllBytes(dstFile, encode);
MessageBox.Show("config encrypted successfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
File.WriteAllBytes(dstFile, decode);
MessageBox.Show("config decrypted successfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
}
}

public MainWindowModel()
{
srcFileDialog = new DelegateCommand(new Action(OnsrcFileDialogExecute));
dstFileDialog = new DelegateCommand(new Action(OndstFileDialogExecute));
LoadConfig = new DelegateCommand(new Action(OnLoadConfigExecute));
SaveConfig = new DelegateCommand(new Action(OnSaveConfigExecute));
encrypt = new DelegateCommand(new Action(OnencryptExecute));
decrypt = new DelegateCommand(new Action(OndecryptExecute));
}
}
}
Loading

0 comments on commit b6c22aa

Please sign in to comment.