gpt4 book ai didi

c# - 我可以在 .net 应用程序中暂时启用 FIPS 吗?

转载 作者:行者123 更新时间:2023-11-30 21:48:21 26 4
gpt4 key购买 nike

Openssl 允许进入和退出 FIPS 模式。 windows crypto api 和 .net 包装类是否有类似的功能?

我想启用 FIPS 模式,签署文档,然后返回正常模式。

最佳答案

不幸的是没有;至少,并非没有一些架构上的变化。

您可以通过设置注册表值来启用/禁用 FIPS 模式:

HKLM\System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy\Enabled (DWORD)

0为禁用,1为启用

但是,存在一些限制:一旦您将加密提供程序加载到您的进程中,它就会在进程剩余的生命周期中“记住”该 FIPS 模式的状态。所以像这样的代码可以工作:

(注意:两种情况都假设 FIPS 模式在开始时处于关闭状态)

static void Main(string[] args)
{
using (RegistryKey fipsAlgorithmPolicy = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy", true))
{
fipsAlgorithmPolicy.SetValue("Enabled", 1, RegistryValueKind.DWord);
}
SHA1 sha = new SHA1Managed(); // throws, which is what you want
}

但是这样的代码不会:

static void Main(string[] args)
{
SHA1 sha = new SHA1Managed(); // Does not throw, which is expected
using (RegistryKey fipsAlgorithmPolicy = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy", true))
{
fipsAlgorithmPolicy.SetValue("Enabled", 1, RegistryValueKind.DWord);
}
sha = new SHA1Managed(); // Also does not throw, which is a shame
}

为了让您的代码反射(reflect)新状态,您必须重新启动您的进程。话虽如此,您可以做的是将执行加密例程的代码隔离到您的应用程序在设置 FIPS 模式后生成的“帮助程序”进程中。实现起来会有点麻烦,但它允许您切换 FIPS 模式并让您的代码按预期运行。

关于c# - 我可以在 .net 应用程序中暂时启用 FIPS 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37709434/

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