gpt4 book ai didi

c# - 加密部分应用程序配置配置部分 c#

转载 作者:太空宇宙 更新时间:2023-11-03 19:26:35 24 4
gpt4 key购买 nike

我有带有服务器设置的自定义配置部分,其中包括:服务器的用户名、密码和 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?

最佳答案

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

    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;
}

关于c# - 加密部分应用程序配置配置部分 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8152328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com