Protect Method
Collapse the table of content
Expand the table of content

ProtectedData.Protect Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

public static byte[] Protect(
	byte[] userData,
	byte[] optionalEntropy


Type: System.Byte []
A byte array that contains data to encrypt.
Type: System.Byte []
An optional additional byte array used to increase the complexity of the encryption, or null for no additional complexity.

Return Value

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


The userData parameter is null.


The encryption failed.


The operating system does not support this method.


The system ran out of memory while encrypting the data.

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 Unprotect method.


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.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

© 2017 Microsoft