2011-11-16 52 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; 
    }