Hi there! :)
I am trying to obfuscate a WPF app and apart from the problems that I will mention at the end, I have an improvement that will allow signing with PFX files. Here is the improvement:
Project.cs, Line 118 (replaced old code with this and added a "PfxPsw" element in the configuration XML)
```
} else {
try
{
var keyFile = vars.GetValue("KeyFile", null);
var bytes = File.ReadAllBytes(keyFile);
var ext = Path.GetExtension(keyFile);
if (ext!=null && ext.ToLower() == ".pfx")
{
bytes = Pfx2Snk(bytes, vars.GetValue("PfxPsw", null));
}
else
{
}
keyvalue = CryptoConvert.FromCapiKeyBlob(bytes);
} catch (Exception ex) {
throw new ObfuscarException (String.Format ("Failure loading key file \"{0}\"", vars.GetValue ("KeyFile", null)), ex);
}
```
/// <summary>
/// Converts .pfx file to .snk file.
/// </summary>
/// <param name="pfxData">.pfx file data.</param>
/// <param name="pfxPassword">.pfx file password.</param>
/// <returns>.snk file data.</returns>
public static byte[] Pfx2Snk(byte[] pfxData, string pfxPassword)
{
// load .pfx
var cert = new X509Certificate2(pfxData, pfxPassword, X509KeyStorageFlags.Exportable);
// create .snk
var privateKey = (RSACryptoServiceProvider)cert.PrivateKey;
return privateKey.ExportCspBlob(true);
}
```
```
Now to the WPF problems:
I use this XML configuration that should not change the asseblies at all:
```
<?xml version='1.0'?>
<Obfuscator>
<Var name="InPath" value="C:\Users\Me\" />
<Var name="OutPath" value="$(InPath)\Obfuscar_Output" />
<Var name="MarkedOnly" value="true" />
<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="false" />
<Var name="ReuseNames" value="false" />
<Var name="HideStrings" value="false" />
<Var name="OptimizedMethods" value="false" />
<Var name="RenameProperties" value="false" />
<Var name="RenameEvents" value="false" />
<Var name="XmlMapping" value="true" />
<Var name="PfxPsw" value="myPfxPassword" />
<Var name="KeyFile" value=".\Key.pfx" />
<Module file="$(InPath)\Dll1.dll" />
<Module file="$(InPath)\Dll2.dll" />
<Module file="$(InPath)\EXE1.exe" />
<Module file="$(InPath)\EXE2.exe" />
</Obfuscator>
```
However, when i start the WPF app, it crashes and in the event log I see this:
> Exception Info: System.Windows.Markup.XamlParseException
Stack:
at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri)
at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean)
at System.Windows.Application.LoadComponent(System.Object, System.Uri)
at KEL_Building_Manager.App.Main()
I am trying to obfuscate a WPF app and apart from the problems that I will mention at the end, I have an improvement that will allow signing with PFX files. Here is the improvement:
Project.cs, Line 118 (replaced old code with this and added a "PfxPsw" element in the configuration XML)
```
} else {
try
{
var keyFile = vars.GetValue("KeyFile", null);
var bytes = File.ReadAllBytes(keyFile);
var ext = Path.GetExtension(keyFile);
if (ext!=null && ext.ToLower() == ".pfx")
{
bytes = Pfx2Snk(bytes, vars.GetValue("PfxPsw", null));
}
else
{
}
keyvalue = CryptoConvert.FromCapiKeyBlob(bytes);
} catch (Exception ex) {
throw new ObfuscarException (String.Format ("Failure loading key file \"{0}\"", vars.GetValue ("KeyFile", null)), ex);
}
```
/// <summary>
/// Converts .pfx file to .snk file.
/// </summary>
/// <param name="pfxData">.pfx file data.</param>
/// <param name="pfxPassword">.pfx file password.</param>
/// <returns>.snk file data.</returns>
public static byte[] Pfx2Snk(byte[] pfxData, string pfxPassword)
{
// load .pfx
var cert = new X509Certificate2(pfxData, pfxPassword, X509KeyStorageFlags.Exportable);
// create .snk
var privateKey = (RSACryptoServiceProvider)cert.PrivateKey;
return privateKey.ExportCspBlob(true);
}
```
```
Now to the WPF problems:
I use this XML configuration that should not change the asseblies at all:
```
<?xml version='1.0'?>
<Obfuscator>
<Var name="InPath" value="C:\Users\Me\" />
<Var name="OutPath" value="$(InPath)\Obfuscar_Output" />
<Var name="MarkedOnly" value="true" />
<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="false" />
<Var name="ReuseNames" value="false" />
<Var name="HideStrings" value="false" />
<Var name="OptimizedMethods" value="false" />
<Var name="RenameProperties" value="false" />
<Var name="RenameEvents" value="false" />
<Var name="XmlMapping" value="true" />
<Var name="PfxPsw" value="myPfxPassword" />
<Var name="KeyFile" value=".\Key.pfx" />
<Module file="$(InPath)\Dll1.dll" />
<Module file="$(InPath)\Dll2.dll" />
<Module file="$(InPath)\EXE1.exe" />
<Module file="$(InPath)\EXE2.exe" />
</Obfuscator>
```
However, when i start the WPF app, it crashes and in the event log I see this:
> Exception Info: System.Windows.Markup.XamlParseException
Stack:
at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri)
at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean)
at System.Windows.Application.LoadComponent(System.Object, System.Uri)
at KEL_Building_Manager.App.Main()