简体   繁体   English

加密 C# 配置文件的密码

[英]Encrypting password for C# configuration file

I'm trying to figure out the best way to encrypt a password that is in a configuration file.我试图找出加密配置文件中密码的最佳方法。 We are not allowed to hard code the passwords.我们不允许对密码进行硬编码。

This a requirement that I have to meet.这是我必须满足的要求。

I'm using DOTNET core and this needs to work on windows 10, windows server and OSX.我正在使用 DOTNET 核心,这需要在 Windows 10、Windows 服务器和 OSX 上运行。

Does anyone have any good ways of meeting this requirement?有没有人有任何好的方法来满足这个要求?

Thank you谢谢

First, read up on RSA Key Containers.首先,阅读 RSA Key Containers。 You are going to have your operations group create one for each of your environments.您将让您的运营团队为您的每个环境创建一个。 They will export them and keep track of the exports for you.他们将导出它们并为您跟踪导出。 They'll also ACL them so that devs have access to them in lower environments, but only ops folks have access in higher environments.他们还将对它们进行 ACL,以便开发人员可以在较低的环境中访问它们,但只有操作人员才能在较高的环境中访问它们。 They also need to add an ACL so that the process identities on your servers have access.他们还需要添加一个 ACL,以便您服务器上的进程标识可以访问。

Those Key Containers will be imported on to the servers, and on any client machines that will be doing any encryption work.这些密钥容器将被导入到服务器上,以及将进行任何加密工作的任何客户端机器上。

Then create a C# class that has the secret name, perhaps an encryption timestamp, and two strings, one for the clear text secret, and one for the encrypted version.然后创建一个 C# 类,该类具有机密名称(可能是加密时间戳)和两个字符串,一个用于明文机密,一个用于加密版本。 Make everything JSON serializable.使一切 JSON 可序列化。 Mark the clear text member so that it doesn't serialize.标记明文成员,使其不会序列化。

Create a small client app (say, with WinForms) that can read in a collection of secrets objects, decrypting any encrypted text using a key in the Key Container.创建一个小型客户端应用程序(例如,使用 WinForms),它可以读取一组机密对象,使用密钥容器中的密钥解密任何加密文本。 The app will allow users to paste in a secret (perhaps one copied from KeePass).该应用程序将允许用户粘贴一个秘密(可能是从 KeePass 复制的)。 When the file is saved, the clear text/pasted in secret is encrypted and serialized out.当文件被保存时,明文/秘密粘贴被加密并序列化。 The clear text version is thrown away.明文版本被丢弃。

At that point, you have a JSON text file that contains one or more encrypted secrets, but those secrets can only be decrypted by processes running with a token that has access to the RSA Key Container.此时,您有一个包含一个或多个加密机密的 JSON 文本文件,但这些机密只能由使用有权访问 RSA 密钥容器的令牌运行的进程解密。 So, you can put the file in source control and rely on your build/deploy to push the file(s) out to your servers.因此,您可以将文件置于源代码控制中,并依靠您的构建/部署将文件推送到您的服务器。

Sorry for the lack of code, but I really can't copy/paste code I wrote for my employer.抱歉缺少代码,但我真的无法复制/粘贴我为雇主编写的代码。 There should be enough here to get you started.这里应该有足够的内容让您入门。

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

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