コード例 #1
0
        public FileInfo EncryptFile(string keyUserId, string sourceFile, string encryptedFile)
        {
            // check parameters
            if (string.IsNullOrEmpty(keyUserId))
            {
                throw new ArgumentException("keyUserId parameter is either empty or null", "keyUserId");
            }
            if (string.IsNullOrEmpty(sourceFile))
            {
                throw new ArgumentException("sourceFile parameter is either empty or null", "sourceFile");
            }
            if (string.IsNullOrEmpty(encryptedFile))
            {
                throw new ArgumentException("encryptedFile parameter is either empty or null", "encryptedFile");
            }


            // Create streams - for each of the unencrypted source file and decrypted destination file
            using (Stream sourceFileStream = new FileStream(sourceFile, FileMode.Open))
            {
                using (Stream encryptedFileStream = new FileStream(encryptedFile, FileMode.Create))
                {
                    //  Specify the directory containing gpg.exe (not sure why).
                    gpg.BinaryPath = Path.GetDirectoryName(appPath);
                    gpg.Recipient  = keyUserId;

                    //  Perform encryption
                    gpg.Encrypt(sourceFileStream, encryptedFileStream);
                    return(new FileInfo(encryptedFile));
                }
            }
        }
コード例 #2
0
        /// <summary>
        ///     Will Encrypt any OpenPGP data
        /// </summary>
        /// <param name = "recipient"></param>
        /// <param name = "unencryptedText"></param>
        /// <returns></returns>
        public string Encrypt(string recipient, string unencryptedText)
        {
            // if no recipient is specified then don't do the encryption
            if (string.IsNullOrWhiteSpace(recipient))
            {
                throw new ArgumentNullException();
            }

            // create two memory stream - one to hold the unencrypted data and the other stream holds the encrypted data.
            using (var unencrypted = new MemoryStream(Encoding.ASCII.GetBytes(unencryptedText)))
                using (var encrypted = new MemoryStream())
                {
                    // create a new GnuPG object
                    _gpg.OutputType = OutputTypes.AsciiArmor;
                    _gpg.Recipient  = recipient;
                    _gpg.Encrypt(unencrypted, encrypted);
                    using (var reader = new StreamReader(encrypted))
                    {
                        encrypted.Position = 0;
                        return(reader.ReadToEnd());
                    }
                }
        }
コード例 #3
0
    private string EncryptEmail(string mail, string passphrase, IList<string> recipients)
    {
      using (MemoryStream inputStream = new MemoryStream(mail.Length))
      using (MemoryStream outputStream = new MemoryStream())
      {
        using (StreamWriter writer = new StreamWriter(inputStream))
        {
          // Ready for two passes encryption.
          foreach (string option in new string[] { "", "--trust-model always" })
          {
            _gnuPg.UserCmdOptions = option;

            if (_settings.Encrypt2Self == true)
              _gnuPg.UserCmdOptions += " --encrypt-to " + _settings.DefaultKey;

            writer.Write(mail);
            writer.Flush();
            inputStream.Position = 0;
            _gnuPg.Passphrase = passphrase;
            _gnuPg.Recipients = recipients;
            _gnuPg.OutputStatus = false;

            try
            {
              _gnuPg.Encrypt(inputStream, outputStream);
              break; // Stop two passes here on success.
            }
            catch (Exception ex)
            {
              if (string.IsNullOrEmpty(option) && ex.Message.StartsWith("gpg: C4771111"))
              {
                DialogResult res = MessageBox.Show(
                    ex.Message + Environment.NewLine + Environment.NewLine + "Encrypt mail anyway?",
                    "GnuPG Warning",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Exclamation);
                if (res == DialogResult.Cancel)
                  return _gnuPgErrorString;
              }
              else
              {
                MessageBox.Show(
                ex.Message,
                  "GnuPG Error",
                  MessageBoxButtons.OK,
                  MessageBoxIcon.Error);

                return _gnuPgErrorString;
              }
            }
            finally
            {
              _gnuPg.UserCmdOptions = string.Empty;
            }
          }
        }

        using (StreamReader reader = new StreamReader(outputStream))
        {
          outputStream.Position = 0;
          mail = reader.ReadToEnd();
        }
      }

      return mail;
    }