gpt4 book ai didi

c# - ASPNET_REGIIS : Place AES key and IV into a KeyContainer

转载 作者:可可西里 更新时间:2023-11-01 08:26:15 37 4
gpt4 key购买 nike

是否可以使用 ASPNET_REGIIS 将 AES key 和 IV 放入 KeyContainer 中?如果是,如何?

上下文:

我创建了 AesProtectedConfigurationProvider 以使用 AES 而非三重 DES(即 3DES)加密 web.config 数据。我还创建了一个控制台应用程序,它使用 AesProtectedConfigurationProvider 来生成 AES key 和初始化向量 (IV)。我可以将 key 保存到一个文本文件,然后在 web.config 的提供程序中引用该文本文件。从那里,我能够加密 web.config 文件。但是,如果可能的话,我想通过将 keys.txt 文件移动到 KeyContainer 中来保护它们。

因此,在 provider 标签下,keyContainerName 部分将是:

keyContainerName="AesKeyContainer" 

相对于

keyContainerName="C:\AesKey.txt"

我的理解是 ASPNET_REGIIS 中开箱即用的当前加密产品使用 3DES 来加密 web.config 文件的内容,而 RsaProtectedConfigurationProvider 用于加密 3DES key (如果我错了请纠正我这个)。因此,如果可以使用 RsaProtectedConfigurationProvider 将 AES key 加密到 KeyContainer 中,那将很有帮助。我查看了以下网站,但不确定这是否可行:

https://msdn.microsoft.com/en-us/library/33ws57y0.aspx

How to encrypt web.config with AES instead of 3DES

编辑:有谁知道为什么微软在.NET的后续版本中去掉了AesProtectedConfigurationProvider?这似乎是一种倒退,因为 AES 是当前标准,而不再推荐使用 3DES。在与同事交谈时,他们提到它可能由于安全漏洞而被删除,例如;特权提升。 Microsoft 以在安全方面做出未经宣布的更改而闻名。但是,我想知道是否有人确切知道。如果确实在 AesProtectedConfigurationProvider 中发现了缺陷,那么我可能倾向于继续使用 3DES。

最佳答案

RsaProtectedConfigurationProviderAesProtectedConfigurationProvider 尽管名称非常相似,但却是不同领域的一部分。

RsaProtectedConfigurationProvider 驻留在 System.Configuration 中,并用于(作为继承自抽象 ProtectedConfigurationProvider 的其他提供程序)用于配置部分的加密/解密ASP.NET 应用程序的 web.config。

AesProtectedConfigurationProvider 又位于 Microsoft.ApplicationHost 中,仅用于 IIS 配置加密。在默认应用程序池 (DefaultAppPool.config) 的配置文件中,您将找到以下内容:

<configProtectedData>
<providers>
<!-- ... -->
<add name="AesProvider" type="Microsoft.ApplicationHost.AesProtectedConfigurationProvider" ... />
<add name="IISWASOnlyAesProvider" type="Microsoft.ApplicationHost.AesProtectedConfigurationProvider" ... />
</providers>
</configProtectedData>

您可以在 IIS Securing Configuration 中阅读有关 AesProviderIISWASOnlyAesProvider 的信息文章:

AesProvider - Encrypting IIS configuration sections read by the IIS worker process using AES encryption.

IISWASOnlyAesProvider - Encrypting IIS configuration sections read by WAS using AES encryption.

所以回答你的第一个问题:

  1. Confirm whether using the AesProtectedConfigurationProvider is safe. It was removed by Microsoft in subsequent releases of .NET but I cannot seem to find a reason

是的,如果我们假设您已正确实现且没有安全漏洞,那么使用您的自定义 AES 提供程序是安全的。 Microsoft 尚未从 .Net Framework 中删除 AesProtectedConfigurationProvider,它从来不是 System.Configuration 的一部分。如果 Microsoft 在其实现中发现了安全漏洞,他们可以只修复它而不是删除它,对吗?

  1. Provide steps to implement the AesProtectedConfigurationProvider and to create a KeyContainer in ASPNET_REGIIS

我相信您可以在不实现自定义 AesProtectedConfigurationProvider 的情况下进行 AES 加密。

我深入研究 source code RsaProtectedConfigurationProvider,发现它有如下逻辑:

private SymmetricAlgorithm GetSymAlgorithmProvider() {
SymmetricAlgorithm symAlg;

if (UseFIPS) {
// AesCryptoServiceProvider implementation is FIPS certified
symAlg = new AesCryptoServiceProvider();
}
else {
// Use the 3DES. FIPS obsolated 3DES
symAlg = new TripleDESCryptoServiceProvider();

byte[] rgbKey1 = GetRandomKey();
symAlg.Key = rgbKey1;
symAlg.Mode = CipherMode.ECB;
symAlg.Padding = PaddingMode.PKCS7;
}

return symAlg;
}

如您所见,默认 RSAProtectedConfigurationProvider 支持通过 System.Security.Cryptography.AesCryptoServiceProvider 从三重 DES 加密切换到 AES 加密。

UseFIPS 标志是从 RsaProtectedConfigurationProvider 的配置部分读取的。您可以在机器级别 (machine.config) 上设置它,以便它应用于所有加密的配置或仅应用于特定的 web.config。

对于后面的情况,将以下部分添加到 web.config(我已经从 machine.config 复制了该部分并添加了 useFIPS="true"):

<configuration>

<!-- ... -->

<configProtectedData>
<providers>
<remove name="RsaProtectedConfigurationProvider"/>
<add name="RsaProtectedConfigurationProvider"
type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
description="Uses RsaCryptoServiceProvider to encrypt and decrypt"
keyContainerName="NetFrameworkConfigurationKey"
cspProviderName=""
useMachineContainer="true"
useOAEP="false"
useFIPS="true"
/>
</providers>
</configProtectedData>

<!-- ... -->

</configuration>

现在,如果您运行 aspnet_regiis,您将看到数据是使用 256 位 AES 加密的:

<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />

AES 对称 key 的存储方式与三重 DES 模式相同: key 使用 RSA 加密并嵌入到加密部分,而 RSA key 存储在机器 key 容器中。看这个blog post了解更多详情。

我相信使用已在 RsaProtectedConfigurationProvider 中实现的 AES 加密是比自定义 AES 提供程序更好的选择。您正在使用现有的 key 存储方法,并且您不会受到可能(极有可能)的安全漏洞的影响。

关于c# - ASPNET_REGIIS : Place AES key and IV into a KeyContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48780283/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com