title: How to SFTP a Stream using PGP Encryption
body: <p>I am using PgpCore &amp; SSH.NET to encrypt a stream and create a file on a remote server via sftp with the encrypted stream.</p>
  <p>I have the below attempt, however, while I can successfully list the files in the remote directory, i can not write to a remote file. I get the error <code>no such file</code> at the <code>pgp.EncryptStream</code> line.</p>
  <pre><code>using (var client = new SftpClient(server, 22, username, password))
  {
      client.Connect();
      Console.WriteLine(&quot;Connected to client.&quot;);
      var inputStream = new MemoryStream();
      var fileContents = Encoding.UTF8.GetBytes(sb.ToString());
      inputStream.Write(fileContents, 0, fileContents.Length);
      inputStream.Position = 0;
  
      var outputStream = new MemoryStream();
  
      var files = client.ListDirectory(&quot;/upload/&quot;);
  
      foreach (var file in files)
      {
          string remoteFileName = file.Name;
  
          Console.WriteLine(remoteFileName);
      }
  
      foreach (var file in files)
      {
          string remoteFileName = file.FullName;
  
          Console.WriteLine(remoteFileName);
      }
  
      using (PGP pgp = new PGP())
      {
          Console.WriteLine(&quot;Doing PGP...&quot;);
          using (Stream outputFileStream = client.Create(&quot;/upload/test.txt.pgp&quot;))
          using (Stream publicKeyStream = new FileStream(@&quot;.\PublicKey.asc&quot;, FileMode.Open))
          {
              Console.WriteLine(&quot;Encrypting Stream...&quot;);
              pgp.EncryptStream(inputStream, outputFileStream, publicKeyStream);
              Console.WriteLine(&quot;Stream Encrypted.&quot;);
  
              //Console.WriteLine(&quot;Uploading File.&quot;);
              //client.UploadFile(outputFileStream, string.Format(&quot;/upload/{0}&quot;, &quot;test.txt.pgp&quot;));
              //Console.WriteLine(&quot;Upload Complete.&quot;);
          }
      }
  }
  </code></pre>
  <p>Any ideas on how I can do this?</p>
  <p>Exception Stack Trace:</p>
  <pre><code>   at Renci.SshNet.Sftp.SftpSession.RequestWrite(Byte[] handle, UInt64 serverOffset, Byte[] data, Int32 offset, Int32 length, AutoResetEvent wait, Action`1 writeCompleted)
     at Renci.SshNet.Sftp.SftpFileStream.FlushWriteBuffer()
     at Renci.SshNet.Sftp.SftpFileStream.Dispose(Boolean disposing)
     at System.IO.Stream.Close()
     at Project.Web.Areas.Pims.Controllers.Eod.EodController.EncryptStream(String fileName, StringBuilder sb) in C:\Repos\Solution\Project.Web\Areas\Pims\Controllers\Eod\EodController.cs:line 144
  </code></pre>
  <p>EDIT:
  For those who may find it useful, I managed to get the encrypted file uploaded using <code>client.OpenWrite</code> instead of <code>client.Create</code> as shown below. I have also added code to sign the encrypted file with my pgp Private Key and Passphrase:</p>
  <p><strong>WORKING SOLUTION:</strong></p>
  <pre><code>using (PGP pgp = new PGP())
  {
      using (Stream outputFileStream = client.OpenWrite(&quot;/upload/test.txt.pgp&quot;))
      using (Stream publicKeyStream = new FileStream(@&quot;.\MSPublicKey.asc&quot;, FileMode.Open))
      using (Stream privateKeyStream = new FileStream(@&quot;.\PrivateKey.asc&quot;, FileMode.Open))
      {
          pgp.EncryptStreamAndSign(inputStream, outputFileStream, publicKeyStream, privateKeyStream, pgpPasspharse, true, true);
      }
  }
  </code></pre>