com.didisoft.pgp
Class PGPKeyPair

java.lang.Object
  extended by com.didisoft.pgp.KeyPairInformation
      extended by com.didisoft.pgp.PGPKeyPair
All Implemented Interfaces:
java.io.Serializable

public class PGPKeyPair
extends KeyPairInformation
implements java.io.Serializable

Represents an OpenPGP key loaded from a key file.

Provides methods for key generation and key export.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.didisoft.pgp.KeyPairInformation
KeyPairInformation.SubKey
 
Constructor Summary
PGPKeyPair(java.lang.String fileName)
          Initializes the object from a PGP key file.
PGPKeyPair(java.lang.String publicKeyFileName, java.lang.String privateKeyFileName)
          Initializes the object from a public and private PGP key files.
 
Method Summary
 void changePrivateKeyPassword(java.lang.String oldPassword, java.lang.String newPassword)
          Changes the password of this private key.
static PGPKeyPair generateKeyPair(int keySize, java.lang.String userId, java.lang.String keyAlgorithm, java.lang.String password, java.lang.String[] compressionTypes, java.lang.String[] hashingAlgorithmTypes, java.lang.String[] cipherTypes, long expirationAfterDays)
          Generates an OpenPGP key pair (public and private key).
 java.lang.String getAsciiVersionHeader()
          Returns the Version comment text that is printed in ASCII armored output
 void setAsciiVersionHeader(java.lang.String creator)
          Sets the Version comment text that is printed in ASCII armored output

Example usage:
 
Methods inherited from class com.didisoft.pgp.KeyPairInformation
checkPassword, exportKeyRing, exportPrivateKey, exportPublicKey, getAlgorithm, getCreationTime, getFingerprint, getKeyID, getKeyIDHex, getKeySize, getPrivateSubKeys, getPublicSubKeys, getRawPrivateKeyRing, getRawPublicKeyRing, getTrust, getUserIDs, getValidDays, getVersion, hasPrivateKey, isEncryptionKey, isExpired, isExpiredOnDate, isRevoked, isSigningKey, setPrivateKeyRing, setPublicKeyRing
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PGPKeyPair

public PGPKeyPair(java.lang.String fileName)
           throws NoPublicKeyFoundException
Initializes the object from a PGP key file.
The PGP key file can be a public key file, a private key file, or combined (mixed).

Parameters:
fileName - absolute or relative path to a PGP key file.
Throws:
NoPublicKeyFoundException - if the file specified through fileName does not contain a PGP key or key pair

PGPKeyPair

public PGPKeyPair(java.lang.String publicKeyFileName,
                  java.lang.String privateKeyFileName)
           throws NoPublicKeyFoundException,
                  WrongPrivateKeyException
Initializes the object from a public and private PGP key files.

Parameters:
publicKeyFileName - absolute or relative path to the public PGP key file.
privateKeyFileName - absolute or relative path to the private PGP key file.
Throws:
NoPublicKeyFoundException - if the file specified through publicKeyFileNameName does not contain a PGP key
WrongPrivateKeyException - if the private key specified with privateKeyFileName does not belong to the public key specified with publicKeyFileName
Method Detail

getAsciiVersionHeader

public java.lang.String getAsciiVersionHeader()
Returns the Version comment text that is printed in ASCII armored output

Returns:
string of the form "Version: XXX"

setAsciiVersionHeader

public void setAsciiVersionHeader(java.lang.String creator)
Sets the Version comment text that is printed in ASCII armored output

Example usage:
 PGPKeyPair key = new PGPKeyPair("my_key.asc");
 key.setAsciiVersionHeader("My Application 1.1"); 
 // Now in ASCII armored export will be printed "Version: My Application 1.1"  
 

Parameters:
creator - Program name and version that will be written in ASCII armored output Version: field

generateKeyPair

public static PGPKeyPair generateKeyPair(int keySize,
                                         java.lang.String userId,
                                         java.lang.String keyAlgorithm,
                                         java.lang.String password,
                                         java.lang.String[] compressionTypes,
                                         java.lang.String[] hashingAlgorithmTypes,
                                         java.lang.String[] cipherTypes,
                                         long expirationAfterDays)
                                  throws PGPException
Generates an OpenPGP key pair (public and private key).
Note: for key size larger than 2048 bits key generation will take a few moments.

Example usage:
 import com.didisoft.pgp.*;
 
 public class GenerateKeyPairRSA {
  public static void main(String[] args) throws Exception {
         
     // key primary user Id
     String userId = "demo2@didisoft.com";
         
     // preferred hashing algorithms
     String[] hashingAlgorithms = new String[]
                               {HashAlgorithm.SHA1,
                                HashAlgorithm.SHA256,
                                HashAlgorithm.SHA384,
                                HashAlgorithm.SHA512,
                                HashAlgorithm.MD5};
  
     // preferred compression algorithms
     String[] compressions = new String[]
                              {CompressionAlgorithm.ZIP,
                              CompressionAlgorithm.ZLIB,
                              CompressionAlgorithm.UNCOMPRESSED};
  
     // preferred symmetric key algorithms
     String[] cyphers = new String[]
                       {CypherAlgorithm.CAST5,
                        CypherAlgorithm.AES_128,
                        CypherAlgorithm.AES_192,
                        CypherAlgorithm.AES_256,
                        CypherAlgorithm.TWOFISH};
  
     String privateKeyPassword = "changeit";
  
     // the key will be valid for 1 year
     long keyExpiresAfter = 365; 
  
     int keySizeInBits = 2048;
     PGPKeyPair key = PGPKeyPair.generateKeyPair(keySizeInBits, 
                                               userId, 
                                               KeyAlgorithm.RSA, 
                                               privateKeyPassword, 
                                               compressions, 
                                               hashingAlgorithms, 
                                               cyphers,
                                               keyExpiresAfter);
                                               
     // we can call key.export ... in order to save the generated key in a file                                                 
  }
 }
 

Parameters:
keySize - Size of the keys in bits
minimum key size is 512
highest suggested key size is 4096 bits for RSA and 3072 for ELGAMAL (DH/DSS)
userId - User Id of the form "name (comment) <email address>"
keyAlgorithm - Key algorithm. Possible values: RSA, ELGAMAL (equivalent of DS/DHH)
password - Secret key password.
compressionTypes - Compression algorithms supported by the key. @see CompressionAlgorithm
hashingAlgorithmTypes - Hashing algorithms supported by the key.
Comma separated list of one or more of: SHA256, SHA384, SHA512, SHA224, SHA1, MD5, RIPEMD160, MD2
cipherTypes - Symmetric algorithms supported by the key.
Comma separated list of one or more of: TRIPLE_DES, CAST5, BLOWFISH, AES_128, AES_192, AES_256, TWOFISH, DES, SAFER
expirationAfterDays - Number of days the key will be valid. For example 365 for one year. Use 0 (zero) for no expiration date.
Returns:
The generated key pair object
Throws:
org.bouncycastle.openpgp.PGPException - Key generation error
PGPException

changePrivateKeyPassword

public void changePrivateKeyPassword(java.lang.String oldPassword,
                                     java.lang.String newPassword)
                              throws WrongPasswordException,
                                     NoPrivateKeyFoundException,
                                     PGPException
Changes the password of this private key.


Example usage:
 import com.didisoft.pgp.*;
 import com.didisoft.pgp.exceptions;
 
 public class ChangePrivateKeyPasswordDemo {
  public static void main(String[] args) throws Exception{
   // initialize the key object
   PGPKeyPair key = new PGPKeyPair("my_private_key.asc");
 
   // change the key password
   try {
      key.changePrivateKeyPassword("old password", "new password");
   } catch (WrongPasswordException e) }
      System.out.println("The old password is not correct.");
   }
  }
 }
 

Parameters:
oldPassword - current password of the private key
newPassword - new password of the private key
Throws:
WrongPasswordException - if the old password is incorrect (extends PGPException)
NoPrivateKeyFoundException - if no private key has been loaded in this key pair object (extends PGPException)
org.bouncycastle.openpgp.PGPException - general error
PGPException


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