ProtectedData.Protect メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ProtectedData.Protect メソッドの意味・解説 

ProtectedData.Protect メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

userData パラメータ保護しバイト配列返します

名前空間: System.Security.Cryptography
アセンブリ: System.Security (system.security.dll 内)
構文構文

Public Shared Function Protect
 ( _
    userData As Byte(), _
    optionalEntropy As Byte(), _
    scope As DataProtectionScope _
) As Byte()
Dim userData As Byte()
Dim optionalEntropy As Byte()
Dim scope As DataProtectionScope
Dim returnValue As Byte()

returnValue = ProtectedData.Protect(userData, optionalEntropy, scope)
public static byte[] Protect (
    byte[] userData,
    byte[] optionalEntropy,
    DataProtectionScope scope
)
public:
static array<unsigned char>^ Protect
 (
    array<unsigned char>^ userData, 
    array<unsigned char>^ optionalEntropy, 
    DataProtectionScope scope
)
public static byte[] Protect (
    byte[] userData, 
    byte[] optionalEntropy, 
    DataProtectionScope scope
)
public static function Protect
 (
    userData : byte[], 
    optionalEntropy : byte[], 
    scope : DataProtectionScope
) : byte[]

パラメータ

userData

保護するデータ格納されているバイト配列

optionalEntropy

データ暗号化使用される追加バイト配列

scope

DataProtectionScope 値の 1 つ

戻り値
暗号化されたデータを表すバイト配列

例外例外
例外種類条件

ArgumentNullException

userData パラメータnull 参照 (Visual Basic では Nothing) です。

CryptographicException

暗号化による保護失敗しました

PlatformNotSupportedException

このメソッドオペレーティング システムサポートされていません。このメソッドは、Microsoft Windows 2000 以降オペレーティング システムでのみ使用できます

OutOfMemoryException

データ暗号化処理中にシステム使用できるメモリなくなりました

解説解説
使用例使用例

データ保護使用方法次のコード例示します

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

}
#using <System.Security.dll>

using namespace System;
using namespace System::Security::Cryptography;

public ref class DataProtectionSample
{
private:

   // Create byte array for additional entropy when using Protect method.
   static array<Byte>^s_aditionalEntropy = {9,8,7,6,5};

public:
   static void Main()
   {
      
      // Create a simple byte array containing data to be encrypted.
      array<Byte>^secret = {0,1,2,3,4,1,2,3,4};
      
      //Encrypt the data.
      array<Byte>^encryptedSecret = Protect( secret );
      Console::WriteLine( "The encrypted byte array is:" );
      PrintValues( encryptedSecret );
      
      // Decrypt the data and store in a byte array.
      array<Byte>^originalData = Unprotect( encryptedSecret );
      Console::WriteLine( "{0}The original data is:", Environment::NewLine
 );
      PrintValues( originalData );
   }

   static array<Byte>^ Protect( array<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 );
         return nullptr;
      }
   }

   static array<Byte>^ Unprotect( array<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 );
         return nullptr;
      }
   }

   static void PrintValues( array<Byte>^myArr
 )
   {
      System::Collections::IEnumerator^ myEnum = myArr->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Byte i = safe_cast<Byte>(myEnum->Current);
         Console::Write( "\t{0}", i );
      }

      Console::WriteLine();
   }
};

int main()
{
   DataProtectionSample::Main();
}
import System.*;
import System.Security.Cryptography.*;

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

    public static void main(String
 args[])
    {
        // Create a simple byte array containing data to be encrypted.
        ubyte secret[] =  { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
        //Encrypt the data.
        ubyte encryptedSecret[] = Protect(secret);
        Console.WriteLine("The encrypted byte array is:");
        PrintValues(encryptedSecret);
        // Decrypt the data and store in a byte array.
        ubyte originalData[] = Unprotect(encryptedSecret);
        Console.WriteLine("{0}The original data is:", 
            Environment.get_NewLine());
        PrintValues(originalData);
    } //main

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

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

    public static void PrintValues(ubyte
 myArr[])
    {
        for (int iCtr = 0; iCtr < myArr.length;
 iCtr++) {
            ubyte i = myArr[iCtr];
            Console.Write("\t{0}", System.Convert.ToString(i));
        }
        Console.WriteLine();
    } //PrintValues
} //DataProtectionSample 
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ProtectedData クラス
ProtectedData メンバ
System.Security.Cryptography 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からProtectedData.Protect メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からProtectedData.Protect メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からProtectedData.Protect メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ProtectedData.Protect メソッド」の関連用語

ProtectedData.Protect メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ProtectedData.Protect メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS