Issues with protectedData API

本文关键字:API protectedData with Issues | 更新日期: 2023-09-27 18:15:01

我有以下代码和应用程序有时成功工作,但对于某些用户它无法解密密码。它主要发生在服务器和多用户环境中,在开发机器上工作得很好。

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

Issues with protectedData API

DataProtectionScope。LocalMachine:此作用域对于解密系统中任何经过身份验证的用户是有效的。

DataProtectionScope。CurrentUser:此作用域仅对其身份用于加密的用户有效,只有该身份可以使其解密。

   public static byte [] Protect( byte [] data )
        {
            try
            {
                return ProtectedData.Protect( data, s_aditionalEntropy, DataProtectionScope.LocalMachine );
            } 
            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
            {
                return ProtectedData.Unprotect( data, s_aditionalEntropy, DataProtectionScope.LocalMachine );
            } 
            catch (CryptographicException e)
            {
                Console.WriteLine("Data was not decrypted. An error occurred.");
                Console.WriteLine(e.ToString());
                return null;
            }
        }

在服务器端上下文中,您有一些问题来利用它。看到细节:

CurrentUser Scope:受保护的数据与CurrentUser相关联,我的意思是,只有加密数据的用户可以实现解密-其他人不能。您可以把它理解为保护个人资料的例行程序。

LocalMachine Scope:如前所述,它允许不同的用户解密数据,但它可能导致安全问题!使用此作用域,即使不在同一组/域的用户也可以解密数据!控制不在加密例程上,而在对该服务器的用户访问中。

如果你有一个公共(或不在域下)服务器,需要一些家伙有访问某些类型的数据,你可以放弃DataProtectionScope,并尝试一个定制的过程,其中:

1 -检查用户是否被授权。你提供加密和解密数据的机制。3 -您可以为不同的用户或组使用不同的密钥。

详细信息,请考虑查看以下链接:https://msdn.microsoft.com/en-us/library/system.security.cryptography.dataprotectionscope (v = vs.110) . aspx