簡體   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