2013-12-08 41 views
3

我需要在C#中開發一個與SOAP Web服務交互的工具。這個web服務的第一個操作是使用用戶名和密碼登錄到應用程序。但是,提供用戶證書時,該工具應在沒有用戶交互的情況下運行。在C#中本地存儲和加密密碼

這意味着該工具知道用戶名和密碼。將加密的用戶名和密碼存儲在程序代碼或外部文件中,或多或少是合適的方式?

+0

此工具是否將作爲運行在IIS服務?除了已經提到的答案之外,還有一些IIS特定的選項可供您使用。 –

+0

不,它不計劃在IIS中作爲服務運行。只是一個簡單的命令行工具:-) –

+0

如果一個職位幫助你,請考慮標記爲答案 –

回答

1

如果確實需要以某種形式存儲密碼,請使用AES對其進行更好的加密。 AES是一種經過驗證的算法,至少在未來十年內不可行。看到這個鏈接SO找到的AES加密一些例子在C#:

Using AES encryption in C#

+0

用戶不得輸入密碼。該工具需要訪問遠程系統而無需用戶交互。 –

+1

okies。在這種情況下,您可以在存儲密碼之前對其進行AES加密。看到我更新的答案。 –

4

我會考慮使用DPAPI應該有一個機器特定的算法。應該確保數據只能在加密的機器上解密。

在鏈接後也是一個不錯的article鏈接

MSDN

// Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted 

// only by the same current user. 

return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
1

的不太合適的方式是存儲在清除密碼以純文本的文本文件或註冊表其中將使您的敏感信息可供所有以管理員身份訪問的人訪問 混淆密碼的最佳方式之一是使用此ProtectedData Class
這裏是來自MSDN的示例:

using System; 
using System.Security.Cryptography; 

public class DataProtectionSample 
{ 
// Create byte array for additional entropy when using Protect method. 
    static byte [] s_aditionalEntropy = { 9, 8, 7, 6, 5 }; 

    public static void Main() 
    { 
// Create a simple byte array containing data to be encrypted. 

byte [] secret = { 0, 1, 2, 3, 4, 1, 2, 3, 4 }; 

//Encrypt the data. 
     byte [] encryptedSecret = Protect(secret); 
     Console.WriteLine("The encrypted byte array is:"); 
     PrintValues(encryptedSecret); 

// Decrypt the data and store in a byte array. 
     byte [] originalData = Unprotect(encryptedSecret); 
     Console.WriteLine("{0}The original data is:", Environment.NewLine); 
     PrintValues(originalData); 

    } 

    public static byte [] Protect(byte [] data) 
    { 
     try 
     { 
      // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted 
      // only by the same current user. 
      return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
     } 
     catch (CryptographicException e) 
     { 
      Console.WriteLine("Data was not encrypted. An error occurred."); 
      Console.WriteLine(e.ToString()); 
      return null; 
     } 
    } 

    public static byte [] Unprotect(byte [] data) 
    { 
     try 
     { 
      //Decrypt the data using DataProtectionScope.CurrentUser. 
      return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
     } 
     catch (CryptographicException e) 
     { 
      Console.WriteLine("Data was not decrypted. An error occurred."); 
      Console.WriteLine(e.ToString()); 
      return null; 
     } 
    } 

    public static void PrintValues(Byte[] myArr) 
    { 
      foreach (Byte i in myArr) 
      { 
       Console.Write("\t{0}", i); 
      } 
     Console.WriteLine(); 
    } 

}