ProtectedData.Protect Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Security.Cryptography
Assembly:  mscorlib.Extensions (in mscorlib.Extensions.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function Protect ( _
    userData As Byte(), _
    optionalEntropy As Byte() _
) As Byte()
[SecuritySafeCriticalAttribute]
public static byte[] Protect(
    byte[] userData,
    byte[] optionalEntropy
)

Parameters

  • userData
    Type: array<System.Byte[]
    A byte array that contains data to encrypt.
  • optionalEntropy
    Type: array<System.Byte[]
    An optional additional byte array used to increase the complexity of the encryption, or nulla null reference (Nothing in Visual Basic) for no additional complexity.

Return Value

Type: array<System.Byte[]
A byte array representing the encrypted data.

Exceptions

Exception Condition
ArgumentNullException

The userData parameter is nulla null reference (Nothing in Visual Basic).

CryptographicException

The encryption failed.

NotSupportedException

The operating system does not support this method.

OutOfMemoryException

The system ran out of memory while encrypting the 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 nulla null reference (Nothing in Visual Basic) for no additional complexity. If provided, this information must also be used when decrypting the data using the Unprotect method.

NoteNote:

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.

Version Information

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.