【问题标题】:Store and encrypt a password locally in C#在 C# 中本地存储和加密密码
【发布时间】:2013-12-08 09:09:20
【问题描述】:

我需要用 C# 开发一个与 SOAP Web 服务交互的工具。该网络服务的第一个操作需要用户名和密码来登录应用程序。但是,在提供用户凭据时,该工具应在没有用户交互的情况下运行。

这意味着该工具知道用户名和密码。将加密的用户名和密码存储在程序代码或外部文件中的或多或少合适的方法是什么?

【问题讨论】:

  • 这个工具会在 IIS 中作为服务运行吗?除了已经提到的答案之外,您还可以使用一些特定于 IIS 的选项。
  • 不,不打算在 IIS 中作为服务运行。只是一个简单的命令行工具:-)
  • 如果帖子对您有帮助,请考虑将其标记为答案

标签: c# web-services password-protection


【解决方案1】:

我会考虑使用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 );

【讨论】:

    【解决方案2】:

    如果您确实需要以某种形式存储密码,最好使用AES 对其进行加密。 AES 是一种经过验证的算法,至少要到下一个十年才能阻止它。请参阅此 SO 链接以查找 C# 中 AES 加密的一些示例:

    Using AES encryption in C#

    【讨论】:

    • 用户不得输入密码。该工具需要在没有用户交互的情况下访问远程系统。
    • 好吧。在这种情况下,您可以在存储密码字符串之前对其进行 AES 加密。请参阅我的更新答案。
    【解决方案3】:

    不太合适的方法是将您的密码以明文形式存储在文本文件或注册表中,这将使获得管理员访问权限的每个人都可以访问您的敏感信息 混淆密码的最佳方法之一是使用此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();
         }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2013-06-02
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多