加密 appConfig configSection c# 的部分

发布于 2024-12-16 13:28:55 字数 463 浏览 0 评论 0原文

我有服务器设置的自定义配置部分,其中包括:服务器的用户名、密码和IP;我需要获得这种类型的加密配置:

<ApplicationServerConfiguration>
  <Server UserName="ASDASDASDASDAS [Some encrypted value] ASDASDASF"/>
  <Server Password="ASDASDASDASDAS [Some encrypted value] ASDASDASF"/>
  <Server ServerAddress="192.168.255.255"/> **Not encrypted value!**
</ApplicationServerConfiguration>

我可以加密整个 configSection,但不能加密其中的一部分。谁知道如何仅加密 configSection 的部分内容?

I have custom config section with Server settings, which includes: username, password and IP of the server; I need to get encrypted config with this type:

<ApplicationServerConfiguration>
  <Server UserName="ASDASDASDASDAS [Some encrypted value] ASDASDASF"/>
  <Server Password="ASDASDASDASDAS [Some encrypted value] ASDASDASF"/>
  <Server ServerAddress="192.168.255.255"/> **Not encrypted value!**
</ApplicationServerConfiguration>

I can encrypt whole configSection, but not part of it. Who knows how to encrypt just parts of configSection?

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

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

发布评论

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

评论(3

吹泡泡o 2024-12-23 13:28:55

您可以手动加密和解密它们

    private static string EncryptString(string Value)
    {
        string ReturnValue = string.Empty;

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes("Bermuda"));

        using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
        {
            provider.Key = TDESKey;
            provider.Mode = CipherMode.ECB;
            provider.Padding = PaddingMode.PKCS7;

            ICryptoTransform Encryptor = provider.CreateEncryptor();
            byte[] ByteValue = ASCIIEncoding.ASCII.GetBytes(Value);

            ReturnValue = Convert.ToBase64String(Encryptor.TransformFinalBlock(ByteValue, 0, ByteValue.Length));
        }

        return ReturnValue;
    }
    private static string DecryptString(string EncryptedValue)
    {
        string ReturnValue = string.Empty;

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes("Bermuda"));

        using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
        {
            provider.Key = TDESKey;
            provider.Mode = CipherMode.ECB;
            provider.Padding = PaddingMode.PKCS7;

            ICryptoTransform Decryptor = provider.CreateDecryptor();
            byte[] ByteValue = Convert.FromBase64String(EncryptedValue);

            ReturnValue = ASCIIEncoding.ASCII.GetString(Decryptor.TransformFinalBlock(ByteValue, 0, ByteValue.Length));
        }

        return ReturnValue;
    }

You could manually encrypt and decrypt them

    private static string EncryptString(string Value)
    {
        string ReturnValue = string.Empty;

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes("Bermuda"));

        using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
        {
            provider.Key = TDESKey;
            provider.Mode = CipherMode.ECB;
            provider.Padding = PaddingMode.PKCS7;

            ICryptoTransform Encryptor = provider.CreateEncryptor();
            byte[] ByteValue = ASCIIEncoding.ASCII.GetBytes(Value);

            ReturnValue = Convert.ToBase64String(Encryptor.TransformFinalBlock(ByteValue, 0, ByteValue.Length));
        }

        return ReturnValue;
    }
    private static string DecryptString(string EncryptedValue)
    {
        string ReturnValue = string.Empty;

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes("Bermuda"));

        using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
        {
            provider.Key = TDESKey;
            provider.Mode = CipherMode.ECB;
            provider.Padding = PaddingMode.PKCS7;

            ICryptoTransform Decryptor = provider.CreateDecryptor();
            byte[] ByteValue = Convert.FromBase64String(EncryptedValue);

            ReturnValue = ASCIIEncoding.ASCII.GetString(Decryptor.TransformFinalBlock(ByteValue, 0, ByteValue.Length));
        }

        return ReturnValue;
    }
时光与爱终年不遇 2024-12-23 13:28:55

不可能只加密部分的一部分。如果您希望能够加密用户名和密码值,则必须将它们放入单独的部分。

It is not possible to encrypt only part of a section. You will have to put the UserName and Password values into a separate section if you want to be able to encrypt them.

请叫√我孤独 2024-12-23 13:28:55

App.config 根本不是存储安全凭证的好地方!

The App.config isn't a good place at all to store security credentials!

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