AesCryptoServiceProvider 不是 SymmetricAlgorithm 的一部分?
我有一个快速的小应用程序,可以逐步完成可能的对称加密方法。我通过以下行获取它们:
private static List<Type> GetAlgorithmTypes
{
get { return Assembly.GetAssembly(typeof(SymmetricAlgorithm)).GetTypes().Where( type => type.IsSubclassOf(typeof(SymmetricAlgorithm))).ToList(); }
}
正如您在运行此命令时所看到的,AesCryptoServiceProvider 不是该组的成员,尽管它继承自 AES,而 AES 确实属于 SymmetricAlgorithm 并显示在我的列表中。这不会是一个太大的问题,如果我也有的话,我可以手动将提供程序添加到组中,但是如果我尝试通过其名称检索此类型:
Type t = Type.GetType("System.Security.Cryptography.AesCryptoServiceProvider");
我会得到 AesCryptoServiceProvider 的空对象,但不会得到任何 AesCryptoServiceProvider 的空对象该组中的其他项目。
这真的很奇怪,我想知道是否有人有任何想法。因此,这有点让我需要使用 TripleDES(因为我的机器都运行 FIPS 合规性要求)。
感谢您的帮助!
I have a quick little app that steps through the possible symmetric encryption methods. I get them with the following line:
private static List<Type> GetAlgorithmTypes
{
get { return Assembly.GetAssembly(typeof(SymmetricAlgorithm)).GetTypes().Where( type => type.IsSubclassOf(typeof(SymmetricAlgorithm))).ToList(); }
}
As you can see when i run this, AesCryptoServiceProvider is not a member of this group, even though it inherits from AES, which does belong to SymmetricAlgorithm and shows up in my list. This wouldn't be so much of a problem, i can manually add the provider in the group if i have too, but then if i try to retrieve this type by its name:
Type t = Type.GetType("System.Security.Cryptography.AesCryptoServiceProvider");
i get a null object for AesCryptoServiceProvider, but not for any of the other items in the group.
This is really strange, and i'm wondering if anyone has any ideas. It's kinda making me need to use tripleDES because of this (since my machines are all running the FIPS compliance requirement).
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SymmetricAlgorithm 位于 mscorlib.dll 中,AesCryptoServiceProvider 位于 System.Core.dll
通过获取基于 SymmetricAlgorithm 类型的程序集,您将获取不包含 AesCryptoServiceProvider 的 mscorlib 程序集。
您可能需要 pinvoke CryptEnumProviders,要获取可用 CSP 的列表,然后您可以使用 CryptoConfig.CreateFromName(...) 创建实例那个 CSP。
您可以尝试这样的操作,如果其程序集加载到当前 AppDomain 中,它将找到类型。
SymmetricAlgorithm is in mscorlib.dll, AesCryptoServiceProvider is located in System.Core.dll
By getting the assembly based on the SymmetricAlgorithm type you are getting the mscorlib assembly which does not contain AesCryptoServiceProvider.
You might want to pinvoke CryptEnumProviders, to get a list of available CSPs, then you can use CryptoConfig.CreateFromName(...) to create an instance of that CSP.
You could try something like this, it will find the type if its assembly loaded in the current AppDomain.
您确定已加载带有 AesCryptoServiceProvider 的程序集吗? Assembly.GetAssembly 只会在当前加载的程序集集合中查找。
Are you sure the assembly with AesCryptoServiceProvider is loaded? Assembly.GetAssembly will only look in the currently loaded assembly collection.
我相信 Type.IsSubclassOf 仅检查类型是指定类型的直接子类。您是否尝试过使用 Type.IsAssignableFrom 相反?
I believe Type.IsSubclassOf checks only if the type is a direct subclass of the specified type. Have you tried using Type.IsAssignableFrom instead?
你应该做什么很容易在 Win32 API 中解释。有以下本机 API:
CryptEnumProviders
、CryptEnumProviderTypes
、CryptGetDefaultProvider
,这些 API 与检查
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography 没什么区别\默认\提供者
。请参阅 http://msdn.microsoft.com 中的代码示例/en-us/library/aa382359(VS.85).aspx。每个提供商都实施一些加密算法。
CryptGetProvParam
的这方面(请参阅 http://msdn.microsoft.com/en-us/library/aa380196(VS.85).aspx) 和PP_ENUMALGS
或PP_ENUMALGS_EX
您可以检查这个算法。您不会感到困惑,您可以在不同的提供程序(DLL)中拥有相同算法的许多实现。不同的提供商可以将密钥保存在不同的地方(如智能卡)或有一些主要工作(如 RSA SChannel),但需要实现一些其他算法。每个算法都具有类似
ALG_ID aiAlgid;
的标识,其中ALG_ID
是unsigned int
,由三部分组成,如 wincrypt.h 中所示。 :我只包含一种加密算法。因此,您对
ALG_CLASS_DATA_ENCRYPT
类的所有算法都很感兴趣。您可以通过“ALG_ID破解者”GET_ALG_CLAS()
轻松找到所有算法的类部分,并仅过滤ALG_CLASS_DATA_ENCRYPT
。What you should do is easy to explain in Win32 API. There are following native API:
CryptEnumProviders
,CryptEnumProviderTypes
,CryptGetDefaultProvider
which do not much more as examine of
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\Defaults\Provider
. See code example from http://msdn.microsoft.com/en-us/library/aa382359(VS.85).aspx.Every provider implement some cryptographic algorithms. This respect of
CryptGetProvParam
(see http://msdn.microsoft.com/en-us/library/aa380196(VS.85).aspx) andPP_ENUMALGS
orPP_ENUMALGS_EX
you can examine this algorithms. You don't confused that you can have many implementation of the same algorithms inside of different providers (DLLs). Different providers can hold a key on the different place (like smart card) or has some main job like RSA SChannel, but need and implement some other algorithms.Every algorithm has identified like
ALG_ID aiAlgid;
whereALG_ID
isunsigned int
and consist from three parts like one can see in wincrypt.h:I included only one encrypting algorithm. So you are interesting for all algorithms with the class
ALG_CLASS_DATA_ENCRYPT
. You can easy find out from "ALG_ID crackers"GET_ALG_CLAS()
the class part of all algorithms and filterALG_CLASS_DATA_ENCRYPT
only.