internal static void ImportEncryptedFileRawCore(System.IO.Stream inputStream, string destinationFilePath, PathFormat pathFormat, NativeMethods.EncryptedFileRawMode mode)
        {
            string lpPath = Path.GetExtendedLengthPathCore(null, destinationFilePath, pathFormat, GetFullPathOptions.FullCheck | GetFullPathOptions.TrimEnd);
            SafeEncryptedFileRawHandle context = null;
            int errorCode = NativeMethods.OpenEncryptedFileRaw(lpPath, mode, out context);

            try
            {
                if (errorCode != Win32Errors.ERROR_SUCCESS)
                {
                    NativeError.ThrowException(errorCode, null, destinationFilePath);
                }

                errorCode = NativeMethods.WriteEncryptedFileRaw((IntPtr pbData, IntPtr pvCallbackContext, ref uint ulLength) =>
                {
                    try
                    {
                        byte[] buffer = new byte[ulLength];
                        ulLength      = (uint)inputStream.Read(buffer, 0, (int)ulLength);
                        if (ulLength == 0)
                        {
                            return((int)Win32Errors.ERROR_SUCCESS);
                        }

                        Marshal.Copy(buffer, 0, pbData, (int)ulLength);
                    }
                    catch (Exception ex)
                    {
                        return(Marshal.GetHRForException(ex));
                    }

                    return((int)Win32Errors.ERROR_SUCCESS);
                }, IntPtr.Zero, context);

                if (errorCode != Win32Errors.ERROR_SUCCESS)
                {
                    NativeError.ThrowException(errorCode, null, destinationFilePath);
                }
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
        /// <summary>
        ///   Backs up (export) encrypted files. This is one of a group of Encrypted File System (EFS) functions that is
        ///   intended to implement backup and restore functionality, while maintaining files in their encrypted state.
        /// </summary>
        /// <remarks>
        ///   <para>
        ///      The file being backed up is not decrypted; it is backed up in its encrypted state.
        ///   </para>
        ///   <para>
        ///      If the caller does not have access to the key for the file, the caller needs
        ///      <see cref="Alphaleonis.Win32.Security.Privilege.Backup"/> to export encrypted files. See
        ///      <see cref="Alphaleonis.Win32.Security.PrivilegeEnabler"/>.
        ///   </para>
        ///   <para>
        ///      To backup an encrypted file call one of the
        ///      <see cref="O:Alphaleonis.Win32.Filesystem.File.ExportEncryptedFileRaw"/> overloads and specify the file to backup
        ///      along with the destination stream of the backup data.
        ///   </para>
        ///   <para>
        ///      This function is intended for the backup of only encrypted files; see <see cref="BackupFileStream"/> for backup
        ///      of unencrypted files.
        ///   </para>
        /// </remarks>
        /// <param name="fileName">The name of the file to be backed up.</param>
        /// <param name="output">The destination stream to which the backup data will be written.</param>
        /// <param name="pathFormat">The path format of the <paramref name="fileName"/> parameter.</param>
        /// <seealso cref="O:Alphaleonis.Win32.Filesystem.File.ImportEncryptedFileRaw"/>
        public static void ExportEncryptedFileRaw(string fileName, System.IO.Stream output, PathFormat pathFormat)
        {
            string lpPath = Path.GetExtendedLengthPathCore(null, fileName, pathFormat, GetFullPathOptions.FullCheck | GetFullPathOptions.TrimEnd);
            SafeEncryptedFileRawHandle context = null;
            int errorCode = NativeMethods.OpenEncryptedFileRaw(lpPath, NativeMethods.EncryptedFileRawMode.CreateForExport, out context);

            try
            {
                if (errorCode != Win32Errors.ERROR_SUCCESS)
                {
                    NativeError.ThrowException(errorCode, fileName);
                }

                errorCode = NativeMethods.ReadEncryptedFileRaw((IntPtr pbData, IntPtr pvCallbackContext, uint ulLength) =>
                {
                    try
                    {
                        byte[] data = new byte[ulLength];
                        Marshal.Copy(pbData, data, 0, (int)ulLength);
                        output.Write(data, 0, (int)ulLength);
                    }
                    catch (Exception ex)
                    {
                        return(Marshal.GetHRForException(ex));
                    }

                    return((int)Win32Errors.ERROR_SUCCESS);
                }, IntPtr.Zero, context);

                if (errorCode != Win32Errors.ERROR_SUCCESS)
                {
                    NativeError.ThrowException(errorCode, fileName);
                }
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
 internal static extern int WriteEncryptedFileRaw([MarshalAs(UnmanagedType.FunctionPtr)] EncryptedFileRawImportCallback pfExportCallback, IntPtr pvCallbackContext, SafeEncryptedFileRawHandle pvContext);
 internal static extern int OpenEncryptedFileRaw([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, EncryptedFileRawMode ulFlags, out SafeEncryptedFileRawHandle pvContext);
예제 #5
0
 public static extern int OpenEncryptedFileRaw(string lpFileName, EncryptedFileRawMode ulFlags, out SafeEncryptedFileRawHandle pvContext);