繁体   English   中英

protectedData API的问题

[英]Issues with protectedData API

我有以下代码,有时应用程序可以成功运行,但对于某些用户而言,它无法解密密码。 当主要在服务器和多用户环境上运行时,会发生这种情况。

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

在服务器端上下文中,您在使用它时会遇到一些问题。 阅读详情:

CurrentUser范围 :受保护的数据与CurrentUser相关联,我的意思是,只有对数据进行加密的用户才能实现对数据的解密-没有其他人。 您可能会像保护个人数据的例程一样理解它。

LocalMachine范围 :如上所述,它允许不同的用户解密数据,但可能会导致安全问题! 使用此范围,即使不在同一组/域中的用户也可以解密数据! 控制不是通过加密例程进行的,而是通过thar服务器的用户访问进行的。

如果您具有公共(或不在域下)服务器,并且需要某些人才能访问某些类型的数据,则可以放弃DataProtectionScope并尝试自定义过程,其中:

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

有关详细信息,请考虑查看此链接: https : //msdn.microsoft.com/zh-cn/library/system.security.cryptography.dataprotectionscope(v=vs.110).aspx

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM