简体   繁体   English

如何使用 .Net (C#) 在 Windows 中安全地存储 AES 密钥?

[英]How can I securly store an AES key in Windows with .Net (C#)?

I've looking for a way to store a given AES key so that it can't be retrieved, but it can still be used for encryption and decryption (using C#).我一直在寻找一种方法来存储给定的 AES 密钥,以便无法检索它,但它仍然可以用于加密和解密(使用 C#)。 I think the equivalent for asymmetric key storage can be found here , but I'm looking for something that can be used for symmetric encryption.我认为可以在这里找到非对称密钥存储的等价物,但我正在寻找可用于对称加密的东西。 Does it exist in a managed form (pre .Net 4)?它是否以托管形式存在(.Net 4 之前)?

Windows DPAPI ( Win32 documentation ), and its .NET wrapper ( ProtectedData Class ) does not store any data. Windows DPAPI( Win32 文档)及其 .NET 包装器( ProtectedData 类)不存储任何数据。 Rather, Windows DPAPI returns a cryptographic cypher value which you can store anywhere you like, including on multiple servers.相反,Windows DPAPI 返回一个加密密码值,您可以将其存储在您喜欢的任何位置,包括在多个服务器上。

At my place of work we use DPAPI to generate a cypher for an AES key which we then store in the Registry.在我的工作场所,我们使用 DPAPI 为 AES 密钥生成密码,然后将其存储在注册表中。

The sole purpose of Windows DPAPI is to encrypt data such that only a given user account or machine can decrypt it, without needing to store a password. Windows DPAPI 的唯一目的是加密数据,以便只有给定的用户帐户或机器可以解密它,而无需存储密码。

The .NET ProtectedData class has been in the .NET Framework since 2.0. .NET ProtectedData 类从 2.0 开始就存在于 .NET Framework 中。

I would stick with Windows DPAPI over a third party product as it is mature, stable, free, easy to use and fully supported in .NET.我会坚持使用 Windows DPAPI 而不是第三方产品,因为它成熟、稳定、免费、易于使用并且完全受 .NET 支持。

根据您要防御的对象,您可以使用ProtectedData

@SLaks is right, if its in your memory it can be accessed. @SLaks 是对的,如果它在您的记忆中可以访问。 You can make it more difficult, but it's always going to be possible.你可以让它变得更困难,但它总是有可能的。

That's why folks who are serious offload the crypto.这就是为什么认真卸载加密货币的人。

One options is a smart card .一种选择是智能卡 This lets you move data to the card and get results back, but doesn't allow access to the key material.这使您可以将数据移动到卡上并取回结果,但不允许访问密钥材料。 It's not in your PCs memory space so it can't be leaked.它不在您的 PC 内存空间中,因此不会泄漏。

Ross Anderson has a good paper, Programming Satan's Computer about just this kind of thing.罗斯·安德森(Ross Anderson)有一篇很好的论文,为撒旦的计算机编程,就是关于这种事情的。 From the abstract:从摘要:

The problem is the presence of a hostile opponent, who can alter messages at will.问题是存在敌对的对手,他们可以随意更改消息。 In effect, our task is to program a computer which gives answers which are subtly and maliciously wrong at the most inconvenient possible moment.实际上,我们的任务是对一台计算机进行编程,该计算机在最不方便的时候给出巧妙和恶意错误的答案。

Even if you're not concerned about physical memory and just the hard disk and source you still need to be wary of virtual memory.即使您不关心物理内存而只关心硬盘和源,您仍然需要警惕虚拟内存。 If you're not careful (or using a carefully written service) you can get plaintext keys in your swap file.如果您不小心(或使用精心编写的服务),您可以在交换文件中获取明文密钥。 Here's another link that discusses the issue.这是讨论该问题的另一个链接。 Not that you want to do that but it makes the issue apparent: Encrypting Virtual Memory .并不是你想这样做,但它使问题变得明显: Encrypting Virtual Memory I believe there are system calls for this purpose to mark memory as unswappable but I can't find a link.我相信有系统调用为此目的将内存标记为不可交换,但我找不到链接。

Even for asymmetric data, if the key is stored in computer and is used later, then it's retrieved and decrypted before use.即使对于非对称数据,如果密钥存储在计算机中并在以后使用,则在使用前会对其进行检索和解密。 And at this point a skilled hacker can retrieve it (by capturing computer memory and studying it).此时,熟练的黑客可以检索它(通过捕获计算机内存并研究它)。 This is not trivial, but still possible.这不是微不足道的,但仍然可能。

In general to address your problem USB cryptotokens and cryptocards are offered.通常,为了解决您的问题,提供了 USB 加密令牌和加密卡。 These hardware devices have their own memory for storing both symmetric and asymmetric keys, and they have processor to perform cryptographic operations using that keys.这些硬件设备有自己的内存来存储对称和非对称密钥,并且它们有处理器来使用这些密钥执行加密操作。 The key never leaves the device and it's virtually impossible to extract it from the device forcefully (there exist some hardware attacks such as scanning memory with microscope, but they are way more complicated than a software attack on computer).密钥永远不会离开设备,并且几乎不可能强行将其从设备中提取(存在一些硬件攻击,例如用显微镜扫描内存,但它们比对计算机的软件攻击复杂得多)。

So if your key is really valuable, use USB cryptotoken.因此,如果您的密钥真的很有价值,请使用 USB 加密令牌。 The price of the device is very moderate - about $70-$100 per unit and there are several vendors that offer such devices.该设备的价格非常适中——每台约 70-100 美元,并且有多家供应商提供此类设备。

为了继续卸载加密的趋势,如果您知道所有集群的硬件,您可以在TPM 中输入密钥(如果主板有的话),这只是 USB 或智能卡解决方案的另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM