System.Security.Cryptography.ProtectedData.Protect Method

Encrypts the data in a specified byte array and returns a byte array that contains the encrypted data.

Syntax

public static byte[] Protect (byte[] userData, byte[] optionalEntropy, DataProtectionScope scope)

Parameters

userData
A byte array that contains data to encrypt.
optionalEntropy
An optional additional byte array used to increase the complexity of the encryption, or null for no additional complexity.
scope
One of the enumeration values that specifies the scope of encryption.

Returns

A byte array representing the encrypted data.

Remarks

This method can be used to encrypt data such as passwords, keys, or connection strings. The optionalEntropy parameter enables you to add data to increase the complexity of the encryption; specify null for no additional complexity. If provided, this information must also be used when decrypting the data using the ProtectedData.Unprotect(Byte[], Byte[], DataProtectionScope) method.

Note:

If you use this method during impersonation, you may receive the following error: "Key not valid for use in specified state." To prevent this error, load the profile of the user you want to impersonate before calling the method.

Requirements

Namespace: System.Security.Cryptography
Assembly: System.Security (in System.Security.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0