ProtectedData Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides methods for encrypting and decrypting data. This class cannot be inherited.

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


[<HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)>]
type ProtectedData = class end

System_CAPS_pubmethodSystem_CAPS_staticProtect(Byte[], Byte[], DataProtectionScope)

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

System_CAPS_pubmethodSystem_CAPS_staticUnprotect(Byte[], Byte[], DataProtectionScope)

Decrypts the data in a specified byte array and returns a byte array that contains the decrypted data.

This class provides access to the Data Protection API (DPAPI) available in Microsoft Windows 2000 and later operating systems. This is a service that is provided by the operating system and does not require additional libraries. It provides protection using the user or machine credentials to encrypt or decrypt data.

The class consists of two wrappers for the unmanaged DPAPI, Protect and Unprotect. These two methods can be used to encrypt and decrypt data such as passwords, keys, and connection strings.

If you use these methods during impersonation, you may receive the following error: "Key not valid for use in specified state." This occurs because the DPAPI stores the key data in user profiles. If the profile is not loaded, DPAPI won’t be able to perform the decryption. To prevent this error, load the profile of the user you want to impersonate before calling either method. Using DPAPI with impersonation can incur significant complication and requires careful design choices.

The following example shows how to use data protection.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Windows Phone Silverlight
Available since 7.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top