using System; using System.Runtime.InteropServices; using System.Text; using Vanara.InteropServices; namespace Vanara.PInvoke { /// Methods and data types found in ncrypt.dll. public static partial class NCrypt { /// /// /// The PFNCryptStreamOutputCallback function receives encrypted or decrypted data from tasks started by using the /// NCryptStreamOpenToProtect or NCryptStreamOpenToUnprotect functions. This callback must be defined by your application using the /// following syntax. /// /// /// /// Pointer to data that you can use to keep track of your application. The data is not modified by the data protection API. /// /// Note You can set a pointer to your context data in the pvCallbackCtxt member of the NCRYPT_PROTECT_STREAM_INFO /// structure before passing a pointer to that structure in the pStreamInfo parameter of the NCryptStreamOpenToProtect or /// NCryptStreamOpenToUnprotect functions. /// /// /// /// Pointer to a block of processed data that can be used by the application. /// /// /// The size, in bytes, of the processed data pointed to by the pbData parameter. /// /// /// /// If this value is TRUE, the current data block is the last to be processed and this is the last time the callback will be called. /// /// /// /// /// If you return any status code other than ERROR_SUCCESS from your implementation of this callback function, the stream /// encryption or decryption process will fail. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// /// /// /// Set a pointer to this callback function in the pfnStreamOutput member of the NCRYPT_PROTECT_STREAM_INFO structure. Set a /// pointer to the structure in the pStreamInfo parameter of the NCryptStreamOpenToProtect or NCryptStreamOpenToUnprotect functions. /// /// /// You can use this callback to further process the encrypted or decrypted data. A common use of the function is to write the data /// to disk as it is received from the data protection API. The blocks of encrypted or unencrypted data are created by the /// NCryptStreamUpdate function. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nc-ncryptprotect-pfncryptstreamoutputcallback // PFNCryptStreamOutputCallback Pfncryptstreamoutputcallback; SECURITY_STATUS Pfncryptstreamoutputcallback( void *pvCallbackCtxt, // const BYTE *pbData, SIZE_T cbData, BOOL fFinal ) {...} [PInvokeData("ncryptprotect.h", MSDNShortId = "D07B2B63-306B-4C41-AA14-320EFEFFB939")] [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate HRESULT PFNCryptStreamOutputCallback(IntPtr pvCallbackCtxt, IntPtr pbData, SizeT cbData, [MarshalAs(UnmanagedType.Bool)] bool fFinal); /// /// The PFNCryptStreamOutputCallbackEx function receives encrypted or decrypted data from tasks started by using the /// NCryptStreamOpenToProtectEx or NCryptStreamOpenToUnprotectEx functions. This callback must be defined by your application using /// the following syntax. /// /// The arguments specified by NCRYPT_PROTECT_STREAM_INFO_EX. /// /// A pointer to a block of processed data that is available to the application. If data is not available yet, but the descriptor is, /// this will be NULL. /// /// The size, in bytes, of the processed data pointed to by the pbData parameter. /// Handle of Protection Descriptor. /// /// If this value is TRUE, the current data block is the last to be processed and this is the last time the callback will be called. /// /// /// /// If you return any status code other than ERROR_SUCCESS from your implementation of this callback function, the stream /// encryption or decryption process will fail. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// /// /// /// Set a pointer to this callback function in the pfnStreamOutput member of the NCRYPT_PROTECT_STREAM_INFO structure. Set a /// pointer to the structure in the pStreamInfo parameter of the NCryptStreamOpenToProtect or NCryptStreamOpenToUnprotect functions. /// /// /// You can use this callback to further process the encrypted or decrypted data. A common use of the function is to write the data /// to disk as it is received from the data protection API. The blocks of encrypted or unencrypted data are created by the /// NCryptStreamUpdate function. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nc-ncryptprotect-pfncryptstreamoutputcallback [PInvokeData("ncryptprotect.h")] [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate HRESULT PFNCryptStreamOutputCallbackEx(IntPtr pvCallbackCtxt, IntPtr pbData, SizeT cbData, NCRYPT_DESCRIPTOR_HANDLE hDescriptor, [MarshalAs(UnmanagedType.Bool)] bool fFinal); /// Flags for NCryptCreateProtectionDescriptor. [PInvokeData("ncryptprotect.h", MSDNShortId = "BA6B15AC-2CD8-4D9A-817F-65CF9C09D22C")] [Flags] public enum CreateProtectionDescriptorFlags { /// /// To indicate that the string is a display name and that it is saved, along with its associated descriptor string rule, in the /// HKEY_CURRENT_USER registry hive, set only the NCRYPT_NAMED_DESCRIPTOR_FLAG value. That is, there is no unique flag to specify /// the current user registry hive. /// NCRYPT_NAMED_DESCRIPTOR_FLAG = 0x00000001, /// /// To indicate that the string is a display name and that it is saved, along with its associated descriptor rule string, in the /// HKEY_LOCAL_MACHINE registry hive, bitwise-OR the NCRYPT_NAMED_DESCRIPTOR_FLAG value and the NCRYPT_MACHINE_KEY_FLAG value. /// NCRYPT_MACHINE_KEY_FLAG = 0x00000020, } /// Flags for NCryptProtectSecret. [PInvokeData("ncryptprotect.h", MSDNShortId = "8726F92B-34D5-4696-8803-3D7F50F1006D")] [Flags] public enum ProtectFlags { /// Requests that the key service provider not display a user interface. NCRYPT_SILENT_FLAG = 0x00000040, } /// Flags used by NCryptGetProtectionDescriptorInfo. [PInvokeData("ncryptprotect.h", MSDNShortId = "EF4777D5-E218-4868-8D25-58E0EF8C9D30")] [Flags] public enum ProtectionDescriptorInfoType { /// The ppvInfo argument returns the descriptor rule string. [CorrespondingType(typeof(string))] NCRYPT_PROTECTION_INFO_TYPE_DESCRIPTOR_STRING = 0x00000001, } /// Flags for NCryptQueryProtectionDescriptorName. [PInvokeData("ncryptprotect.h", MSDNShortId = "32953AEC-01EE-4ED1-80F3-29963F43004F")] [Flags] public enum ProtectionDescriptorNameFlags { /// /// To indicate that the string is a display name and that it is saved, along with its associated descriptor rule string, in the /// HKEY_LOCAL_MACHINE registry hive, bitwise-OR the NCRYPT_NAMED_DESCRIPTOR_FLAG value and the NCRYPT_MACHINE_KEY_FLAG value. /// NCRYPT_MACHINE_KEY_FLAG = 0x00000020, } /// Flags used by NCryptUnprotectSecret. [PInvokeData("ncryptprotect.h", MSDNShortId = "F532F0ED-36F4-47E3-B478-089CC083E5D1")] [Flags] public enum UnprotectSecretFlags { /// Decodes only the header of the protected data blob. No actual decryption takes place. NCRYPT_UNPROTECT_NO_DECRYPT = 0x00000001, /// Requests that the key service provider not display a user interface. NCRYPT_SILENT_FLAG = 0x00000040, } /// /// The NCryptCloseProtectionDescriptor function zeros and frees a protection descriptor object and releases its handle. /// /// /// Handle of a protection descriptor created by calling NCryptCreateProtectionDescriptor. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_HANDLE /// The handle specified by the hDescriptor parameter cannot be NULL and it must represent a valid descriptor. /// /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptcloseprotectiondescriptor // SECURITY_STATUS NCryptCloseProtectionDescriptor( NCRYPT_DESCRIPTOR_HANDLE hDescriptor ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "523FD83E-85A3-4A0E-BA8D-2F27F82C1072")] public static extern HRESULT NCryptCloseProtectionDescriptor(NCRYPT_DESCRIPTOR_HANDLE hDescriptor); /// /// The NCryptCreateProtectionDescriptor function retrieves a handle to a protection descriptor object. /// /// /// /// Null-terminated Unicode string that contains a protection descriptor rule string or a registered display name for the rule. /// /// /// If you specify the display name and you want this function to look in the registry for the associated protection descriptor rule /// string, you must set the dwFlags parameter to NCRYPT_NAMED_DESCRIPTOR_FLAG. /// /// /// /// /// Flag that specifies whether the string in pwszDescriptorString represents the display name of a protection descriptor and, if so, /// where in the registry the function should search for the associated protection rule string. The following value combinations can /// be set: /// /// /// /// /// To indicate that the value set in the pwszDescriptorString parameter is a complete protection descriptor rule string rather than /// a display name, set the dwFlags parameter to zero (0). /// /// /// /// /// To indicate that the string is a display name and that it is saved, along with its associated descriptor rule string, in the /// HKEY_LOCAL_MACHINE registry hive, bitwise-OR the NCRYPT_NAMED_DESCRIPTOR_FLAG value and the /// NCRYPT_MACHINE_KEY_FLAG value. /// /// /// /// /// To indicate that the string is a display name and that it is saved, along with its associated descriptor string rule, in the /// HKEY_CURRENT_USER registry hive, set only the NCRYPT_NAMED_DESCRIPTOR_FLAG value. That is, there is no unique flag /// to specify the current user registry hive. /// /// /// /// /// Note To associate a descriptor rule with a display name and save both in the registry, call the /// NCryptRegisterProtectionDescriptorName function. /// /// /// /// Pointer to a protection descriptor object handle. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_PARAMETER /// /// The phDescriptor parameter cannot be NULL. The pwszDescriptorString parameter cannot be NULL and it cannot be an empty sting. /// /// /// /// NTE_BAD_FLAGS /// The dwFlags parameter must be NCRYPT_MACHINE_KEY_FLAG or NCRYPT_NAMED_DESCRIPTOR_FLAG. /// /// /// NTE_NO_MEMORY /// Memory could not be allocated to retrieve the registered protection descriptor string. /// /// /// NTE_NOT_FOUND /// The protection descriptor name specified in the pwszDescriptorString parameter could not be found. /// /// /// /// /// /// The protection descriptor object created by this function is an internal data structure that contains information about the /// descriptor. You cannot use it directly. Your application can, however, use the returned handle in the following functions: /// /// /// /// NCryptCloseProtectionDescriptor /// /// /// NCryptGetProtectionDescriptorInfo /// /// /// NCryptProtectSecret /// /// /// NCryptProtectSecret /// /// /// NCryptUnprotectSecret /// /// /// NCryptStreamOpenToProtect /// /// /// The following examples show protection descriptor rule strings: /// /// /// "SID=S-1-5-21-4392301 AND SID=S-1-5-21-3101812" /// /// /// "SDDL=O:S-1-5-5-0-290724G:SYD:(A;;CCDC;;;S-1-5-5-0-290724)(A;;DC;;;WD)" /// /// /// "LOCAL=user" /// /// /// "LOCAL=machine" /// /// /// "WEBCREDENTIALS=MyPasswordName" /// /// /// "WEBCREDENTIALS=MyPasswordName,myweb.com" /// /// /// /// You can use the NCryptRegisterProtectionDescriptorName function to associate a display name with a rule string and save both in /// the registry. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptcreateprotectiondescriptor // SECURITY_STATUS NCryptCreateProtectionDescriptor( LPCWSTR pwszDescriptorString, DWORD dwFlags, NCRYPT_DESCRIPTOR_HANDLE // *phDescriptor ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "BA6B15AC-2CD8-4D9A-817F-65CF9C09D22C")] public static extern HRESULT NCryptCreateProtectionDescriptor([MarshalAs(UnmanagedType.LPWStr)] string pwszDescriptorString, CreateProtectionDescriptorFlags dwFlags, out SafeNCRYPT_DESCRIPTOR_HANDLE phDescriptor); /// /// The NCryptGetProtectionDescriptorInfo function retrieves a protection descriptor rule string. /// /// /// Protection descriptor handle created by calling NCryptCreateProtectionDescriptor. /// /// /// /// Pointer to an NCRYPT_ALLOC_PARA structure that you can use to specify custom memory management functions. If you set this /// argument to NULL, the LocalAlloc function is used internally to allocate memory and your application must call LocalFree /// to release memory pointed to by the ppvInfo parameter. /// /// /// /// Specifies how to return descriptor information to the ppvInfo parameter. This can be the following value: /// /// /// Value /// Meaning /// /// /// NCRYPT_PROTECTION_INFO_TYPE_DESCRIPTOR_STRING /// The ppvInfo argument returns the descriptor rule string. /// /// /// /// /// Pointer to the descriptor information. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_PARAMETER /// The ppvInfo parameter cannot be NULL. /// /// /// NTE_NOT_SUPPORTED /// An unsupported value was specified in the dwInfoType parameter. /// /// /// NTE_INVALID_HANDLE /// The handle specified by the hDescriptor parameter is not valid. /// /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptgetprotectiondescriptorinfo // SECURITY_STATUS NCryptGetProtectionDescriptorInfo( NCRYPT_DESCRIPTOR_HANDLE hDescriptor, const NCRYPT_ALLOC_PARA *pMemPara, DWORD // dwInfoType, void **ppvInfo ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "EF4777D5-E218-4868-8D25-58E0EF8C9D30")] public static extern HRESULT NCryptGetProtectionDescriptorInfo(NCRYPT_DESCRIPTOR_HANDLE hDescriptor, in NCRYPT_ALLOC_PARA pMemPara, ProtectionDescriptorInfoType dwInfoType, out IntPtr ppvInfo); /// /// The NCryptGetProtectionDescriptorInfo function retrieves a protection descriptor rule string. /// /// /// Protection descriptor handle created by calling NCryptCreateProtectionDescriptor. /// /// /// /// Pointer to an NCRYPT_ALLOC_PARA structure that you can use to specify custom memory management functions. If you set this /// argument to NULL, the LocalAlloc function is used internally to allocate memory and your application must call LocalFree /// to release memory pointed to by the ppvInfo parameter. /// /// /// /// Specifies how to return descriptor information to the ppvInfo parameter. This can be the following value: /// /// /// Value /// Meaning /// /// /// NCRYPT_PROTECTION_INFO_TYPE_DESCRIPTOR_STRING /// The ppvInfo argument returns the descriptor rule string. /// /// /// /// /// Pointer to the descriptor information. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_PARAMETER /// The ppvInfo parameter cannot be NULL. /// /// /// NTE_NOT_SUPPORTED /// An unsupported value was specified in the dwInfoType parameter. /// /// /// NTE_INVALID_HANDLE /// The handle specified by the hDescriptor parameter is not valid. /// /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptgetprotectiondescriptorinfo // SECURITY_STATUS NCryptGetProtectionDescriptorInfo( NCRYPT_DESCRIPTOR_HANDLE hDescriptor, const NCRYPT_ALLOC_PARA *pMemPara, DWORD // dwInfoType, void **ppvInfo ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "EF4777D5-E218-4868-8D25-58E0EF8C9D30")] public static extern HRESULT NCryptGetProtectionDescriptorInfo(NCRYPT_DESCRIPTOR_HANDLE hDescriptor, [Optional] IntPtr pMemPara, ProtectionDescriptorInfoType dwInfoType, out IntPtr ppvInfo); /// /// /// The NCryptProtectSecret function encrypts data to a specified protection descriptor. Call NCryptUnprotectSecret to decrypt /// the data. /// /// /// /// Handle of the protection descriptor object. Create the handle by calling NCryptCreateProtectionDescriptor. /// /// /// The flag can be zero or the following value. /// /// /// Value /// Meaning /// /// /// NCRYPT_SILENT_FLAG /// Requests that the key service provider not display a user interface. /// /// /// /// /// Pointer to the byte array to be protected. /// /// /// Number of bytes in the binary array specified by the pbData parameter. /// /// /// /// Pointer to an NCRYPT_ALLOC_PARA structure that you can use to specify custom memory management functions. If you set this /// argument to NULL, the LocalAlloc function is used internally to allocate memory and your application must call LocalFree /// to release memory pointed to by the ppbProtectedBlob parameter. /// /// /// /// Handle to the parent window of the user interface, if any, to be displayed. /// /// /// Address of a variable that receives a pointer to the encrypted data. /// /// /// /// Pointer to a ULONG variable that contains the size, in bytes, of the encrypted data pointed to by the ppbProtectedBlob variable. /// /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_PARAMETER /// /// The pbData, ppbProtectedBlob, and pcbProtectedBlob parameters cannot be NULL. The cbData parameter cannot be less than one. /// /// /// /// NTE_NO_MEMORY /// Insufficient memory exists to allocate the content encryption key. /// /// /// NTE_INVALID_HANDLE /// The handle specified by the hDescriptor parameter is not valid. /// /// /// /// /// /// Use the NCryptProtectSecret function to protect keys, key material, and passwords. Use the NCryptStreamOpenToProtect and /// the NCryptStreamUpdate functions to encrypt larger messages. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptprotectsecret SECURITY_STATUS // NCryptProtectSecret( NCRYPT_DESCRIPTOR_HANDLE hDescriptor, DWORD dwFlags, const BYTE *pbData, ULONG cbData, const // NCRYPT_ALLOC_PARA *pMemPara, HWND hWnd, BYTE **ppbProtectedBlob, ULONG *pcbProtectedBlob ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "8726F92B-34D5-4696-8803-3D7F50F1006D")] public static extern HRESULT NCryptProtectSecret(NCRYPT_DESCRIPTOR_HANDLE hDescriptor, ProtectFlags dwFlags, [In] IntPtr pbData, uint cbData, in NCRYPT_ALLOC_PARA pMemPara, HWND hWnd, out IntPtr ppbProtectedBlob, out uint pcbProtectedBlob); /// /// /// The NCryptProtectSecret function encrypts data to a specified protection descriptor. Call NCryptUnprotectSecret to decrypt /// the data. /// /// /// /// Handle of the protection descriptor object. Create the handle by calling NCryptCreateProtectionDescriptor. /// /// /// The flag can be zero or the following value. /// /// /// Value /// Meaning /// /// /// NCRYPT_SILENT_FLAG /// Requests that the key service provider not display a user interface. /// /// /// /// /// Pointer to the byte array to be protected. /// /// /// Number of bytes in the binary array specified by the pbData parameter. /// /// /// /// Pointer to an NCRYPT_ALLOC_PARA structure that you can use to specify custom memory management functions. If you set this /// argument to NULL, the LocalAlloc function is used internally to allocate memory and your application must call LocalFree /// to release memory pointed to by the ppbProtectedBlob parameter. /// /// /// /// Handle to the parent window of the user interface, if any, to be displayed. /// /// /// Address of a variable that receives a pointer to the encrypted data. /// /// /// /// Pointer to a ULONG variable that contains the size, in bytes, of the encrypted data pointed to by the ppbProtectedBlob variable. /// /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_PARAMETER /// /// The pbData, ppbProtectedBlob, and pcbProtectedBlob parameters cannot be NULL. The cbData parameter cannot be less than one. /// /// /// /// NTE_NO_MEMORY /// Insufficient memory exists to allocate the content encryption key. /// /// /// NTE_INVALID_HANDLE /// The handle specified by the hDescriptor parameter is not valid. /// /// /// /// /// /// Use the NCryptProtectSecret function to protect keys, key material, and passwords. Use the NCryptStreamOpenToProtect and /// the NCryptStreamUpdate functions to encrypt larger messages. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptprotectsecret SECURITY_STATUS // NCryptProtectSecret( NCRYPT_DESCRIPTOR_HANDLE hDescriptor, DWORD dwFlags, const BYTE *pbData, ULONG cbData, const // NCRYPT_ALLOC_PARA *pMemPara, HWND hWnd, BYTE **ppbProtectedBlob, ULONG *pcbProtectedBlob ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "8726F92B-34D5-4696-8803-3D7F50F1006D")] public static extern HRESULT NCryptProtectSecret(NCRYPT_DESCRIPTOR_HANDLE hDescriptor, ProtectFlags dwFlags, [In] IntPtr pbData, uint cbData, [Optional] IntPtr pMemPara, HWND hWnd, out IntPtr ppbProtectedBlob, out uint pcbProtectedBlob); /// /// /// The NCryptQueryProtectionDescriptorName function retrieves the protection descriptor rule string associated with a /// registered descriptor display name. /// /// /// /// /// The registered display name for the protection descriptor. Register a name by calling the NCryptRegisterProtectionDescriptorName function. /// /// /// /// /// A null-terminated Unicode string that contains the protection descriptor rule. Set this value to NULL and set the size of /// the descriptor string pointed to by pcDescriptorString argument to zero on your initial call to this function. For more /// information, see Remarks. /// /// /// /// /// Pointer to a variable that contains the number of characters in the string retrieved in the pwszDescriptorString parameter. Set /// the variable to zero on your initial call to this function. For more information, see Remarks. /// /// /// /// /// Flag that specifies which registry hive to query for the registered name. This can be zero to look in the /// HKEY_CURRENT_USER hive or you can specify NCRYPT_MACHINE_KEY_FLAG to query the HKEY_LOCAL_MACHINE hive. /// /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_PARAMETER /// The pwszName parameter cannot be NULL, and the value pointed to by the parameter cannot be an empty string. /// /// /// NTE_BAD_FLAGS /// The dwFlags parameter must be zero or NCRYPT_MACHINE_KEY_FLAG. /// /// /// /// /// /// To retrieve a protection descriptor rule string, you must call this function twice. The first time you call, set the /// pwszDescriptorString argument to NULL and the value pointed to by the pcDescriptorString argument to zero. Your first call /// retrieves the number of characters in the descriptor string. Use this number to allocate memory for the string and retrieve a /// pointer to the allocated buffer. To retrieve the string, call the function again using the pointer. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptqueryprotectiondescriptorname // SECURITY_STATUS NCryptQueryProtectionDescriptorName( LPCWSTR pwszName, LPWSTR pwszDescriptorString, SIZE_T *pcDescriptorString, // DWORD dwFlags ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("ncryptprotect.h", MSDNShortId = "32953AEC-01EE-4ED1-80F3-29963F43004F")] public static extern HRESULT NCryptQueryProtectionDescriptorName(string pwszName, StringBuilder pwszDescriptorString, out SizeT pcDescriptorString, ProtectionDescriptorNameFlags dwFlags); /// /// /// The NCryptRegisterProtectionDescriptorName function registers the display name and the associated rule string for a /// protection descriptor. /// /// /// /// Pointer to a null-terminated Unicode string that contains the display name of the descriptor to be registered. /// /// /// /// Pointer to a null-terminated Unicode string that contains a protection descriptor rule. If this parameter is NULL or the /// string is empty, the registry value previously created for the pwszName parameter will be deleted. /// /// /// /// /// A constant that indicates the registry hive under which to register the new entry. If this value is zero (0), the registry root /// is HKEY_CURRENT_USER. If this value is NCRYPT_MACHINE_KEY_FLAG, the root is HKEY_LOCAL_MACHINE. /// /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_PARAMETER /// The pwszName parameter cannot be NULL, and the value pointed to by the parameter cannot be an empty string. /// /// /// NTE_BAD_FLAGS /// The dwFlags parameter must be zero or NCRYPT_MACHINE_KEY_FLAG. /// /// /// /// /// /// The registry key created by using this function is not volatile. The information is stored in a file and preserved when the /// computer shuts down. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptregisterprotectiondescriptorname // SECURITY_STATUS NCryptRegisterProtectionDescriptorName( LPCWSTR pwszName, LPCWSTR pwszDescriptorString, DWORD dwFlags ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("ncryptprotect.h", MSDNShortId = "DAB03CB2-630F-4BB3-93BD-06BE9126B1C4")] public static extern HRESULT NCryptRegisterProtectionDescriptorName(string pwszName, string pwszDescriptorString, ProtectionDescriptorNameFlags dwFlags); /// /// /// The NCryptStreamClose function closes a data protection stream object opened by using the NCryptStreamOpenToProtect or /// NCryptStreamOpenToUnprotect functions. /// /// /// /// Data stream handle returned by NCryptStreamOpenToProtect or NCryptStreamOpenToUnprotect. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_INVALID_HANDLE /// The handle specified by the hStream parameter is not valid. /// /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptstreamclose SECURITY_STATUS // NCryptStreamClose( NCRYPT_STREAM_HANDLE hStream ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "770640F2-04C7-4512-8004-41F4ECDC110E")] public static extern HRESULT NCryptStreamClose(NCRYPT_STREAM_HANDLE hStream); /// /// /// The NCryptStreamOpenToProtect function opens a stream object that can be used to encrypt large amounts of data to a given /// protection descriptor. Call NCryptStreamUpdate to encrypt the content. To encrypt smaller messages such as keys and passwords, /// call NCryptProtectSecret. /// /// /// /// Handle of the protection descriptor. Create the handle by calling NCryptCreateProtectionDescriptor. /// /// /// The flag can be zero or the following value. /// /// /// Value /// Meaning /// /// /// NCRYPT_SILENT_FLAG /// Requests that the key service provider not display a user interface. /// /// /// /// /// Handle to the parent window of the user interface, if any, to be displayed. /// /// /// /// Pointer to an NCRYPT_PROTECT_STREAM_INFO structure that contains the address of a user defined callback function to receive the /// encrypted data and a pointer to user-defined context data. /// /// /// /// Pointer to the stream object handle. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_BAD_FLAGS /// The dwFlags parameter must contain zero (0), NCRYPT_MACHINE_KEY_FLAG, or NCRYPT_SILENT_FLAG. /// /// /// NTE_INVALID_HANDLE /// The handle specified by the hDescriptor parameter is not valid. /// /// /// NTE_INVALID_PARAMETER /// /// The phStream and pStreamInfo parameters cannot be NULL. The callback function pointed to by the pfnStreamOutput member of the /// NCRYPT_PROTECT_STREAM_INFO structure pointed to by the pStreamInfo parameter cannot be NULL. /// /// /// /// NTE_NO_MEMORY /// There was insufficient memory to allocate a data stream. /// /// /// /// /// /// The NCryptStreamOpenToProtect function creates an internal stream object that can be used to encrypt large messages. You /// cannot use the object directly. Instead, you must use the object handle returned by this function. /// /// /// Call this function before calling the NCryptStreamUpdate function. If you are encrypting a large file, use /// NCryptStreamUpdate in a loop that advances through the file block by block, encrypting each block as it advances and /// notifying your callback when each block is finished. For more information, see NCryptStreamUpdate. /// /// /// The NCryptStreamOpenToProtect function writes the unencrypted protection descriptor rule string to the stream object /// header so that NCryptStreamOpenToUnprotect will be able to start the decrypting the stream by using the same protector used /// during encryption. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptstreamopentoprotect SECURITY_STATUS // NCryptStreamOpenToProtect( NCRYPT_DESCRIPTOR_HANDLE hDescriptor, DWORD dwFlags, HWND hWnd, NCRYPT_PROTECT_STREAM_INFO // *pStreamInfo, NCRYPT_STREAM_HANDLE *phStream ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "7DE74BB1-1B84-4721-BE4A-4D2661E93E00")] public static extern HRESULT NCryptStreamOpenToProtect(NCRYPT_DESCRIPTOR_HANDLE hDescriptor, ProtectFlags dwFlags, HWND hWnd, in NCRYPT_PROTECT_STREAM_INFO pStreamInfo, out SafeNCRYPT_STREAM_HANDLE phStream); /// /// /// The NCryptStreamOpenToUnprotect function opens a stream object that can be used to decrypt large amounts of data to the /// same protection descriptor used for encryption. Call NCryptStreamUpdate to perform the decryption. To decrypt smaller messages /// such as keys and passwords, call NCryptUnprotectSecret. /// /// /// /// /// Pointer to an NCRYPT_PROTECT_STREAM_INFO structure that contains the address of a user defined callback function to receive the /// decrypted data and a pointer to user-defined context data. /// /// /// /// A flag that specifies additional information for the key service provider. This can be zero or the following value. /// /// /// Value /// Meaning /// /// /// NCRYPT_SILENT_FLAG /// Requests that the key service provider not display a user interface. /// /// /// /// /// Handle to the parent window of the user interface, if any, to be displayed. /// /// /// Pointer to the handle of the decrypted stream of data. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_BAD_FLAGS /// The dwFlags parameter must contain zero (0) or NCRYPT_SILENT_FLAG. /// /// /// NTE_INVALID_PARAMETER /// /// The phStream and pStreamInfo parameters cannot be NULL. The callback function pointed to by the pfnStreamOutput member of the /// NCRYPT_PROTECT_STREAM_INFO structure pointed to by the pStreamInfo parameter cannot be NULL. /// /// /// /// NTE_NO_MEMORY /// There was insufficient memory to allocate a data stream. /// /// /// /// /// /// The NCryptStreamOpenToUnprotect function creates an internal stream object that can be used to encrypt large messages. You /// cannot use the object directly. Instead, you must use the object handle returned by this function. /// /// /// Call this function before calling the NCryptStreamUpdate function. If you are encrypting a large file, use /// NCryptStreamUpdate in a loop that advances through the file block by block, encrypting each block as it advances and /// notifying your callback when each block is finished. For more information, see NCryptStreamUpdate. /// /// /// The NCryptStreamOpenToUnprotect function retrieves the unencrypted protection descriptor rule string from the stream /// header. The rule string is placed in the header by the NCryptStreamOpenToUnprotect function. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptstreamopentounprotect SECURITY_STATUS // NCryptStreamOpenToUnprotect( NCRYPT_PROTECT_STREAM_INFO *pStreamInfo, DWORD dwFlags, HWND hWnd, NCRYPT_STREAM_HANDLE *phStream ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "9848082E-EDDA-4DA1-9896-42EAF2ADFAB4")] public static extern HRESULT NCryptStreamOpenToUnprotect(in NCRYPT_PROTECT_STREAM_INFO pStreamInfo, ProtectFlags dwFlags, HWND hWnd, out SafeNCRYPT_STREAM_HANDLE phStream); /// /// /// [Some information relates to pre-released product which may be substantially modified before it's commercially released. /// Microsoft makes no warranties, express or implied, with respect to the information provided here.] /// /// /// Opens a stream object that can be used to decrypt large amounts of data to the same protection descriptor used for /// encryption.Call NCryptStreamUpdate to perform the decryption. To decrypt smaller messages such as keys and passwords, call NCryptUnprotectSecret. /// /// /// /// A pointer to NCRYPT_PROTECT_STREAM_INFO_EX. /// /// /// Only the NCRYPT_SILENT_FLAG is supported. /// /// /// A window handle to be used as the parent of any user interface that is displayed. /// /// /// Receives a pointer to a stream handle. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited to: /// /// /// /// ERROR_SUCCESS /// /// /// NTE_INVALID_PARAMETER /// /// /// NTE_BAD_FLAGS /// /// /// NTE_BAD_DATA /// /// /// NTE_NO_MEMORY /// /// /// NTE_NOT_FOUND /// /// /// NTE_NOT_SUPPORTED /// /// /// NTE_INVALID_HANDLE /// /// /// NTE_BAD_KEY /// /// /// NTE_BAD_PROVIDER /// /// /// NTE_BAD_TYPE /// /// /// NTE_DECRYPTION_FAILURE /// /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptstreamopentounprotectex SECURITY_STATUS // NCryptStreamOpenToUnprotectEx( NCRYPT_PROTECT_STREAM_INFO_EX *pStreamInfo, DWORD dwFlags, HWND hWnd, NCRYPT_STREAM_HANDLE // *phStream ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "8E607F4F-4A0F-4796-8F40-D232687815AF")] public static extern HRESULT NCryptStreamOpenToUnprotectEx(in NCRYPT_PROTECT_STREAM_INFO_EX pStreamInfo, ProtectFlags dwFlags, HWND hWnd, out SafeNCRYPT_STREAM_HANDLE phStream); /// /// The NCryptStreamUpdate function encrypts and decrypts blocks of data. /// /// /// Handle to the stream object created by calling NCryptStreamOpenToProtect or NCryptStreamOpenToUnprotect. /// /// /// Pointer to the byte array to be processed. /// /// /// Number of bytes in the binary array specified by the pbData parameter. /// /// /// A Boolean value that specifies whether the last block of data has been processed. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_BAD_DATA /// The content could not be decoded. /// /// /// NTE_INVALID_HANDLE /// The stream handle pointed to by the hStream parameter is not valid. /// /// /// NTE_NO_MEMORY /// There was insufficient memory available to process the content. /// /// /// /// /// You must call NCryptStreamOpenToProtect or NCryptStreamOpenToUnprotect to open a stream before calling NCryptStreamUpdate /// /// Messages can be so large that processing them all at once by storing the entire message in memory can be difficult. It is /// possible, however, to process large messages by partitioning the data to be processed into manageable blocks. /// /// /// To do this, use NCryptStreamUpdate in a loop that advances through the file block by block. As the streamed message is /// processed, the resulting output data is passed back to your application by using a callback function that you specify. This is /// shown by the following example. For more information about the callback function, see PFNCryptStreamOutputCallback. /// /// /// Note We recommend against using too small of a block size. Small blocks require more calls and therefore more calling /// overhead. Further, the streaming APIs are optimized for larger blocks. You should experiment to find the best block size for the /// data you must process. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptstreamupdate SECURITY_STATUS // NCryptStreamUpdate( NCRYPT_STREAM_HANDLE hStream, const BYTE *pbData, SIZE_T cbData, BOOL fFinal ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "417F9267-6055-489C-AF26-BEF5E17CB8B4")] public static extern HRESULT NCryptStreamUpdate(NCRYPT_STREAM_HANDLE hStream, IntPtr pbData, SizeT cbData, [MarshalAs(UnmanagedType.Bool)] bool fFinal); /// /// /// The NCryptUnprotectSecret function decrypts data to a specified protection descriptor. Call NCryptProtectSecret to encrypt /// the data. /// /// /// /// Pointer to the protection descriptor handle. /// /// /// The flag can be zero or a bitwise OR of the following values. /// /// /// Value /// Meaning /// /// /// NCRYPT_SILENT_FLAG /// Requests that the key service provider not display a user interface. /// /// /// NCRYPT_UNPROTECT_NO_DECRYPT /// Decodes only the header of the protected data blob. No actual decryption takes place. /// /// /// /// /// Pointer to an array of bytes that contains the data to decrypt. /// /// /// The number of bytes in the array pointed to by the pbProtectedBlob parameter. /// /// /// /// Pointer to an NCRYPT_ALLOC_PARA structure that you can use to specify custom memory management functions. If you set this /// argument to NULL, the LocalAlloc function is used internally to allocate memory and your application must call LocalFree /// to release memory pointed to by the ppbData parameter. /// /// /// /// Handle to the parent window of the user interface, if any, to be displayed. /// /// /// Address of a variable that receives a pointer to the decrypted data. /// /// /// Pointer to a ULONG variable that contains the size, in bytes, of the decrypted data pointed to by the ppbData variable. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_BAD_FLAGS /// The dwFlags parameter can only contain NCRYPT_SILENT_FLAG or NCRYPT_UNPROTECT_NO_DECRYPT. /// /// /// NTE_INVALID_PARAMETER /// The pbProtectedBlob, ppbData, and pcbData parameters cannot be NULL. The cbData parameter cannot be less than one. /// /// /// /// /// /// Use the NCryptUnprotectSecret function to decrypt keys, key material, and passwords. Use the NCryptStreamOpenToUnprotect /// and the NCryptStreamUpdate functions to decrypt larger messages. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptunprotectsecret SECURITY_STATUS // NCryptUnprotectSecret( NCRYPT_DESCRIPTOR_HANDLE *phDescriptor, DWORD dwFlags, const BYTE *pbProtectedBlob, ULONG cbProtectedBlob, // const NCRYPT_ALLOC_PARA *pMemPara, HWND hWnd, BYTE **ppbData, ULONG *pcbData ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "F532F0ED-36F4-47E3-B478-089CC083E5D1")] public static extern HRESULT NCryptUnprotectSecret(out SafeNCRYPT_DESCRIPTOR_HANDLE phDescriptor, UnprotectSecretFlags dwFlags, [In] IntPtr pbProtectedBlob, uint cbProtectedBlob, in NCRYPT_ALLOC_PARA pMemPara, HWND hWnd, out IntPtr ppbData, out uint pcbData); /// /// /// The NCryptUnprotectSecret function decrypts data to a specified protection descriptor. Call NCryptProtectSecret to encrypt /// the data. /// /// /// /// Pointer to the protection descriptor handle. /// /// /// The flag can be zero or a bitwise OR of the following values. /// /// /// Value /// Meaning /// /// /// NCRYPT_SILENT_FLAG /// Requests that the key service provider not display a user interface. /// /// /// NCRYPT_UNPROTECT_NO_DECRYPT /// Decodes only the header of the protected data blob. No actual decryption takes place. /// /// /// /// /// Pointer to an array of bytes that contains the data to decrypt. /// /// /// The number of bytes in the array pointed to by the pbProtectedBlob parameter. /// /// /// /// Pointer to an NCRYPT_ALLOC_PARA structure that you can use to specify custom memory management functions. If you set this /// argument to NULL, the LocalAlloc function is used internally to allocate memory and your application must call LocalFree /// to release memory pointed to by the ppbData parameter. /// /// /// /// Handle to the parent window of the user interface, if any, to be displayed. /// /// /// Address of a variable that receives a pointer to the decrypted data. /// /// /// Pointer to a ULONG variable that contains the size, in bytes, of the decrypted data pointed to by the ppbData variable. /// /// /// /// Returns a status code that indicates the success or failure of the function. Possible return codes include, but are not limited /// to, the following. /// /// /// /// Return code /// Description /// /// /// ERROR_SUCCESS /// The function was successful. /// /// /// NTE_BAD_FLAGS /// The dwFlags parameter can only contain NCRYPT_SILENT_FLAG or NCRYPT_UNPROTECT_NO_DECRYPT. /// /// /// NTE_INVALID_PARAMETER /// The pbProtectedBlob, ppbData, and pcbData parameters cannot be NULL. The cbData parameter cannot be less than one. /// /// /// /// /// /// Use the NCryptUnprotectSecret function to decrypt keys, key material, and passwords. Use the NCryptStreamOpenToUnprotect /// and the NCryptStreamUpdate functions to decrypt larger messages. /// /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/nf-ncryptprotect-ncryptunprotectsecret SECURITY_STATUS // NCryptUnprotectSecret( NCRYPT_DESCRIPTOR_HANDLE *phDescriptor, DWORD dwFlags, const BYTE *pbProtectedBlob, ULONG cbProtectedBlob, // const NCRYPT_ALLOC_PARA *pMemPara, HWND hWnd, BYTE **ppbData, ULONG *pcbData ); [DllImport(Lib.Ncrypt, SetLastError = false, ExactSpelling = true)] [PInvokeData("ncryptprotect.h", MSDNShortId = "F532F0ED-36F4-47E3-B478-089CC083E5D1")] public static extern HRESULT NCryptUnprotectSecret(out SafeNCRYPT_DESCRIPTOR_HANDLE phDescriptor, UnprotectSecretFlags dwFlags, [In] IntPtr pbProtectedBlob, uint cbProtectedBlob, [Optional] IntPtr pMemPara, HWND hWnd, out IntPtr ppbData, out uint pcbData); /// Provides a handle to a protection descriptor. [StructLayout(LayoutKind.Sequential)] public struct NCRYPT_DESCRIPTOR_HANDLE : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public NCRYPT_DESCRIPTOR_HANDLE(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static NCRYPT_DESCRIPTOR_HANDLE NULL => new NCRYPT_DESCRIPTOR_HANDLE(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(NCRYPT_DESCRIPTOR_HANDLE h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator NCRYPT_DESCRIPTOR_HANDLE(IntPtr h) => new NCRYPT_DESCRIPTOR_HANDLE(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(NCRYPT_DESCRIPTOR_HANDLE h1, NCRYPT_DESCRIPTOR_HANDLE h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(NCRYPT_DESCRIPTOR_HANDLE h1, NCRYPT_DESCRIPTOR_HANDLE h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is NCRYPT_DESCRIPTOR_HANDLE h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// /// The NCRYPT_PROTECT_STREAM_INFO structure is used by the NCryptStreamOpenToProtect and NCryptStreamOpenToUnprotect /// functions to pass blocks of processed data to your application. /// // https://docs.microsoft.com/en-us/windows/desktop/api/ncryptprotect/ns-ncryptprotect-ncrypt_protect_stream_info typedef struct // NCRYPT_PROTECT_STREAM_INFO { PFNCryptStreamOutputCallback pfnStreamOutput; void *pvCallbackCtxt; } NCRYPT_PROTECT_STREAM_INFO; [PInvokeData("ncryptprotect.h", MSDNShortId = "77FADFC1-6C66-4801-B0BD-263963555C3C")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct NCRYPT_PROTECT_STREAM_INFO { /// /// Address of a callback function that accepts data from the stream encryption or decryption process. for more information, see PFNCryptStreamOutputCallback. /// public PFNCryptStreamOutputCallback pfnStreamOutput; /// /// Pointer to a buffer supplied the caller. The buffer is not modified by the data protection API. You can use the buffer to /// keep track of your application. /// public IntPtr pvCallbackCtxt; } /// /// The NCRYPT_PROTECT_STREAM_INFO_EX structure is used by the NCryptStreamOpenToProtectEx and NCryptStreamOpenToUnprotectEx /// functions to pass blocks of processed data to your application. /// [PInvokeData("ncryptprotect.h")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct NCRYPT_PROTECT_STREAM_INFO_EX { /// /// Address of a callback function that accepts data from the stream encryption or decryption process. for more information, see PFNCryptStreamOutputCallback. /// public PFNCryptStreamOutputCallbackEx pfnStreamOutput; /// /// Pointer to a buffer supplied the caller. The buffer is not modified by the data protection API. You can use the buffer to /// keep track of your application. /// public IntPtr pvCallbackCtxt; } /// Provides a handle to a data protection stream object. [StructLayout(LayoutKind.Sequential)] public struct NCRYPT_STREAM_HANDLE : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public NCRYPT_STREAM_HANDLE(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static NCRYPT_STREAM_HANDLE NULL => new NCRYPT_STREAM_HANDLE(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(NCRYPT_STREAM_HANDLE h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator NCRYPT_STREAM_HANDLE(IntPtr h) => new NCRYPT_STREAM_HANDLE(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(NCRYPT_STREAM_HANDLE h1, NCRYPT_STREAM_HANDLE h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(NCRYPT_STREAM_HANDLE h1, NCRYPT_STREAM_HANDLE h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is NCRYPT_STREAM_HANDLE h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a for that is disposed using . public class SafeNCRYPT_DESCRIPTOR_HANDLE : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeNCRYPT_DESCRIPTOR_HANDLE(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeNCRYPT_DESCRIPTOR_HANDLE() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator NCRYPT_DESCRIPTOR_HANDLE(SafeNCRYPT_DESCRIPTOR_HANDLE h) => h.handle; /// protected override bool InternalReleaseHandle() => NCryptCloseProtectionDescriptor(this).Succeeded; } /// Provides a for that is disposed using . public class SafeNCRYPT_STREAM_HANDLE : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeNCRYPT_STREAM_HANDLE(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeNCRYPT_STREAM_HANDLE() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator NCRYPT_STREAM_HANDLE(SafeNCRYPT_STREAM_HANDLE h) => h.handle; /// protected override bool InternalReleaseHandle() => NCryptStreamClose(this).Succeeded; } } }