使用.Net持久存储加密数据

发布于 2024-07-05 17:35:06 字数 307 浏览 4 评论 0原文

我需要在应用程序运行之间存储加密数据(一些小字符串)。 我不希望用户每次启动应用程序时都提供密码。 也就是说,毕竟它归结为安全地存储加密密钥。

我正在研究 RSACryptoServiceProvider 并使用 PersistentKeyInCsp,但我不确定它是如何工作的。 密钥容器在应用程序运行或计算机重新启动之间是否持续存在? 如果是,是特定于用户的还是特定于机器的。 即,如果我将加密数据存储在用户的漫游配置文件中,如果用户登录到不同的计算机上,我可以解密数据吗?

如果上述方法不起作用,我有什么选择(我需要处理漫游配置文件)。

I need to store encrypted data (few small strings) between application runs. I do not want the user to provide a passphrase every time (s)he launches the application. I.e. after all it goes down to storing securely the encryption key(s).

I was looking into RSACryptoServiceProvider and using PersistentKeyInCsp, but I'm not sure how it works. Is the key container persistent between application runs or machine restarts? If yes, is it user specific, or machine specific. I.e. if I store my encrypted data in user's roaming profile, can I decrypt the data if the user logs on a different machine?

If the above does not work, what are my options (I need to deal with roaming profiles).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

抚你发端 2024-07-12 17:35:07

数据保护 API (DPAPI) 正是您想要的。 它使用机器或(更好)用户的凭据作为加密密钥,提供任意数据的对称加密。 您不必担心管理密钥; Windows 会为您处理好这些事情。 如果用户更改密码,Windows 将使用用户的新密码重新加密数据。

DPAPI 在 .NET 中通过 System.Security.Cryptography.ProtectedData 类公开:

byte[] plaintextBytes = GetDataToProtect();
byte[] encodedBytes = ProtectedData.Protect(plaintextBytes, null, DataProtectionScope.CurrentUser);

Protect 方法的第二个参数是可选的熵字节数组,它可以用作附加的特定于应用程序的“秘密”。

要解密,请使用 ProtectedData.Unprotect 调用:

byte[] encodedBytes = GetDataToUnprotect();
byte[] plaintextBytes = ProtectedData.Unprotect(encodedBytes, null, DataProtectionScope.CurrentUser);

DPAPI 可与漫游配置文件一起正常工作(如此处所述) ,尽管您需要将加密数据存储在某个位置(网络共享、IsolatedStorage 和 IsolatedStorageScope.Roaming 等),您的各种计算机都可以访问。

有关详细信息,请参阅 MSDN 中的 ProtectedData 类。 此处有一份 DPAPI 白皮书,其中包含比您以前了解的更多信息想。

The Data Protection API (DPAPI) does exactly what you want. It provides symmetric encryption of arbitrary data, using the credentials of the machine or (better) the user, as the encryption key. You don't have to worry about managing the keys; Windows takes care of that for you. If the user changes his password, Windows will re-encrypt the data using the user's new password.

DPAPI is exposed in .NET with the System.Security.Cryptography.ProtectedData class:

byte[] plaintextBytes = GetDataToProtect();
byte[] encodedBytes = ProtectedData.Protect(plaintextBytes, null, DataProtectionScope.CurrentUser);

The second parameter of the Protect method is an optional entropy byte array, which can be used as an additional application-specific "secret".

To decrypt, use the ProtectedData.Unprotect call:

byte[] encodedBytes = GetDataToUnprotect();
byte[] plaintextBytes = ProtectedData.Unprotect(encodedBytes, null, DataProtectionScope.CurrentUser);

DPAPI works correctly with roaming profiles (as described here), though you'll need to store the encrypted data in a place (network share, IsolatedStorage with IsolatedStorageScope.Roaming, etc.) that your various machines can access.

See the ProtectedData class in MSDN for more information. There's a DPAPI white paper here, with more information than you'd ever want.

笑咖 2024-07-12 17:35:07

我想添加 DPAPI 方法。

尽管我自己没有实现用户存储方法,但 Microsoft 提供了有关用户存储方法的文档,该文档可以为特定用户加密和解密数据。

我使用机器商店的 DPAPI。 如果它符合您想要做的事情,我会描述它。 我使用 Windows 服务加载 Windows 用户配置文件,并且该用户的密码用于加密数据。

附带说明一下,DPAPI 使用 Triple-DES,它可能稍弱(比 AES),但我不确定您正在寻找哪种类型的保护。

Windows 数据保护
http://msdn.microsoft.com/en-us/library/ms995355。 ASPX

I'd like to add to the DPAPI approach.

Although I haven't implemented the user-store approach myself, there is Microsoft documentation for a user-store approach which encrypts and decrypts data for a specific user.

I used the DPAPI using machine store. I'll describe it in case it fits with what you're looking to do. I used a Windows service to load a Windows user profile and that user's password is used to encrypt data.

As a side note, DPAPI uses Triple-DES which may be slightly weaker (than AES), but then I'm not sure what type of protection you're looking for.

Windows Data Protection
http://msdn.microsoft.com/en-us/library/ms995355.aspx

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