简体   繁体   English

C# 如何安全存储不变的密码?

[英]C# How to securly store an unchanging password?

I've been looking at this all morning and I'm getting to the point I cannot see the wood forthe trees so looking to the community for suggestion and clear thought.我整个早上都在看这个,我已经到了我看不到树木的地步,所以向社区寻求建议和清晰的想法。

I'm writing an application which will read data from a password protected Zip file supplied by a third party.我正在编写一个应用程序,它将从第三方提供的受密码保护的 Zip 文件中读取数据。 This format will not change (so suggestions to do so cannot be entertained) and neither will the password.这种格式不会改变(因此不能接受这样做的建议),密码也不会改变。

As the password on this Zip will always be the same my problem is where and how to store this password which is secure?由于此 Zip 上的密码将始终相同,因此我的问题是在何处以及如何存储此安全密码? I don't want to store it as a string in the code for obvious reasons.出于明显的原因,我不想将其作为字符串存储在代码中。 And as I need the actual password then storing the Hash of it isn't a route (I don't think).因为我需要实际密码,所以存储它的 Hash 不是一条路线(我不认为)。

I might be over thinking this and there is a simple option but like I say I've lost my tress in the wood:)我可能想多了,有一个简单的选择,但就像我说我在树林里丢了头发:)

EDIT: To give more background to the constraints of this issue:编辑:为了给这个问题的限制提供更多背景:

  • The data I have to read is in the form of a encrypted zip files.我必须读取的数据是加密的 zip 文件的形式。 The password for which is static (eg remains the same on all files)密码为 static(例如,所有文件都保持相同)

  • Many files might come through in a single day and non-regular intervals许多文件可能会在一天之内完成,而且时间间隔不固定

  • The user of the application does not know and not allowed to know the password of the zip file (typing in isn't an option)应用程序的用户不知道也不允许知道 zip 文件的密码(不能输入)

  • The application has to run as a Windows Service and process these应用程序必须作为 Windows 服务运行并处理这些
    files automatically as they are provided and without any user文件在提供时自动提供,无需任何用户
    intervention.干涉。

  • These files and the way they are delivered and formatted is by a 3rd party and I have no control to change these parameters这些文件以及它们的交付和格式化方式是由第 3 方提供的,我无法控制更改这些参数

These are the contraints I've been given for the project and I need to provide a solution to it.这些是我为该项目提供的约束,我需要为其提供解决方案。 I already know storing as a string in the code is WRONG.我已经知道在代码中存储为字符串是错误的。 Repeating this as your reply is not an answer!重复此作为您的回复不是答案!

Thank you to the community for your help:)感谢社区的帮助:)

Nowhere.无处。 THERE IS NO WAY TO STORE A PASSWORD IN A SAFE WAY AND STILL USE IT.没有办法以安全的方式存储密码并继续使用它。 And yes, this is all caps.是的,这都是大写。 You can try to mitigate the damage, but at the end if your app can decode whatevery you use to store the password, then so can a hacker.您可以尝试减轻损失,但最后,如果您的应用程序可以解码您用来存储密码的任何内容,那么黑客也可以。

CODE is a bad place - not for security, but because it is REALLY unchangeable.代码是一个糟糕的地方——不是为了安全,而是因为它真的是不可改变的。 But otherwise - no way.但除此之外 - 没办法。 Simple.简单的。 People tried hiding things since computers where invented.自从计算机发明以来,人们就试图隐藏东西。 Never worked.从来没有工作过。

I would suggest here one thing.我在这里建议一件事。

You can not store the password directly in your application.您不能将密码直接存储在您的应用程序中。

You can always make it a combination of something like date of birth + first 4 characters of first name + some other information您始终可以将其组合为出生日期 + 名字的前 4 个字符 + 其他一些信息

It's possible to encrypt sections in the configuration file .可以对配置文件中的部分进行加密 The tool only works with web.config files, so if you have another type of application simply rename the app.config to web.config before running the command and change back to app.config afterwards.该工具仅适用于 web.config 文件,因此如果您有其他类型的应用程序,只需在运行命令之前将 app.config 重命名为 web.config,然后再改回 app.config。

Using machine store essentially restricts access to administrators on the machine, and using user store restricts access to only the account that performed the encryption command.使用机器存储实质上限制了对机器管理员的访问,而使用用户存储将访问限制为仅对执行加密命令的帐户的访问。

To encrypt with machine store:使用机器存储加密:

To encrypt:加密:

aspnet_regiis.exe -pef "sectionName" "C:\Path\To\Application" -prov "DataProtectionConfigurationProvider"

To decrypt:解密:

aspnet_regiis.exe -pdf "sectionName" "C:\Path\To\Application"

To encrypt with user store使用用户存储加密

Add this section to the config file:将此部分添加到配置文件中:

<configProtectedData>
        <providers>
            <add useMachineProtection="false"
                 keyEntropy=""
                 name="CustomDataProtectionConfigurationProvider"
                 type="System.Configuration.DpapiProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
            />      
        </providers>
</configProtectedData>

To encrypt:加密:

aspnet_regiis.exe -pef "sectionName" "C:\Path\To\Application" -prov "CustomDataProtectionConfigurationProvider"

To decrypt:解密:

aspnet_regiis.exe -pef "sectionName" "C:\Path\To\Application" -prov "CustomDataProtectionConfigurationProvider"

Note for user store用户存储注意事项

Since only the user running the command can access/decrypt the data the application will need a dedicated service account, and to run commands as that user runas /profile /user:theusername cmd can be used to start a command prompt as another user由于只有运行命令的用户才能访问/解密数据,因此应用程序需要一个专用的服务帐户,并以该用户身份运行命令runas /profile /user:theusername cmd可用于以另一个用户身份启动命令提示符

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

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