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 generateEccKeyPair(java.lang.String ecCurve, java.lang.String userId, java.lang.String password)
          Generates an OpenPGP key pair with Elliptic Curve cryptography (ECC) asymmetric encryption algorithm

Note: The generated key pair has no expiration date
static PGPKeyPair generateEccKeyPair(java.lang.String ecCurve, java.lang.String userId, java.lang.String password, java.lang.String[] compressionTypes, java.lang.String[] hashingAlgorithmTypes, java.lang.String[] cipherTypes)
          Generates an OpenPGP key pair with Elliptic Curve cryptography (ECC) asymmetric encryption algorithm

Note: The generated key pair has no expiration date
static PGPKeyPair generateEccKeyPair(java.lang.String ecCurve, java.lang.String userId, java.lang.String password, java.lang.String[] compressionTypes, java.lang.String[] hashingAlgorithmTypes, java.lang.String[] cipherTypes, long expirationAfterDays)
          Generates an OpenPGP key pair with Elliptic Curve cryptography (ECC) asymmetric encryption algorithm
static PGPKeyPair generateElGamalKeyPair(int keySize, java.lang.String userId, java.lang.String password)
          Generates OpenPGP Key pair with ElGamal (DH/DSS) encryption and predefined values for compression, hashing and cypher like the ones generated with PGP Desktop(tm) key generation wizard.
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).
static PGPKeyPair generateRsaKeyPair(int keySize, java.lang.String userId, java.lang.String password)
          Generates OpenPGP Key pair with RSA encryption and predefined values for compression, hashing and cypher like the ones generated with PGP Desktop(tm) key generation wizard.
 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, getUserID, getUserIDs, getValidDays, getVersion, hasPrivateKey, isEncryptionKey, isExpired, isExpiredOnDate, isRevoked, isSigningKey, keyId2Hex, 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

generateEccKeyPair

public static PGPKeyPair generateEccKeyPair(java.lang.String ecCurve,
                                            java.lang.String userId,
                                            java.lang.String password)
                                     throws PGPException
Generates an OpenPGP key pair with Elliptic Curve cryptography (ECC) asymmetric encryption algorithm

Note: The generated key pair has no expiration date

Parameters:
ecCurve - Elliptic curve of the key pair (see EcCurve for supported curves)
userId - user Id of this key
password - password for the private key (can be empty string if none)
Returns:
a PGPKeyPair object representing the newly created key pair
Throws:
PGPException - if an error occurs
java.lang.IllegalArgumentException - if the curve parameter or one of the preferred algorithms parameters is invalid

Example usage:
 import com.didisoft.pgp.*;
 
 public class GenerateECCKeyPair {
  public static void main(String[] args) throws Exception {
         
     // key primary user Id
     String userId = "demo2@didisoft.com";
         
     String privateKeyPassword = "changeit";
  
          // EC curve for this key
     String curve = EcCurve.P384;

     PGPKeyPair key = PGPKeyPair.generateEccKeyPair(curve, 
                                               userId, 
                                               privateKeyPassword);
                                               
     // we can call key.export ... in order to save the generated key in a file                                                 
  }
 }
 

generateEccKeyPair

public static PGPKeyPair generateEccKeyPair(java.lang.String ecCurve,
                                            java.lang.String userId,
                                            java.lang.String password,
                                            java.lang.String[] compressionTypes,
                                            java.lang.String[] hashingAlgorithmTypes,
                                            java.lang.String[] cipherTypes)
                                     throws PGPException
Generates an OpenPGP key pair with Elliptic Curve cryptography (ECC) asymmetric encryption algorithm

Note: The generated key pair has no expiration date

Parameters:
ecCurve - Elliptic curve of the key pair (see EcCurve for supported curves)
userId - user Id of this key
password - password for the private key (can be empty string if none)
compressionTypes - Compression algorithms supported by the key. (see CompressionAlgorithm)
hashingAlgorithmTypes - Hashing algorithms supported by the key. (see HashAlgorithm)
cipherTypes - Symmetric algorithms supported by the key. (see CypherAlgorithm)
Returns:
a PGPKeyPair object representing the newly created key pair
Throws:
PGPException - if an error occurs
java.lang.IllegalArgumentException - if the curve parameter or one of the preferred algorithms parameters is invalid

Example usage:
 import com.didisoft.pgp.*;
 
 public class GenerateECCKeyPair {
  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.SHA256,
                                HashAlgorithm.SHA384,
                                HashAlgorithm.SHA512};
  
     // preferred compression algorithms
     String[] compressions = new String[]
                              {CompressionAlgorithm.ZIP,
                              CompressionAlgorithm.ZLIB,
                              CompressionAlgorithm.UNCOMPRESSED};
  
     // preferred symmetric key algorithms
     String[] cyphers = new String[] 
                        {CypherAlgorithm.AES_128,
                        CypherAlgorithm.AES_192,
                        CypherAlgorithm.AES_256};
  
     String privateKeyPassword = "changeit";
  
          // EC curve for this key
     String curve = EcCurve.P384;

     PGPKeyPair key = PGPKeyPair.generateEccKeyPair(curve, 
                                               userId, 
                                               privateKeyPassword, 
                                               compressions, 
                                               hashingAlgorithms, 
                                               cyphers,
                                               keyExpiresAfter);
                                               
     // we can call key.export ... in order to save the generated key in a file                                                 
  }
 }
 

generateEccKeyPair

public static PGPKeyPair generateEccKeyPair(java.lang.String ecCurve,
                                            java.lang.String userId,
                                            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 with Elliptic Curve cryptography (ECC) asymmetric encryption algorithm

Parameters:
ecCurve - Elliptic curve of the key pair (see EcCurve for supported curves)
userId - user Id of this key
password - password for the private key (can be empty string if none)
compressionTypes - Compression algorithms supported by the key. (see CompressionAlgorithm)
hashingAlgorithmTypes - Hashing algorithms supported by the key. (see HashAlgorithm)
cipherTypes - Symmetric algorithms supported by the key. (see CypherAlgorithm)
expirationAfterDays - the key validity period in days
Returns:
a PGPKeyPair object representing the newly created key pair
Throws:
PGPException - if an error occurs
java.lang.IllegalArgumentException - if the curve parameter or one of the preferred algorithms parameters is invalid

Example usage:
 import com.didisoft.pgp.*;
 
 public class GenerateECCKeyPair {
  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.SHA256,
                                HashAlgorithm.SHA384,
                                HashAlgorithm.SHA512};
  
     // preferred compression algorithms
     String[] compressions = new String[]
                              {CompressionAlgorithm.ZIP,
                              CompressionAlgorithm.ZLIB,
                              CompressionAlgorithm.UNCOMPRESSED};
  
     // preferred symmetric key algorithms
     String[] cyphers = new String[] 
                        {CypherAlgorithm.AES_128,
                        CypherAlgorithm.AES_192,
                        CypherAlgorithm.AES_256};
  
     String privateKeyPassword = "changeit";
  
     // the key will be valid for 1 year
     long keyExpiresAfter = 365; 
  
          // EC curve for this key
     String curve = EcCurve.P384;

     PGPKeyPair key = PGPKeyPair.generateEccKeyPair(curve, 
                                               userId, 
                                               privateKeyPassword, 
                                               compressions, 
                                               hashingAlgorithms, 
                                               cyphers,
                                               keyExpiresAfter);
                                               
     // we can call key.export ... in order to save the generated key in a file                                                 
  }
 }
 

generateRsaKeyPair

public static PGPKeyPair generateRsaKeyPair(int keySize,
                                            java.lang.String userId,
                                            java.lang.String password)
                                     throws PGPException
Generates OpenPGP Key pair with RSA encryption and predefined values for compression, hashing and cypher like the ones generated with PGP Desktop(tm) key generation wizard.

Note: for key size larger than 2048 key generation will take a few moments.

Default algorithms for the key are:
Symmetric cipher: CAST-5, TRIPLE DES, AES-256, AES-192, AES-128, TWOFISH
Hash: SHA-256, SHA-384, SHA-521, SHA-1, MD-5
Compression: ZIP, Uncompressed, ZLIB, BZIP2


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";
         
     String privateKeyPassword = "changeit";
  
     int keySizeInBits = 2048;
     PGPKeyPair key = PGPKeyPair.generateRsaKeyPair(keySizeInBits, 
                                               userId, 
                                               privateKeyPassword); 
                                               
     // 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>"
password - Secret key password.
Returns:
The generated key pair object
Throws:
org.bouncycastle.openpgp.PGPException - Key generation error
PGPException

generateElGamalKeyPair

public static PGPKeyPair generateElGamalKeyPair(int keySize,
                                                java.lang.String userId,
                                                java.lang.String password)
                                         throws PGPException
Generates OpenPGP Key pair with ElGamal (DH/DSS) encryption and predefined values for compression, hashing and cypher like the ones generated with PGP Desktop(tm) key generation wizard.

Note: for key size larger than 2048 key generation will take a few moments.

Default algorithms for the key are:
Symmetric cipher: CAST-5, TRIPLE DES, AES-256, AES-192, AES-128, TWOFISH
Hash: SHA-256, SHA-384, SHA-521, SHA-1, MD-5
Compression: ZIP, Uncompressed, ZLIB, BZIP2


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";
         
     String privateKeyPassword = "changeit";
  
     int keySizeInBits = 2048;
     PGPKeyPair key = PGPKeyPair.generateElGamalKeyPair(keySizeInBits, 
                                               userId, 
                                               privateKeyPassword); 
                                               
     // 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>"
password - Secret key password.
Returns:
The generated key pair object
Throws:
org.bouncycastle.openpgp.PGPException - Key generation error
PGPException

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)
com.didisoft.PGPException - general error
PGPException


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