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);
        }
        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);
        }
        public static bool IpcfIsFileStreamEncrypted(Stream inputStream, string inputFilePath)
        {
            uint       fileStatus;
            ILockBytes ilInputStream = new ILockBytesOverStream(inputStream);
            int        hr            = UnsafeFileApiMethods.IpcfIsFileStreamEncrypted(ilInputStream, inputFilePath, out fileStatus);

            SafeNativeMethods.ThrowOnErrorCode(hr);

            return((FileEncryptedStatus)fileStatus != FileEncryptedStatus.IPCF_FILE_STATUS_DECRYPTED);
        }
Exemplo n.º 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);
        }
Exemplo n.º 5
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 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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
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 bool IpcfIsFileStreamEncrypted(Stream inputStream, string inputFilePath)
        {
            uint fileStatus;
            ILockBytes ilInputStream = new ILockBytesOverStream(inputStream);
            int hr = UnsafeFileApiMethods.IpcfIsFileStreamEncrypted(ilInputStream, inputFilePath, out fileStatus);
            SafeNativeMethods.ThrowOnErrorCode(hr);

            return (FileEncryptedStatus)fileStatus != FileEncryptedStatus.IPCF_FILE_STATUS_DECRYPTED;
        }
        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;
        }
        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 IpcfEncryptFileStream(
            Stream inputStream,
            string inputFilePath,
            SafeInformationProtectionLicenseHandle licenseHandle,
            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);

            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;
        }