Example #1
0
        /// <summary>
        /// Attempts to decrypt the contents of a data buffer.
        /// If required, the user will be prompted for a password using the default pinentry program.
        /// </summary>
        /// <param name="cipher">A DataBuffer containing the data to be decrypted.</param>
        /// <returns>A DataBuffer containing the decrypted data.</returns>
        public GpgBuffer Decrypt(GpgBuffer cipher)
        {
            var output = MemoryGpgBuffer.Create();

            ErrorHandler.Check(GpgMeWrapper.gpgme_op_decrypt(Handle, cipher.Handle, output.Handle));
            output.Position = 0;
            return(output);
        }
Example #2
0
 /// <summary>
 /// Decrypts a file and returns its contents.
 /// </summary>
 /// <param name="context">The <see cref="GpgContext"/> to operate on.</param>
 /// <param name="inputFilePath">The path to the encrypted file.</param>
 public static string DecryptFile(this GpgContext context, string inputFilePath)
 {
     using (var cipher = MemoryGpgBuffer.CreateFromFile(inputFilePath))
         using (var reader = new StreamReader(context.Decrypt(cipher)))
         {
             return(reader.ReadToEnd());
         }
 }
Example #3
0
 /// <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);
             }
 }
Example #4
0
 /// <summary>
 /// Decrypts a file and writes its decrypted content to another file.
 /// </summary>
 /// <param name="context">The <see cref="GpgContext"/> to operate on.</param>
 /// <param name="inputFilePath">The path to the encrypted file.</param>
 /// <param name="outputFilePath">The path where the decrypted file should be saved.</param>
 /// <param name="overwrite">Whether the existing file should be overwritten if a file already exists at <paramref name="outputFilePath"/>.</param>
 public static void DecryptFile(this GpgContext context, string inputFilePath, string outputFilePath, bool overwrite = false)
 {
     using (var file = File.Open(outputFilePath, overwrite ? FileMode.Create : FileMode.CreateNew))
         using (var cipher = MemoryGpgBuffer.CreateFromFile(inputFilePath))
             using (var plain = context.Decrypt(cipher))
             {
                 plain.CopyTo(file);
             }
 }
Example #5
0
        /// <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);
        }