コード例 #1
0
        public static string IpcfEncryptFile(
            string inputFile,
            string templateId,
            EncryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            IntPtr parentWindow,
            SymmetricKeyCredential symmKey    = null,
            string outputDirectory            = null,
            WaitHandle cancelCurrentOperation = null)
        {
            int    hr = 0;
            IntPtr encryptedFileName = IntPtr.Zero;
            string outputFileName    = null;

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

            IntPtr licenseInfoPtr = Marshal.StringToHGlobalUni(templateId);

            try
            {
                using (var wrappedContext = ipcContext.Wrap())
                {
                    hr = UnsafeFileApiMethods.IpcfEncryptFile(
                        inputFile,
                        licenseInfoPtr,
                        (uint)EncryptLicenseInfoTypes.IPCF_EF_TEMPLATE_ID,
                        (uint)flags,
                        (IpcPromptContext)wrappedContext,
                        outputDirectory,
                        out encryptedFileName);
                }
                SafeNativeMethods.ThrowOnErrorCode(hr);

                outputFileName = Marshal.PtrToStringUni(encryptedFileName);
                if (null == outputFileName || 0 == outputFileName.Length)
                {
                    outputFileName = inputFile;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(licenseInfoPtr);
                UnsafeFileApiMethods.IpcFreeMemory(encryptedFileName);
                SafeNativeMethods.ReleaseIpcPromptContext(ipcContext);
            }

            return(outputFileName);
        }
コード例 #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);
        }
コード例 #3
0
        public static string IpcfEncryptFileStream(
            Stream inputStream,
            string inputFilePath,
            string templateId,
            EncryptFlags flags,
            ref Stream outputStream,
            SafeIpcPromptContext ipcContext = null)
        {
            int        hr = 0;
            IntPtr     encryptedFileName = IntPtr.Zero;
            string     outputFileName    = null;
            ILockBytes ilInputStream     = new ILockBytesOverStream(inputStream);
            ILockBytes ilOutputStream    = new ILockBytesOverStream(outputStream);

            IntPtr licenseInfoPtr = Marshal.StringToHGlobalUni(templateId);

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

                outputFileName = Marshal.PtrToStringUni(encryptedFileName);
                if (null == outputFileName || 0 == outputFileName.Length)
                {
                    outputFileName = inputFilePath;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(licenseInfoPtr);
                UnsafeFileApiMethods.IpcFreeMemory(encryptedFileName);
            }

            return(outputFileName);
        }
コード例 #4
0
        public static string IpcfEncryptFileStream(
            Stream inputStream,
            string inputFilePath,
            SafeInformationProtectionLicenseHandle licenseHandle,
            EncryptFlags flags,
            ref Stream outputStream,
            SafeIpcPromptContext ipcContext = null)
        {
            int        hr = 0;
            IntPtr     encryptedFileName = IntPtr.Zero;
            string     outputFileName    = 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.IpcfEncryptFileStream(
                        ilInputStream,
                        inputFilePath,
                        licenseHandle.Value,
                        (uint)EncryptLicenseInfoTypes.IPCF_EF_LICENSE_HANDLE,
                        (uint)flags,
                        (IpcPromptContext)wrappedContext,
                        ilOutputStream,
                        out encryptedFileName);
                }
                SafeNativeMethods.ThrowOnErrorCode(hr);

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

            return(outputFileName);
        }
コード例 #5
0
        public static SafeInformationProtectionFileHandle IpcfOpenFileOnStream(
            Stream inputStream,
            EncryptFlags flags,
            SafeIpcPromptContext ipcContext)
        {
            int        hr            = 0;
            ILockBytes ilInputStream = new ILockBytesOverStream(inputStream);
            SafeInformationProtectionFileHandle fileHandle;

            using (var wrappedContext = ipcContext.Wrap())
            {
                hr = UnsafeFileApiMethods.IpcfOpenFileOnILockBytes(ilInputStream, (IpcPromptContext)wrappedContext,
                                                                   (uint)flags, out fileHandle);
                SafeNativeMethods.ThrowOnErrorCode(hr);
                return(fileHandle);
            }
        }
コード例 #6
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);
        }