无法从程序集 Version=2.4.0.0,区域性 = 中性加载类型“System.Security.Cryptogr
本文关键字:类型 加载 Cryptogr Security System 程序集 Version 区域性 | 更新日期: 2023-09-27 18:34:54
我使用此代码动态加载加密提供程序
string hashAlgoTypeString =
ConfigurationManager.AppSettings[HashAlgorithmProviderConfiguration];
if (hashAlgoTypeString != null)
{
Type hashAlgoType = Type.GetType(hashAlgoTypeString, true);
return (HashAlgorithm) Activator.CreateInstance(hashAlgoType);
}
hashAlgoTypeString 是在我的 Web.config 中设置的 CryptoServiceProviders 之一的完整限定名称
加载这些我没有任何问题
System.Security.Cryptography.DESCryptoServiceProvider
System.Security.Cryptography.DSACryptoServiceProvider
System.Security.Cryptography.MD5CryptoServiceProvider
System.Security.Cryptography.RC2CryptoServiceProvider
System.Security.Cryptography.RNGCryptoServiceProvider
System.Security.Cryptography.RSACryptoServiceProvider
System.Security.Cryptography.SHA1CryptoServiceProvider
System.Security.Cryptography.TripleDESCryptoServiceProvider
但是,以下加密服务提供程序都抛出">无法加载类型"异常。
System.Security.Cryptography.AesCryptoServiceProvider
System.Security.Cryptography.SHA256CryptoServiceProvider
System.Security.Cryptography.SHA384CryptoServiceProvider
System.Security.Cryptography.SHA512CryptoServiceProvider
这四个提供商可能有什么问题?
我正在运行带有.NET 4.5.2的Windows 7 x64
在广汽
System.Security v2.0.0.0
mscorlib v2.0.0.0 for x86 and AMD64
编辑
我能够实例化所有四个
var Aes = new System.Security.Cryptography.AesCryptoServiceProvider();
var sha256 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
var sha384 = new System.Security.Cryptography.SHA384CryptoServiceProvider();
var sha512 = new System.Security.Cryptography.SHA512CryptoServiceProvider();
并从我的代码中使用它们,没有问题。
它们不是在同一个 dll 中定义的!
那些工作的在mscorlib.dll中,而那些不起作用的在System.Core中.dll
可能如果你添加对System.Core的引用.dll它将起作用。
问题出在从 Web.config 返回的值中
string hashAlgoTypeString =
ConfigurationManager.AppSettings[HashAlgorithmProviderConfiguration];
而不是
System.Security.Cryptography.SHA256CryptoServiceProvider
它应该是完全限定的名称
System.Security.Cryptography.SHA512CryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089