AesCryptoServiceProvider 不是 SymmetricAlgorithm 的一部分?

发布于 2024-08-30 18:26:12 字数 743 浏览 7 评论 0原文

我有一个快速的小应用程序,可以逐步完成可能的对称加密方法。我通过以下行获取它们:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云醉月微眠 2024-09-06 18:26:12

SymmetricAlgorithm 位于 mscorlib.dll 中,AesCryptoServiceProvider 位于 System.Core.dll

通过获取基于 SymmetricAlgorithm 类型的程序集,您将获取不包含 AesCryptoServiceProvider 的 mscorlib 程序集。

您可能需要 pinvoke CryptEnumProviders,要获取可用 CSP 的列表,然后您可以使用 CryptoConfig.CreateFromName(...) 创建实例那个 CSP。


您可以尝试这样的操作,如果其程序集加载到当前 AppDomain 中,它将找到类型。

var types = AppDomain.CurrentDomain.GetAssemblies()
    .Select(
        a => a.GetTypes()
            .Where( t => typeof(SymmetricAlgorithm).IsAssignableFrom(t) )
    )

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.

var types = AppDomain.CurrentDomain.GetAssemblies()
    .Select(
        a => a.GetTypes()
            .Where( t => typeof(SymmetricAlgorithm).IsAssignableFrom(t) )
    )
背叛残局 2024-09-06 18:26:12

您确定已加载带有 AesCryptoServiceProvider 的程序集吗? Assembly.GetAssembly 只会在当前加载的程序集集合中查找。

Are you sure the assembly with AesCryptoServiceProvider is loaded? Assembly.GetAssembly will only look in the currently loaded assembly collection.

身边 2024-09-06 18:26:12

我相信 Type.IsSubclassOf 仅检查类型是指定类型的直接子类。您是否尝试过使用 Type.IsAssignableFrom 相反?

type => typeof(SymmetricAlgorithm).IsAssignableFrom(type)

I believe Type.IsSubclassOf checks only if the type is a direct subclass of the specified type. Have you tried using Type.IsAssignableFrom instead?

type => typeof(SymmetricAlgorithm).IsAssignableFrom(type)
鼻尖触碰 2024-09-06 18:26:12

你应该做什么很容易在 Win32 API 中解释。有以下本机 API:

CryptEnumProvidersCryptEnumProviderTypesCryptGetDefaultProvider

这些 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_ENUMALGSPP_ENUMALGS_EX 您可以检查这个算法。您不会感到困惑,您可以在不同的提供程序(DLL)中拥有相同算法的许多实现。不同的提供商可以将密钥保存在不同的地方(如智能卡)或有一些主要工作(如 RSA SChannel),但需要实现一些其他算法。

每个算法都具有类似 ALG_ID aiAlgid; 的标识,其中 ALG_IDunsigned int,由三部分组成,如 wincrypt.h 中所示。

//
// Algorithm IDs and Flags
//

// ALG_ID crackers
#define GET_ALG_CLASS(x)                (x & (7 << 13))
#define GET_ALG_TYPE(x)                 (x & (15 << 9))
#define GET_ALG_SID(x)                  (x & (511))

// Algorithm classes
// certenrolld_begin -- ALG_CLASS_*
#define ALG_CLASS_ANY                   (0)
#define ALG_CLASS_SIGNATURE             (1 << 13)
#define ALG_CLASS_MSG_ENCRYPT           (2 << 13)
#define ALG_CLASS_DATA_ENCRYPT          (3 << 13)
#define ALG_CLASS_HASH                  (4 << 13)
#define ALG_CLASS_KEY_EXCHANGE          (5 << 13)
#define ALG_CLASS_ALL                   (7 << 13)
// certenrolld_end

// Algorithm types
#define ALG_TYPE_ANY                    (0)
#define ALG_TYPE_DSS                    (1 << 9)
#define ALG_TYPE_RSA                    (2 << 9)
#define ALG_TYPE_BLOCK                  (3 << 9)
#define ALG_TYPE_STREAM                 (4 << 9)
#define ALG_TYPE_DH                     (5 << 9)
#define ALG_TYPE_SECURECHANNEL          (6 << 9)

#define CALG_AES_256            (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_256)

我只包含一种加密算法。因此,您对 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) and PP_ENUMALGS or PP_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; where ALG_ID is unsigned int and consist from three parts like one can see in wincrypt.h:

//
// Algorithm IDs and Flags
//

// ALG_ID crackers
#define GET_ALG_CLASS(x)                (x & (7 << 13))
#define GET_ALG_TYPE(x)                 (x & (15 << 9))
#define GET_ALG_SID(x)                  (x & (511))

// Algorithm classes
// certenrolld_begin -- ALG_CLASS_*
#define ALG_CLASS_ANY                   (0)
#define ALG_CLASS_SIGNATURE             (1 << 13)
#define ALG_CLASS_MSG_ENCRYPT           (2 << 13)
#define ALG_CLASS_DATA_ENCRYPT          (3 << 13)
#define ALG_CLASS_HASH                  (4 << 13)
#define ALG_CLASS_KEY_EXCHANGE          (5 << 13)
#define ALG_CLASS_ALL                   (7 << 13)
// certenrolld_end

// Algorithm types
#define ALG_TYPE_ANY                    (0)
#define ALG_TYPE_DSS                    (1 << 9)
#define ALG_TYPE_RSA                    (2 << 9)
#define ALG_TYPE_BLOCK                  (3 << 9)
#define ALG_TYPE_STREAM                 (4 << 9)
#define ALG_TYPE_DH                     (5 << 9)
#define ALG_TYPE_SECURECHANNEL          (6 << 9)

#define CALG_AES_256            (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_256)

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 filter ALG_CLASS_DATA_ENCRYPT only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文