I am using MinGW on Windows7 cross platform development tool and was planning to shell out from my console program to console Aescrypt to decrypt a file and re-encrypt it with a new password.
I can do this successfully as two "shells" with an intermediate unencrypted file, but for obvious reasons would prefer not to have anything like that hanging around to clean up. I tested pipes using the command line to see if I could pipe the output from the decryption into the input file of the encryption using STD In and STD OUT.
Combining two examples from the AEScrypt manual, on the windows command line this worked fine with the syntax:
Code: Select all
AEScrypt -d -p pswd1 -o - infile1 | AEScrypt -e -p pswd2 - >outfile1
When I invoke my function the console gives the error:-
Code: Select all
Error writing decrypted block:: Invalid argument
However, When I try escaping the redirection with a caret (ie: ^>outfile) or with escaped brackets (single and double) I get the additional error:-
Code: Select all
Error: STDIN may not be specified with multiple input files.
usage: aescrypt {-e|-d} [-p <password>] { [-o <output filename>] <file> | <file> [<file> ...] }
Error writing decrypted block:: Invalid argument
After execution, the output file contains the three AES headers only, no data is written, so it is getting as far as trying to process the first block to STD OUT. I have tried changing my block size to match or exceed the block size AES reads, but no luck with that either.
I appreciate this may well be a function of the way that the shell is (mis?)treating my parameters, but was wondering if you may be able to give me some clues as to what I'm doing wrong.
My function (tested with ls piped to SORT) is below:
Code: Select all
#include <stdio.h>
int pipe2pipe(char *cmd1, char *cmd2)
{
// -----------------------------------------------------------------------------
// Open two pipes allowing output from one command to be input to the other
// -----------------------------------------------------------------------------
FILE *pipein_fp, *pipeout_fp;
char readbuf[MAX_PIPE_BUFF];
long bytes;
// create one-way pipe for input
if (( pipein_fp = popen(cmd1, "r")) == NULL)
{
perror("popen");
return(1);
}
// create one-way pipe for output
if (( pipeout_fp = popen(cmd2, "w")) == NULL)
{
perror("popen");
return(2);
}
// Get input buffer and write it to the output pipe
while( (bytes=fread(readbuf,sizeof(readbuf),1,pipein_fp))>0)
{
fwrite(readbuf,bytes,1,pipeout_fp);
}
// Clean up
pclose(pipein_fp);
pclose(pipeout_fp);
return(0);
}
Regards,
Gary