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 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 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); }
/// <summary> /// Attempts to encrypt the contents of a data buffer for multiple recipients. /// </summary> /// <param name="plain">A DataBuffer containing the plaintext data to be encrypted.</param> /// <param name="recipients">The GPG keys for which the data should be encrypted.</param> /// <param name="encryptFlags">The encryption flags to be used.</param> /// <returns>A DataBuffer containing the encrypted data.</returns> public GpgBuffer Encrypt(GpgBuffer plain, IEnumerable <GpgKey> recipients, EncryptFlags encryptFlags = EncryptFlags.None) { // Transform the recipient list into a list of GpgME key handles var rcpHandles = recipients.Select(rcp => rcp.Handle).ToArray(); var output = MemoryGpgBuffer.Create(); ErrorHandler.Check(GpgMeWrapper.gpgme_op_encrypt(Handle, rcpHandles, encryptFlags, plain.Handle, output.Handle)); output.Position = 0; return(output); }
public static string IpcfEncryptFile( string inputFile, string templateId, EncryptFlags flags, bool suppressUI, bool offline, bool hasUserConsent, System.Windows.Forms.Form parentForm, SymmetricKeyCredential symmKey = null, string outputDirectory = null) { int hr = 0; IntPtr encryptedFileName = IntPtr.Zero; string outputFileName = null; SafeIpcPromptContext ipcContext = SafeNativeMethods.CreateIpcPromptContext(suppressUI, offline, hasUserConsent, parentForm, symmKey); IntPtr licenseInfoPtr = Marshal.StringToHGlobalUni(templateId); try { hr = UnsafeFileApiMethods.IpcfEncryptFile( inputFile, licenseInfoPtr, (uint)EncryptLicenseInfoTypes.IPCF_EF_TEMPLATE_ID, (uint)flags, (IpcPromptContext)ipcContext, 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 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 IpcfEncryptFile( string inputFile, string templateId, EncryptFlags flags, bool suppressUI, bool offline, bool hasUserConsent, System.Windows.Forms.Form parentForm, string outputDirectory = null) { int hr = 0; IntPtr encryptedFileName = IntPtr.Zero; string outputFileName = null; IpcPromptContext ipcContext = SafeNativeMethods.CreateIpcPromptContext(suppressUI, offline, hasUserConsent, parentForm); IntPtr licenseInfoPtr = Marshal.StringToHGlobalUni(templateId); try { hr = UnsafeFileApiMethods.IpcfEncryptFile( inputFile, licenseInfoPtr, (uint)EncryptLicenseInfoTypes.IPCF_EF_TEMPLATE_ID, (uint)flags, ipcContext, 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); } 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 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 IpcfEncryptFile( string inputFile, string templateId, EncryptFlags flags, bool suppressUI, bool offline, bool hasUserConsent, Form parentWindow, SymmetricKeyCredential symmKey = null, string outputDirectory = null) { return(IpcfEncryptFile( inputFile, templateId, flags, suppressUI, offline, hasUserConsent, IpcWindow.Create(parentWindow).Handle, symmKey, outputDirectory)); }
public static string IpcfEncryptFile( string inputFile, SafeInformationProtectionLicenseHandle licenseHandle, EncryptFlags flags, bool suppressUI, bool offline, bool hasUserConsent, Form parentWindow, SymmetricKeyCredential symmKey, string outputDirectory = null) { return(IpcfEncryptFile( inputFile, licenseHandle, flags, suppressUI, offline, hasUserConsent, IpcWindow.Create(parentWindow).Handle, symmKey, outputDirectory)); }
public static string IpcfEncryptFileStream( Stream inputStream, string inputFilePath, SafeInformationProtectionLicenseHandle licenseHandle, EncryptFlags flags, bool suppressUI, bool offline, bool hasUserConsent, Form parentWindow, SymmetricKeyCredential symmKey, ref Stream outputStream) { return(IpcfEncryptFileStream( inputStream, inputFilePath, licenseHandle, flags, suppressUI, offline, hasUserConsent, IpcWindow.Create(parentWindow).Handle, symmKey, ref outputStream)); }
public EncryptionResult EncryptAndSign(Key[] recipients, EncryptFlags flags, GpgmeData plain, GpgmeData cipher) { if (IsValid) { if (plain == null) { throw new ArgumentNullException("Source data buffer must be supplied."); } if (!(plain.IsValid)) { throw new InvalidDataBufferException("The specified source data buffer is invalid."); } if (cipher == null) { throw new ArgumentNullException("Destination data buffer must be supplied."); } if (!(cipher.IsValid)) { throw new InvalidDataBufferException("The specified destination data buffer is invalid."); } IntPtr[] recp = Gpgme.KeyArrayToIntPtrArray(recipients); lock (CtxLock) { int err = libgpgme.gpgme_op_encrypt_sign( CtxPtr, recp, (gpgme_encrypt_flags_t)flags, plain.dataPtr, cipher.dataPtr); gpg_err_code_t errcode = libgpgerror.gpg_err_code(err); switch (errcode) { case gpg_err_code_t.GPG_ERR_NO_ERROR: break; case gpg_err_code_t.GPG_ERR_UNUSABLE_PUBKEY: break; case gpg_err_code_t.GPG_ERR_GENERAL: // Bug? should be GPG_ERR_UNUSABLE_PUBKEY break; case gpg_err_code_t.GPG_ERR_UNUSABLE_SECKEY: throw new InvalidKeyException("There is one or more invalid signing key(s) in the current context."); case gpg_err_code_t.GPG_ERR_INV_VALUE: throw new InvalidPtrException("Either the context, recipient key array, plain text or cipher text pointer is invalid."); case gpg_err_code_t.GPG_ERR_BAD_PASSPHRASE: throw new BadPassphraseException(); default: throw new GeneralErrorException("An unexpected error " + errcode.ToString() + " (" + err.ToString() + ") occurred."); } IntPtr rstPtr = libgpgme.gpgme_op_encrypt_result(CtxPtr); GC.KeepAlive(recp); GC.KeepAlive(recipients); GC.KeepAlive(plain); GC.KeepAlive(cipher); if (rstPtr != IntPtr.Zero) { EncryptionResult encRst = new EncryptionResult(rstPtr); return(encRst); } else { throw new GeneralErrorException("An unexpected error occurred. " + errcode.ToString()); } } } else { throw new InvalidContextException(); } }
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; }
/// <summary> /// Encrypts a file and writes the ciphertext to another file. /// </summary> /// <param name="context">The <see cref="GpgContext"/> to operate on.</param> /// <param name="inputFilePath">The path to the unencrypted file.</param> /// <param name="outputFilePath">The path where the encrypted file should be saved.</param> /// <param name="encryptFlags">The encryption flags to be used.</param> /// <param name="overwrite">Whether the existing file should be overwritten if a file already exists at <paramref name="outputFilePath"/>.</param> /// <param name="recipients">The GPG keys for which the data should be encrypted.</param> public static void EncryptFile(this GpgContext context, string inputFilePath, string outputFilePath, IEnumerable <GpgKey> recipients, EncryptFlags encryptFlags = EncryptFlags.None, bool overwrite = false) { using (var file = File.Open(outputFilePath, overwrite ? FileMode.Create : FileMode.CreateNew)) using (var plain = MemoryGpgBuffer.CreateFromFile(inputFilePath)) using (var cipher = context.Encrypt(plain, recipients, encryptFlags)) { cipher.CopyTo(file); } }
public EncryptionResult Encrypt(Key[] recipients, EncryptFlags flags, GpgmeData plain, GpgmeData cipher) { if (plain == null) { throw new ArgumentNullException("plain", "Source data buffer must be supplied."); } if (!(plain.IsValid)) { throw new InvalidDataBufferException("The specified source data buffer is invalid."); } if (cipher == null) { throw new ArgumentNullException("cipher", "Destination data buffer must be supplied."); } if (!(cipher.IsValid)) { throw new InvalidDataBufferException("The specified destination data buffer is invalid."); } if (!IsValid) { throw new InvalidContextException(); } IntPtr[] recp = Gpgme.KeyArrayToIntPtrArray(recipients); lock (CtxLock) { #if (VERBOSE_DEBUG) DebugOutput("gpgme_op_encrypt(..) START"); #endif int err = libgpgme.gpgme_op_encrypt( CtxPtr, recp, (gpgme_encrypt_flags_t)flags, plain.dataPtr, cipher.dataPtr); #if (VERBOSE_DEBUG) DebugOutput("gpgme_op_encrypt(..) DONE"); #endif gpg_err_code_t errcode = libgpgerror.gpg_err_code(err); switch (errcode) { case gpg_err_code_t.GPG_ERR_NO_ERROR: break; case gpg_err_code_t.GPG_ERR_UNUSABLE_PUBKEY: break; case gpg_err_code_t.GPG_ERR_GENERAL: // Bug? should be GPG_ERR_UNUSABLE_PUBKEY break; case gpg_err_code_t.GPG_ERR_INV_VALUE: throw new InvalidPtrException( "Either the context, recipient key array, plain text or cipher text pointer is invalid."); case gpg_err_code_t.GPG_ERR_BAD_PASSPHRASE: throw new BadPassphraseException(); case gpg_err_code_t.GPG_ERR_EBADF: throw new InvalidDataBufferException( "The source (plain) or destination (cipher) data buffer is invalid for encryption."); default: throw new GeneralErrorException("An unexpected error " + errcode.ToString() + " (" + err.ToString(CultureInfo.InvariantCulture) + ") occurred."); } IntPtr rst_ptr = libgpgme.gpgme_op_encrypt_result(CtxPtr); #if (VERBOSE_DEBUG) DebugOutput("gpgme_op_encrypt_result(..) DONE"); #endif GC.KeepAlive(recp); GC.KeepAlive(recipients); GC.KeepAlive(plain); GC.KeepAlive(cipher); if (rst_ptr != IntPtr.Zero) { var enc_rst = new EncryptionResult(rst_ptr); return(enc_rst); } throw new GeneralErrorException("An unexpected error occurred. " + errcode.ToString()); } }
public static extern GpgMeError gpgme_op_encrypt(IntPtr ctx, IntPtr[] recp, EncryptFlags flags, IntPtr plain, IntPtr cipher);