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, WaitHandle cancelCurrentOperation = null) { SafeIpcPromptContext ipcContext = SafeNativeMethods.CreateIpcPromptContext( suppressUI, offline, hasUserConsent, parentWindow, symmKey, cancelCurrentOperation); return(IpcfEncryptFileStream( inputStream, inputFilePath, licenseHandle, flags, ref outputStream, ipcContext)); }
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)); }
public static string IpcfEncryptFileStream( Stream inputStream, string inputFilePath, string templateId, EncryptFlags flags, bool suppressUI, bool offline, bool hasUserConsent, Form parentWindow, SymmetricKeyCredential symmKey, ref Stream outputStream, WaitHandle cancelCurrentOperation = null) { SafeIpcPromptContext ipcContext = SafeNativeMethods.CreateIpcPromptContext( suppressUI, offline, hasUserConsent, IpcWindow.Create(parentWindow).Handle, symmKey, cancelCurrentOperation); return(IpcfEncryptFileStream( inputStream, inputFilePath, templateId, flags, ref outputStream, ipcContext)); }
public SafeIpcPromptContextWrapper(SafeIpcPromptContext context) { if (context != null) { this.context = GCHandle.Alloc(context); } }
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 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, 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); }
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 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 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 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); }
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); } }
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 SafeIpcPromptContextWrapper Wrap(SafeIpcPromptContext context) { return(new SafeIpcPromptContextWrapper(context)); }
// Private Helpers public static SafeIpcPromptContext CreateIpcPromptContext(bool suppressUI, bool offline, bool hasUserConsent, System.Windows.Forms.Form parentForm, SymmetricKeyCredential symmKey = null) { SafeIpcPromptContext ipcPromptContext = new SafeIpcPromptContext( ((null != parentForm) ? parentForm.Handle : IntPtr.Zero), ((null != symmKey) ? new IpcCredential(IpcCredentialType.SymmetricKey, symmKey) : null), //We don't support the cancel event IntPtr.Zero); IpcPromptContext context = (IpcPromptContext)ipcPromptContext; context.flags = 0; if (suppressUI) { context.flags |= (uint)PromptContextFlag.Slient; } if (offline) { context.flags |= (uint)PromptContextFlag.Offline; } if (hasUserConsent) { context.flags |= (uint)PromptContextFlag.HasUserConsent; } return ipcPromptContext; }
public static void ReleaseIpcPromptContext(SafeIpcPromptContext ctx) { if (null != ctx) { ctx.Dispose(); } }