XInputStream aCipherText, XOutputStream aPlainText )
throws IOException, PGPException
{
assumeAvailable();
SignatureInfo info = null;
try
{
byte[][] readBuff= new byte[1][];
int nRead;
int nSizeLine=100;
// read in the first 100 bytes
nRead = aCipherText.readBytes( readBuff, nSizeLine );
// Check if a pass phrase is required. This is not the case if
// the text has just been signed and not encrypted. To find out
// we look for the string "BEGIN PGP SIGNED MESSAGE". This only works
// if pgp was used with options -sat !!!!
// If a pass phrase is required than prompt the user for it
String passPhrase= null;
String text= new String( readBuff[0], 0, nSizeLine); //uses Default byte to character encoding
if( text.indexOf("BEGIN PGP SIGNED MESSAGE") == -1){
passPhrase= dialog.getPassPhrase();
if( passPhrase == null)
return null;
}
//---
String params[];
if( passPhrase == null)
params= new String[]{"pgp", "+batchmode", "+language=en", "-f"};
else
params= new String[] { "pgp", "+batchmode", "+language=en", "-f", "-z", passPhrase };
Runtime runtime = Runtime.getRuntime();
Process proc = null;
proc = runtime.exec( params );
OutputStream stdout = proc.getOutputStream();
// fed pgp with the actual text
stdout.write( readBuff[0], 0, nRead); // write the formerly read data
do
{
nRead = aCipherText.readBytes( readBuff, 0x10000 );
if (DEBUG)
{
System.out.print( "# writing stdout: " );
byte ar[] = readBuff[0];
for ( int n = 0; n < nRead; ++n )
{
System.out.print( (char)ar[n] );
if (n < ar.length-1)
System.out.print( ", " );
}
}
stdout.write( readBuff[0], 0, nRead);
}
while (nRead == 0x10000);
stdout.close();
// scan error stream
BufferedReader stderr = new BufferedReader( new InputStreamReader( proc.getErrorStream() ) );
String str;
while ((str = stderr.readLine()) != null)
{
if (str.indexOf( "Error:" ) == 1)
{
// proc.waitFor();
throw new PGPException( str.substring( 7 ).trim() );
}
/* If text was signed then there is an output like:
* Good signature from user "Joachim Lingner <jlingner@gmx.de>".
*/
else if (str.indexOf( "signature from user" ) != -1)
{
String user = str.substring( str.indexOf( '\"' )-1, str.lastIndexOf( '\"' ) ).trim();
info = new SignatureInfo();
info._userId = user;
info._bSignatureChecked = true;
info._bMessageVerified = (str.indexOf( "Good" ) == 0);
}
else if (str.indexOf( "Key matching expected Key ID" ) == 1)
{
String user = str.substring( 28, str.indexOf( "not found" ) ).trim();
info = new SignatureInfo();
info._userId = user;
info._bSignatureChecked = false;
info._bMessageVerified = false;
}
}