Exemple #1
0
        public static string IpcfDecryptFileStream(
            Stream inputStream,
            string inputFilePath,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            IntPtr parentWindow,
            ref Stream outputStream,
            WaitHandle cancelCurrentOperation = null)
        {
            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                                                         offline,
                                                         hasUserConsent,
                                                         parentWindow,
                                                         cancelCurrentOperation);

            return(IpcfDecryptFileStream(
                       inputStream,
                       inputFilePath,
                       flags,
                       ref outputStream,
                       ipcContext));
        }
Exemple #2
0
        public static string IpcfDecryptFile(
            string inputFile,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            IntPtr parentWindow,
            SymmetricKeyCredential symmKey,
            string outputDirectory            = null,
            WaitHandle cancelCurrentOperation = null)
        {
            int    hr = 0;
            IntPtr decryptedFileNamePtr = IntPtr.Zero;
            string decryptedFileName    = null;

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                                                         offline,
                                                         hasUserConsent,
                                                         parentWindow,
                                                         symmKey,
                                                         cancelCurrentOperation);

            try
            {
                using (var wrappedContext = ipcContext.Wrap())
                {
                    hr = UnsafeFileApiMethods.IpcfDecryptFile(
                        inputFile,
                        (uint)flags,
                        (IpcPromptContext)wrappedContext,
                        outputDirectory,
                        out decryptedFileNamePtr);
                }
                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFile;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
                SafeNativeMethods.ReleaseIpcPromptContext(ipcContext);
            }

            return(decryptedFileName);
        }
        public static string IpcfDecryptFileStream(
            Stream inputStream,
            string inputFilePath,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            ref Stream outputStream)
        {
            int        hr = 0;
            IntPtr     decryptedFileNamePtr = IntPtr.Zero;
            string     decryptedFileName    = null;
            ILockBytes ilInputStream        = new ILockBytesOverStream(inputStream);
            ILockBytes ilOutputStream       = new ILockBytesOverStream(outputStream);

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                                                         offline,
                                                         hasUserConsent,
                                                         parentForm);

            try
            {
                hr = UnsafeFileApiMethods.IpcfDecryptFileStream(
                    ilInputStream,
                    inputFilePath,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    ilOutputStream,
                    out decryptedFileNamePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFilePath;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
            }

            return(decryptedFileName);
        }
        public static string IpcfDecryptFile(
            string inputFile,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            SymmetricKeyCredential symmKey,
            string outputDirectory = null)
        {
            int    hr = 0;
            IntPtr decryptedFileNamePtr = IntPtr.Zero;
            string decryptedFileName    = null;

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                                                         offline,
                                                         hasUserConsent,
                                                         parentForm,
                                                         symmKey);

            try
            {
                hr = UnsafeFileApiMethods.IpcfDecryptFile(
                    inputFile,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    outputDirectory,
                    out decryptedFileNamePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFile;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
                SafeNativeMethods.ReleaseIpcPromptContext(ipcContext);
            }

            return(decryptedFileName);
        }
Exemple #5
0
        public static string IpcfDecryptFileStream(
            Stream inputStream,
            string inputFilePath,
            DecryptFlags flags,
            ref Stream outputStream,
            SafeIpcPromptContext ipcContext = null)
        {
            int        hr = 0;
            IntPtr     decryptedFileNamePtr = IntPtr.Zero;
            string     decryptedFileName    = null;
            ILockBytes ilInputStream        = new ILockBytesOverStream(inputStream);
            ILockBytes ilOutputStream       = new ILockBytesOverStream(outputStream);

            if (null == ipcContext) //use the default
            {
                ipcContext = SafeNativeMethods.CreateIpcPromptContext(false, false, false, IntPtr.Zero);
            }
            try
            {
                using (var wrappedContext = ipcContext.Wrap())
                {
                    hr = UnsafeFileApiMethods.IpcfDecryptFileStream(
                        ilInputStream,
                        inputFilePath,
                        (uint)flags,
                        (IpcPromptContext)wrappedContext,
                        ilOutputStream,
                        out decryptedFileNamePtr);
                }
                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFilePath;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
            }

            return(decryptedFileName);
        }
Exemple #6
0
 public static string IpcfDecryptFileStream(
     Stream inputStream,
     string inputFilePath,
     DecryptFlags flags,
     bool suppressUI,
     bool offline,
     bool hasUserConsent,
     Form parentWindow,
     ref Stream outputStream)
 {
     return(IpcfDecryptFileStream(
                inputStream,
                inputFilePath,
                flags,
                suppressUI,
                offline,
                hasUserConsent,
                IpcWindow.Create(parentWindow).Handle,
                ref outputStream));
 }
Exemple #7
0
 public static string IpcfDecryptFile(
     string inputFile,
     DecryptFlags flags,
     bool suppressUI,
     bool offline,
     bool hasUserConsent,
     Form parentWindow,
     SymmetricKeyCredential symmKey,
     string outputDirectory = null)
 {
     return(IpcfDecryptFile(
                inputFile,
                flags,
                suppressUI,
                offline,
                hasUserConsent,
                IpcWindow.Create(parentWindow).Handle,
                symmKey,
                outputDirectory));
 }
        public static string IpcfDecryptFileStream(
            Stream inputStream,
            string inputFilePath,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            ref Stream outputStream)
        {
            int hr = 0;
            IntPtr decryptedFileNamePtr = IntPtr.Zero;
            string decryptedFileName = null;
            ILockBytes ilInputStream = new ILockBytesOverStream(inputStream);
            ILockBytes ilOutputStream = new ILockBytesOverStream(outputStream);

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                    offline,
                    hasUserConsent,
                    parentForm);

            try
            {
                hr = UnsafeFileApiMethods.IpcfDecryptFileStream(
                    ilInputStream,
                    inputFilePath,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    ilOutputStream,
                    out decryptedFileNamePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFilePath;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
            }

            return decryptedFileName;
        }
        public static string IpcfDecryptFile(
            string inputFile,
            DecryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            SymmetricKeyCredential symmKey,
            string outputDirectory = null)
        {
            int hr = 0;
            IntPtr decryptedFileNamePtr = IntPtr.Zero;
            string decryptedFileName = null;

            SafeIpcPromptContext ipcContext =
                SafeNativeMethods.CreateIpcPromptContext(suppressUI,
                    offline,
                    hasUserConsent,
                    parentForm,
                    symmKey);

            try
            {
                hr = UnsafeFileApiMethods.IpcfDecryptFile(
                    inputFile,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    outputDirectory,
                    out decryptedFileNamePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                decryptedFileName = Marshal.PtrToStringUni(decryptedFileNamePtr);
                if (null == decryptedFileName || 0 == decryptedFileName.Length)
                {
                    decryptedFileName = inputFile;
                }
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(decryptedFileNamePtr);
                SafeNativeMethods.ReleaseIpcPromptContext(ipcContext);
            }

            return decryptedFileName;
        }