public static byte[] IpcfGetSerializedLicenseFromFileStream(
            Stream inputStream,
            string inputFilePath)
        {
            byte[]     license       = null;
            int        hr            = 0;
            ILockBytes ilInputStream = new ILockBytesOverStream(inputStream);

            IntPtr licensePtr = IntPtr.Zero;

            try
            {
                hr = UnsafeFileApiMethods.IpcfGetSerializedLicenseFromFileStream(
                    ilInputStream,
                    inputFilePath,
                    out licensePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                license = SafeNativeMethods.MarshalIpcBufferToManaged(licensePtr);
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(licensePtr);
            }
            return(license);
        }
示例#2
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);
        }
        public static string IpcfEncryptFileStream(
            Stream inputStream,
            string inputFilePath,
            string templateId,
            EncryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            SymmetricKeyCredential symmKey,
            ref Stream outputStream)
        {
            int        hr = 0;
            IntPtr     encryptedFileName = IntPtr.Zero;
            string     outputFileName    = null;
            ILockBytes ilInputStream     = new ILockBytesOverStream(inputStream);
            ILockBytes ilOutputStream    = new ILockBytesOverStream(outputStream);


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

            IntPtr licenseInfoPtr = Marshal.StringToHGlobalUni(templateId);

            try
            {
                hr = UnsafeFileApiMethods.IpcfEncryptFileStream(
                    ilInputStream,
                    inputFilePath,
                    licenseInfoPtr,
                    (uint)EncryptLicenseInfoTypes.IPCF_EF_TEMPLATE_ID,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    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,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            IntPtr parentWindow,
            SymmetricKeyCredential symmKey,
            ref Stream outputStream)
        {
            int        hr = 0;
            IntPtr     encryptedFileName = IntPtr.Zero;
            string     outputFileName    = null;
            ILockBytes ilInputStream     = new ILockBytesOverStream(inputStream);
            ILockBytes ilOutputStream    = new ILockBytesOverStream(outputStream);

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

            try
            {
                hr = UnsafeFileApiMethods.IpcfEncryptFileStream(
                    ilInputStream,
                    inputFilePath,
                    licenseHandle.Value,
                    (uint)EncryptLicenseInfoTypes.IPCF_EF_LICENSE_HANDLE,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    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 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);
        }
示例#6
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);
        }
        public static string IpcfEncryptFile(
            string inputFile,
            SafeInformationProtectionLicenseHandle licenseHandle,
            EncryptFlags flags,
            bool suppressUI,
            bool offline,
            bool hasUserConsent,
            System.Windows.Forms.Form parentForm,
            SymmetricKeyCredential symmKey,
            string outputDirectory = null)
        {
            int    hr = 0;
            IntPtr encryptedFileName = IntPtr.Zero;
            string outputFileName    = null;

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

            try
            {
                hr = UnsafeFileApiMethods.IpcfEncryptFile(
                    inputFile,
                    licenseHandle.Value,
                    (uint)EncryptLicenseInfoTypes.IPCF_EF_LICENSE_HANDLE,
                    (uint)flags,
                    (IpcPromptContext)ipcContext,
                    outputDirectory,
                    out encryptedFileName);

                SafeNativeMethods.ThrowOnErrorCode(hr);

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

            return(outputFileName);
        }
        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);
        }
示例#9
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);
        }
        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);
        }
示例#11
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);
        }
        public static byte[] IpcfGetSerializedLicenseFromFile(string inputFile)
        {
            byte[] license = null;
            int    hr      = 0;

            IntPtr licensePtr = IntPtr.Zero;

            try
            {
                hr = UnsafeFileApiMethods.IpcfGetSerializedLicenseFromFile(
                    inputFile,
                    out licensePtr);

                SafeNativeMethods.ThrowOnErrorCode(hr);

                license = SafeNativeMethods.MarshalIpcBufferToManaged(licensePtr);
            }
            finally
            {
                UnsafeFileApiMethods.IpcFreeMemory(licensePtr);
            }
            return(license);
        }