2011-11-16 53 views
1

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

<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的部分内容?

回答

2

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

+0

是的,我已经这样做了,谢谢回复! –

0

App.config对于存储安全证书并不是一个好的地方!

+0

为什么不呢?连接字符串凭证怎么样?你会推荐存储它们在哪里? –

4

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

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