Stream text/data to zip/gpg instead of passing app a filename?

T

Tony!

I currently have an app written in C# that can take a file and encrypt
it using gpg.exe

What I'm trying to do is, instead of
1. Creating a file (from database queries usually)
2. encrypting the file
3. deleting the non-encrypted file

I want to

1. Gather info into memory (into a dictionary or a list or whatever)
2. stream the text/data into gpg.exe to end up with the encrypted file
outputted

I've looked into pipestream, redirecting standard input to the gpg
process, etc, but I haven't figured out a way to trick gpg.exe into
accepting streamed text/data instead of a file on the hard drive.

Initially figured if I could do it for gpg, I could also do it for Zip
as well, but I'm wondering if it's even possible.
Found some refs to popen which seems to be php related, but nothing
for c#.

Essentially, I'm looking to do the below programatically with text.txt
being stuff in memory streamed to the app instead of an actual file on
the hard drive.

C:\Program Files\GNU\GnuPG>type C:\test.txt | zip > plubber.zip
C:\Program Files\GNU\GnuPG>type C:\test.txt | gpg -er
"mycomp_operations <[email protected]>" > Test.pgp


Thanks for any help you may be able to give :)

Tony!
 
T

Tony!

[...]
I want to
1. Gather info into memory (into a dictionary or a list or whatever)
2. stream the text/data into gpg.exe to end up with the encrypted file
outputted
I've looked into pipestream, redirecting standard input to the gpg
process,  etc, but I haven't figured out a way to trick  gpg.exe into
accepting streamed text/data instead of a file on the hard drive.

That will depend on gpg.exe, and isn't a matter of "tricking" it so much  
as whether it supports input from the standard input or always requires a 
file.

If it always requires a file, then you need to provide it a file.  If it  
accepts input from stdin, then you simply need to write to its stdin  
(which you can do by setting ProcessStartInfo.RedirectStandardInput to  
"true" before starting the process, and then writing to  
Process.StandardInput.BaseStream...if you're dealing with only text data, 
you can just use Process.StandardInput directly).
Initially figured if I could do it for gpg, I could also do it for Zip
as well, but I'm wondering if it's even possible.
Found some refs to popen which seems to be php related, but nothing
for c#.
Essentially, I'm looking to do the below programatically with text.txt
being stuff in memory streamed to the app instead of an actual file on
the hard drive.
C:\Program Files\GNU\GnuPG>type C:\test.txt | zip > plubber.zip
C:\Program Files\GNU\GnuPG>type C:\test.txt | gpg -er
"mycomp_operations <[email protected]>" > Test.pgp

Do those commands work when you type them into the command line?  Then the  
programs support using stdin as their input, and you can do it as I  
describe above.  If not, then you can't.

Pete



The commands typed from the command line work, but when I try to
create the same commands using stdin as their input, I get an error
(the error for gpg being that it doen't recognize what I send because
it returns "usage: gpg [options] [filename]\r\n")

I stole some code from GnuPGDotNet and incorporated it into a testing
app.. But I must have something wrong since I still get the usage
error back

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics; // for Process class
using System.IO; // for StreamWriter/StreamReader classes
using System.Threading; // for Thread class

namespace playing
{
public partial class Form1 : Form
{
// Variables used for monitoring external process and threads
private Process _processObject;
private string _outputString;
private string _errorString;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

ProcessStartInfo pInfo = new ProcessStartInfo("C:\\Program
Files\\GNU\\GnuPG\\gpg.exe", "gpg -er > \"my_operations
<[email protected]>\" > Test.pgp");
pInfo.WorkingDirectory = "C:\\Program Files\\GNU\\GnuPG\
\";
pInfo.CreateNoWindow = true;
pInfo.UseShellExecute = false;
// Redirect everything:
// stdin to send the passphrase, stdout to get encrypted
message, stderr in case of errors...
pInfo.RedirectStandardInput = true;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
_processObject = Process.Start(pInfo);

// Send input text
_processObject.StandardInput.Write("This is a test");
_processObject.StandardInput.Flush();
_processObject.StandardInput.Close();

// Create two threads to read both output/error streams
without creating a deadlock
ThreadStart outputEntry = new ThreadStart
(StandardOutputReader);
Thread outputThread = new Thread(outputEntry);
outputThread.Start();
ThreadStart errorEntry = new ThreadStart
(StandardErrorReader);
Thread errorThread = new Thread(errorEntry);
errorThread.Start();

} /// <summary>
/// Reader thread for standard output
///
/// <p/>Updates the private variable _outputString (locks it
first)
/// </summary>
public void StandardOutputReader()
{
string output = _processObject.StandardOutput.ReadToEnd();
lock (this)
{
_outputString = output;
}
}

/// <summary>
/// Reader thread for standard error
///
/// <p/>Updates the private variable _errorString (locks it
first)
/// </summary>
public void StandardErrorReader()
{
string error = _processObject.StandardError.ReadToEnd();
lock (this)
{
_errorString = error;
}
}

}
}


Tony!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top