com.didisoft.pgp.inspect
Class PGPInspectLib

java.lang.Object
  extended by com.didisoft.pgp.bc.BaseLib
      extended by com.didisoft.pgp.inspect.PGPInspectLib

public class PGPInspectLib
extends com.didisoft.pgp.bc.BaseLib

Provides methods for inspecting OpenPGP files and streams.


Copyright (c) DidiSoft Ltd 2012.
www.didisoft.com


Field Summary
 
Fields inherited from class com.didisoft.pgp.bc.BaseLib
BOUNCY_CASTLE_PROVIDER
 
Constructor Summary
PGPInspectLib()
           
 
Method Summary
 boolean isPBEEncrypted(java.io.InputStream dataStream)
          Checks is a given OpenPGP stream encrypted with a password.
 boolean isPBEEncrypted(java.lang.String fileName)
          Checks is a given OpenPGP file encrypted with a password.
 boolean isPublicKeyEncrypted(java.io.InputStream dataStream)
          Checks is a given OpenPGP stream encrypted with a public key.
 boolean isPublicKeyEncrypted(java.lang.String fileName)
          Checks is a given OpenPGP file encrypted with a public key.
 boolean isSignedOnly(java.io.InputStream dataStream)
          Checks is a given OpenPGP archive stream signed only
 boolean isSignedOnly(java.lang.String fileName)
          Checks is a given OpenPGP archive signed only
 SignatureItem[] listDetachedSignatureFile(java.lang.String detachedSignatureFile)
          Lists information for an OpenPGP detached signature file
 SignatureItem[] listDetachedSignatureStream(java.io.InputStream detachedSignatureStream)
          Lists information for an OpenPGP detached signature stream
 long[] listEncryptionKeyIds(java.io.InputStream dataStream)
          Returns the Key Id's that have encrypted a given OpenPGP archive
 long[] listEncryptionKeyIds(java.lang.String fileName)
          Returns the Key Id's that have encrypted a given OpenPGP archive
 ContentItem[] listOpenPGPFile(java.lang.String signedFileName)
          Lists the contents of an OpenPGP signed file

Example:
 ContentItem[] listOpenPGPFile(java.lang.String fileName, java.lang.String privateKeyFileName, java.lang.String privateKeyPassword)
          Lists the contents of an OpenPGP encrypted file

Example:
 ContentItem[] listOpenPGPStream(java.io.InputStream signedStream)
          Lists the contents of an OpenPGP signed stream

Example:
 ContentItem[] listOpenPGPStream(java.io.InputStream encryptedStream, java.io.InputStream privateKeyStream, java.lang.String privateKeyPassword)
          Lists the contents of an OpenPGP encrypted stream

Example:
 long[] listSigningKeyIds(java.io.InputStream dataStream)
          Returns the Key Id's that have signed a given OpenPGP signed only archive
 long[] listSigningKeyIds(java.io.InputStream encryptedStream, java.io.InputStream privateKeyStream, java.lang.String privateKeyPassword)
          Returns a list of the Key Id's for the key(s) that have encrypted a given OpenPGP archive
 long[] listSigningKeyIds(java.lang.String dataFileName)
          Returns the Key Id's that have signed a given OpenPGP signed only archive
 long[] listSigningKeyIds(java.lang.String dataFileName, java.lang.String privateKeyFileName, java.lang.String privateKeyPassword)
          Returns the Key Id's that have signed a given OpenPGP archive
 
Methods inherited from class com.didisoft.pgp.bc.BaseLib
extractPrivateKey, isForVerification, replaceAll
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PGPInspectLib

public PGPInspectLib()
Method Detail

isPublicKeyEncrypted

public boolean isPublicKeyEncrypted(java.lang.String fileName)
                             throws java.io.IOException,
                                    NonPGPDataException
Checks is a given OpenPGP file encrypted with a public key.

Parameters:
fileName - source file to be checked
Returns:
true if the file is an OpenPGP archive encrypted with a public key, false if this is another type of OpenPGP archive
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source file is not an OpenPGP archive at all

isPublicKeyEncrypted

public boolean isPublicKeyEncrypted(java.io.InputStream dataStream)
                             throws java.io.IOException,
                                    NonPGPDataException
Checks is a given OpenPGP stream encrypted with a public key.

Parameters:
dataStream - OpenPGP source stream to be checked
Returns:
true if the stream is an OpenPGP archive encrypted with a public key, false if it is another type of OpenPGP archive
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source is not an OpenPGP archive at all

isPBEEncrypted

public boolean isPBEEncrypted(java.lang.String fileName)
                       throws java.io.IOException,
                              NonPGPDataException
Checks is a given OpenPGP file encrypted with a password.

Parameters:
fileName - source file to be checked
Returns:
true if the file is an OpenPGP archive encrypted with a password, false if this is another type of OpenPGP archive
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source file is not an OpenPGP archive at all

isPBEEncrypted

public boolean isPBEEncrypted(java.io.InputStream dataStream)
                       throws java.io.IOException,
                              NonPGPDataException
Checks is a given OpenPGP stream encrypted with a password.

Parameters:
dataStream - source stream to be checked
Returns:
true if the stream is an OpenPGP archive encrypted with a password, false if this is another type of OpenPGP archive
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source file is not an OpenPGP archive at all

listEncryptionKeyIds

public long[] listEncryptionKeyIds(java.lang.String fileName)
                            throws java.io.IOException,
                                   NonPGPDataException
Returns the Key Id's that have encrypted a given OpenPGP archive

Parameters:
fileName - encrypted OpenPGP archive
Returns:
list of encryption Key Id's
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source file is not an OpenPGP archive at all

listEncryptionKeyIds

public long[] listEncryptionKeyIds(java.io.InputStream dataStream)
                            throws java.io.IOException,
                                   NonPGPDataException
Returns the Key Id's that have encrypted a given OpenPGP archive

Parameters:
dataStream - encrypted OpenPGP stream
Returns:
list of encryption Key Id's
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source is not an OpenPGP archive at all

listDetachedSignatureFile

public SignatureItem[] listDetachedSignatureFile(java.lang.String detachedSignatureFile)
                                          throws java.io.IOException,
                                                 NonPGPDataException
Lists information for an OpenPGP detached signature file

Parameters:
detachedSignatureFile - OpenPGP detached signature file
Returns:
array of SignatureItem objects
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source is not an OpenPGP detached signature Example:
  PGPLib pgp = new PGPLib();
  
  // should the output be in ASCII or binary format 
  boolean asciiArmour = true;
  pgp.detachedSignFile("myfile.txt", 
                                        "my_private_key.asc", 
                                        "my_private_key password", 
                                        "detached.sig", 
                                        asciiArmour);
  
  PGPInspectLib inspect = new PGPInspectLib();
  com.didisoft.pgp.inspect.SignatureItem[] signatures = 
        inspect.listDetachedSignatureFile("detached.sig");
  
  for (SignatureItem sig : signatures) {
        System.out.println("Signednature produced with Key Id: " + sig.getKeyId());
        System.out.println("Signednature created on: " + sig.getSignatureTime());
  }
 

listDetachedSignatureStream

public SignatureItem[] listDetachedSignatureStream(java.io.InputStream detachedSignatureStream)
                                            throws java.io.IOException,
                                                   NonPGPDataException
Lists information for an OpenPGP detached signature stream

Parameters:
detachedSignatureFile - OpenPGP detached signature stream
Returns:
array of SignatureItem objects
Throws:
java.io.IOException - I/O error
NonPGPDataException - if the source is not an OpenPGP detached signature

listSigningKeyIds

public long[] listSigningKeyIds(java.lang.String dataFileName)
                         throws PGPException,
                                java.io.IOException
Returns the Key Id's that have signed a given OpenPGP signed only archive

Parameters:
dataFileName - OpenPGP signed only data
Returns:
list of signing Key Id's, empty if no signatures are present
Throws:
java.io.IOException - I/O error
PGPException - general OpenPGP error
NonPGPDataException - if the source file is not an OpenPGP archive at all
FileIsEncryptedException - if the source file is not only signed but also encrypted

listSigningKeyIds

public long[] listSigningKeyIds(java.io.InputStream dataStream)
                         throws PGPException,
                                java.io.IOException
Returns the Key Id's that have signed a given OpenPGP signed only archive

Parameters:
dataStream - OpenPGP signed only input stream
Returns:
list of signing Key Id's, empty if no signatures are present
Throws:
java.io.IOException - I/O error
PGPException - general OpenPGP error
NonPGPDataException - if the source is not an OpenPGP archive at all
FileIsEncryptedException - if the source is not only signed but also encrypted

listSigningKeyIds

public long[] listSigningKeyIds(java.lang.String dataFileName,
                                java.lang.String privateKeyFileName,
                                java.lang.String privateKeyPassword)
                         throws PGPException,
                                java.io.IOException
Returns the Key Id's that have signed a given OpenPGP archive

Parameters:
encryptedStream - OpenPGP encrypted data
privateKeyStream - private key data in order to decrypt the file and inspect the signatures
privateKeyPassword - private key
Returns:
list of signing Key Id's, empty if no signatures are present
Throws:
java.io.IOException - I/O error
PGPException - OpenPGP decryption error
NonPGPDataException - if the source file is not an OpenPGP archive at all
WrongPrivateKeyException - if the source file was not encrypted with this private key. Extends PGPException
WrongPasswordException - if the password for this private key is misspelled. Extends PGPException

isSignedOnly

public boolean isSignedOnly(java.lang.String fileName)
                     throws PGPException,
                            java.io.IOException
Checks is a given OpenPGP archive signed only

Parameters:
fileName - encrypted OpenPGP archive
Returns:
true if the archive is only signed, false if it is also encrypted
Throws:
java.io.IOException - I/O error
PGPException - OpenPGP decryption error
NonPGPDataException - if the source file is not an OpenPGP archive at all

isSignedOnly

public boolean isSignedOnly(java.io.InputStream dataStream)
                     throws PGPException,
                            java.io.IOException
Checks is a given OpenPGP archive stream signed only

Parameters:
dataStream - input stream obtained from a OpenPGP archive
Returns:
true if the archive is only signed, false if it is encrypted
Throws:
java.io.IOException - I/O error
PGPException - OpenPGP error
NonPGPDataException - if the source is not an OpenPGP archive at all

listSigningKeyIds

public long[] listSigningKeyIds(java.io.InputStream encryptedStream,
                                java.io.InputStream privateKeyStream,
                                java.lang.String privateKeyPassword)
                         throws PGPException,
                                java.io.IOException
Returns a list of the Key Id's for the key(s) that have encrypted a given OpenPGP archive

Parameters:
encryptedStream - OpenPGP encrypted data
privateKeyStream - private key data in order to decrypt the file and inspect the signatures
privateKeyPassword - private key
Returns:
list of signing Key Id's, empty if no signatures are present
Throws:
java.io.IOException - I/O error
PGPException - OpenPGP decryption error
NonPGPDataException - if the source file is not an OpenPGP archive at all
WrongPrivateKeyException - if the input stream was not encrypted with this private key. Extends PGPException
WrongPasswordException - if the password for this private key is misspelled. Extends PGPException

listOpenPGPFile

public ContentItem[] listOpenPGPFile(java.lang.String fileName,
                                     java.lang.String privateKeyFileName,
                                     java.lang.String privateKeyPassword)
                              throws PGPException,
                                     java.io.IOException
Lists the contents of an OpenPGP encrypted file

Example:
 import com.didisoft.pgp.*;
 import com.didisoft.pgp.inspect.*;
 
 public class InspectEncryptedFile {
 
     public static void main(String[] args) throws Exception {
 
         // Inspect the content of the encrypted file without decrypting
         PGPInspectLib inspectLib = new PGPInspectLib();
         
         String privateKey = "DataFiles/private.key";
         String privateKeyPassword = "changeit";
         String encryptedFile = "DataFiles/encrypted.pgp";
         
         ContentItem[] files = inspectLib.listOpenPGPFile(encryptedFile, privateKey, privateKeyPassword);
         for (int i = 0; i < files.length; i++) {
             System.out.print(files[i].getFileName());
             System.out.print(files[i].isDirectory() ? "" : "     ");
             System.out.println(files[i].getModificationDate());
         }
     }
 }
 

Parameters:
encryptedFileName - OpenPGP encrypted file (absolute or relative path)
privateKeyFileName - Private Key file name (absolute or relative path)
privateKeyPassword - Private key password
Returns:
array of ContentItem elements contained in the specified OpenPGP file
Throws:
java.io.IOException - If a file/stream operation throws an exception
PGPException - if a problem has occurred while decrypting file
WrongPrivateKeyException - if the file was not encrypted with this private key.
WrongPasswordException - if the password for this private key is wrong.
NonPGPDataException - if the input file is not a valid OpenPGP encrypted file

listOpenPGPFile

public ContentItem[] listOpenPGPFile(java.lang.String signedFileName)
                              throws PGPException,
                                     java.io.IOException
Lists the contents of an OpenPGP signed file

Example:
 import com.didisoft.pgp.*;
 import com.didisoft.pgp.inspect.*;
 
 public class InspectEncryptedFile {
 
     public static void main(String[] args) throws Exception {
 
         // Inspect the content of the encrypted file without decrypting
         PGPInspectLib inspectLib = new PGPInspectLib();
         
         String encryptedFile = "DataFiles/encrypted.pgp";
         
         ContentItem[] files = null;
         if (inspectLib.isSignedOnly(encryptedFile)) {
           files = inspectLib.listOpenPGPStream(encryptedFile);
         } else {
           String privateKey = "DataFiles/private.key";
           String privateKeyPassword = "changeit";
           files = inspectLib.listOpenPGPFile(encryptedFile, privateKey, privateKeyPassword);
         }
         
         for (int i = 0; i < files.length; i++) {
             System.out.print(files[i].getFileName());
             System.out.print(files[i].isDirectory() ? "" : "     ");
             System.out.println(files[i].getModificationDate());
         }
     }
 }
 

Parameters:
signedFileName - OpenPGP encrypted file (absolute or relative path)
Returns:
array of ContentItem elements contained in the specified OpenPGP file
Throws:
java.io.IOException - if a file/stream operation throws an exception
PGPException - if a problem has occurred while decrypting file
FileIsEncryptedException - if the supplied data is encrypted
WrongPrivateKeyException - if the file was not encrypted with this private key.
WrongPasswordException - if the password for this private key is wrong.
NonPGPDataException - if the input file is not a valid OpenPGP encrypted file

listOpenPGPStream

public ContentItem[] listOpenPGPStream(java.io.InputStream encryptedStream,
                                       java.io.InputStream privateKeyStream,
                                       java.lang.String privateKeyPassword)
                                throws PGPException,
                                       java.io.IOException
Lists the contents of an OpenPGP encrypted stream

Example:
 import java.io.*;
 
 import com.didisoft.pgp.*;
 import com.didisoft.pgp.inspect.*;
 
 public class InspectEncryptedStream {
 
     public static void main(String[] args) throws Exception {
 
         // Inspect the content of an encrypted stream without decrypting
         PGPInspectLib inspectLib = new PGPInspectLib();
         
         InputStream privateKey = new FileInputStream("DataFiles/private.key");
         String privateKeyPassword = "changeit";
         InputStream encryptedStream = new BufferedInputStream(new FileInputStream("DataFiles/encrypted.pgp"));
         
         ContentItem[] files = inspectLib.listOpenPGPStream(encryptedStream, privateKey, privateKeyPassword);
         for (int i = 0; i < files.length; i++) {
             System.out.print(files[i].getFileName());
             System.out.print(files[i].isDirectory() ? "" : "     ");
             System.out.println(files[i].getModificationDate());
         }
         
         privateKey.close();
         encryptedStream.close();
     }
 }
 

Parameters:
encryptedStream - OpenPGP encrypted or signed stream
privateKeyStream - Private key file stream
privateKeyPassword - Private key password
Returns:
array of ContentItem elements contained in the specified OpenPGP file
Throws:
java.io.IOException - If a file/stream operation throws an exception
PGPException - if a problem has occurred while decrypting file
WrongPrivateKeyException - if the file was not encrypted with this private key.
WrongPasswordException - if the password for this private key is wrong.
NonPGPDataException - if the input file is not a valid OpenPGP encrypted file

listOpenPGPStream

public ContentItem[] listOpenPGPStream(java.io.InputStream signedStream)
                                throws PGPException,
                                       java.io.IOException
Lists the contents of an OpenPGP signed stream

Example:
 import java.io.*;
 
 import com.didisoft.pgp.*;
 import com.didisoft.pgp.inspect.*;
 
 public class InspectSignedStream {
 
     public static void main(String[] args) throws Exception {
 
         // Inspect the content of an encrypted stream without decrypting
         PGPInspectLib inspectLib = new PGPInspectLib();
         
         String pgpFile = "DataFiles/encrypted.pgp";
         InputStream encryptedStream = new FileInputStream(pgpFile);
         
         ContentItem[] files = null;
         if (inspectLib.isSignedOnly(pgpFile)) {
           files = inspectLib.listOpenPGPStream(encryptedStream);
         } else {
           InputStream privateKey = new FileInputStream("DataFiles/private.key");
           String privateKeyPassword = "changeit";
           files = inspectLib.listOpenPGPStream(encryptedStream, privateKey, privateKeyPassword);
         }

         for (int i = 0; i < files.length; i++) {
             System.out.print(files[i].getFileName());
             System.out.print(files[i].isDirectory() ? "" : "     ");
             System.out.println(files[i].getModificationDate());
         }

         privateKey.close();
         encryptedStream.close();
     }
 }
 

Parameters:
signedStream - OpenPGP signed stream
Returns:
array of ContentItem elements contained in the specified OpenPGP file
Throws:
java.io.IOException - If a file/stream operation throws an exception
PGPException - if a problem has occurred while decrypting file
WrongPrivateKeyException - if the data was not encrypted with this private key.
WrongPasswordException - if the password for this private key is wrong.
FileIsEncryptedException - if the supplied data is encrypted
NonPGPDataException - if the data is not a valid OpenPGP encrypted message


Copyright © 2006-2013 DidiSoft Ltd. All Rights Reserved.