title: How do I create an ascii-armored signed OpenPGP message using ArmoredOutputStream of the BouncyCastle crypto library (C# version)
body: <p>When I use ArmoredOutputStream I get a "-----BEGIN PGP MESSAGE-----" instead of a "-----BEGIN PGP SIGNATURE-----" after the clear text. I cannot figure out how to use ArmoredOutputStream on my own. Sometimes I get the signature header, but have non-base64 characters in the message. The BC legionaries must be sadists releasing such a good library with nearly no documentation how to use it...</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    private void doTestSig(
          PublicKeyAlgorithmTag encAlgorithm,
          HashAlgorithmTag hashAlgorithm,
          PgpPublicKey pubKey,
          PgpPrivateKey privKey)
      {
          MemoryStream testIn = new MemoryStream(TEST_DATA, false);
          MemoryStream baseOut = new MemoryStream();
  
          ArmoredOutputStream aOut = new ArmoredOutputStream(baseOut);
          aOut.BeginClearText(hashAlgorithm);
          aOut.Write(testIn.ToArray(), 0, testIn.ToArray().Length);
          aOut.EndClearText();
  
          PgpSignatureGenerator sGen = new PgpSignatureGenerator(encAlgorithm, hashAlgorithm);
          sGen.InitSign(PgpSignature.BinaryDocument, privKey);
          sGen.GenerateOnePassVersion(false).Encode(aOut);
  
  
          PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator();
          Stream lOut = lGen.Open(
              new FilterStream(aOut),
              PgpLiteralData.Binary,
              PgpLiteralDataGenerator.Console,
              TEST_DATA.Length * 2,
              DateTime.UtcNow);
  
          int ch;
          while ((ch = testIn.ReadByte()) &gt;= 0)
          {
              aOut.WriteByte((byte)ch);
              sGen.Update((byte)ch);
          }        
  
          lOut.Write(TEST_DATA, 0, TEST_DATA.Length);
          sGen.Update(TEST_DATA);
  
          lGen.Close();
  
          sGen.Generate().Encode(aOut);
  
          aOut.Close();
  
          byte[] outarr = baseOut.ToArray();
          string strOut = System.Text.Encoding.UTF8.GetString(outarr, 0, outarr.Length);
  // strOut gets this:
  // "-----BEGIN PGP SIGNED MESSAGE-----\r\n
  // Hash: SHA1\r\n
  // \r\n
  // hello world!\n
  // hello world!\n
  // 
  // -----BEGIN PGP MESSAGE-----\r\n
  // Version: BCPG C# v1.7.0.0
  // \r\n
  // \r\n
  // kA0DAAIR4GIXExqVFDEBy0JiCF9DT05TT0xFVBcoRmhlbGxvIHdvcmxkIQpoZWxs\r\n
  // byB3b3JsZCEKaGVsbG8gd29ybGQhCmhlbGxvIHdvcmxkIQqIRgQAEQIABgUCVBco\r\n
  // RgAKCRDgYhcTGpUUMXglAJwNOiEzA7H9RxaoU/gs9AoxZCOd+ACffl8lJ/6ZNpkB\r\n
  // FLBjbd9wlEU3/SY=\r\n=u7Mk\r\n
  // -----END PGP MESSAGE-----\r\n"
  
      }
  </code></pre>