Json.NET 和混淆、反序列化不起作用
Json.NET and Obfuscation, deserialization not working
我想分享我的团队的一个解决方案,我发现它可以克服在尝试结合 Json.Net 和混淆时出现的问题。
我的配置:VS2013、C#、.NET 4.0、Json.Net 7.0.1、CodeWall/Crypto 混淆器。
一切正常,直到我混淆我的代码。之后我无法反序列化我的 json 文件(已正确序列化!)。
我们的解决方案提供了 DefaultSerializationBinder class 的 "sort of hacking",您可以简单地从源代码中挑选并随意修改(或提供您自定义的抽象覆盖 class序列化绑定器)。当它试图从程序集名称中发现正确的程序集时,问题似乎出现了……这里的混淆不会匹配两者。
此处是您需要重写的方法代码:
private static Type GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
{
string assemblyName = typeNameKey.AssemblyName;
string typeName = typeNameKey.TypeName;
if (assemblyName != null)
{
// look, I don't like using obsolete methods as much as you do but this is the only way
// Assembly.Load won't check the GAC for a partial name
Assembly assembly = Assembly.LoadWithPartialName(assemblyName);
if (assembly == null)
{
string partialName = assemblyName;
var elements = assemblyName.Split(',');
if (elements.Length > 0)
{
partialName = elements[0];
}
// will find assemblies loaded with Assembly.LoadFile outside of the main directory
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in loadedAssemblies)
{
if (a.GetName().Name == assemblyName || a.FullName == assemblyName || a.GetName().Name == partialName)
{
assembly = a;
break;
}
}
}
if (assembly == null)
{
throw new JsonSerializationException(string.Format("Could not load assembly '{0}'.", assemblyName));
}
Type type = assembly.GetType(typeName);
if (type == null)
{
throw new JsonSerializationException(string.Format("Could not find type '{0}' in assembly '{1}'.", typeName, assembly.FullName));
}
return type;
}
else if (typeName != null)
{
return Type.GetType(typeName);
}
else
{
return null;
}
}
希望对您有所帮助!
请随意分享您的想法,欢迎进行其他测试!
我想分享我的团队的一个解决方案,我发现它可以克服在尝试结合 Json.Net 和混淆时出现的问题。
我的配置:VS2013、C#、.NET 4.0、Json.Net 7.0.1、CodeWall/Crypto 混淆器。
一切正常,直到我混淆我的代码。之后我无法反序列化我的 json 文件(已正确序列化!)。
我们的解决方案提供了 DefaultSerializationBinder class 的 "sort of hacking",您可以简单地从源代码中挑选并随意修改(或提供您自定义的抽象覆盖 class序列化绑定器)。当它试图从程序集名称中发现正确的程序集时,问题似乎出现了……这里的混淆不会匹配两者。
此处是您需要重写的方法代码:
private static Type GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
{
string assemblyName = typeNameKey.AssemblyName;
string typeName = typeNameKey.TypeName;
if (assemblyName != null)
{
// look, I don't like using obsolete methods as much as you do but this is the only way
// Assembly.Load won't check the GAC for a partial name
Assembly assembly = Assembly.LoadWithPartialName(assemblyName);
if (assembly == null)
{
string partialName = assemblyName;
var elements = assemblyName.Split(',');
if (elements.Length > 0)
{
partialName = elements[0];
}
// will find assemblies loaded with Assembly.LoadFile outside of the main directory
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in loadedAssemblies)
{
if (a.GetName().Name == assemblyName || a.FullName == assemblyName || a.GetName().Name == partialName)
{
assembly = a;
break;
}
}
}
if (assembly == null)
{
throw new JsonSerializationException(string.Format("Could not load assembly '{0}'.", assemblyName));
}
Type type = assembly.GetType(typeName);
if (type == null)
{
throw new JsonSerializationException(string.Format("Could not find type '{0}' in assembly '{1}'.", typeName, assembly.FullName));
}
return type;
}
else if (typeName != null)
{
return Type.GetType(typeName);
}
else
{
return null;
}
}
希望对您有所帮助!
请随意分享您的想法,欢迎进行其他测试!