ProtectedData.Protect メソッド
アセンブリ: 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 array<unsigned char>^ Protect ( array<unsigned char>^ userData, array<unsigned char>^ optionalEntropy, DataProtectionScope scope )
public static function Protect ( userData : byte[], optionalEntropy : byte[], scope : DataProtectionScope ) : byte[]
戻り値
暗号化されたデータを表すバイト配列。


このメソッドを使用して、パスワード、キー、接続文字列などのデータを保護できます。optionalEntropy パラメータを使用すると、追加情報を使用してデータを保護できます。この情報は、Unprotect メソッドを使用してデータの保護を解除するときにも使用する必要があります。

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


Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からProtectedData.Protect メソッドを検索する場合は、下記のリンクをクリックしてください。

- ProtectedData.Protect メソッドのページへのリンク