com.didisoft.pgp
Class KeyStore

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

public class KeyStore
extends java.lang.Object
implements java.io.Serializable

Represents an OpenPGP Key Store.

The KeyStore file contains both the public and secret key rings combined in one file and protected with password.
' (In PGP (r) and GnuPG they are stored in two different files)

It can be used with PGPLib in cases that require KeyStore parameter, or alone when only KeyStore or key generation operations are required.

See Also:
Serialized Form

Field Summary
static java.lang.String DSA
          DSA Algorithm constant, implementation of DH/DSS (signature)
static java.lang.String ELGAMAL
          ElGamal Algorithm constant, implementation of DH/DSS (encryption)
static java.lang.String RSA
          RSA Algorithm constant
 
Constructor Summary
KeyStore(java.lang.String keystoreFileName, java.lang.String keystorePassword)
          Creates a new Key Store object.
 
Method Summary
 void addCertification(long keyId, long certKeyId, java.lang.String certPassword, java.lang.String userID)
          Appends certification to a given OpenPGP key in the key store.
 void changePrivateKeyPassword(long keyId, java.lang.String oldPassword, java.lang.String newPassword)
          Changes private key password.
 boolean changePrivateKeyPassword(java.lang.String userId, java.lang.String oldPassword, java.lang.String newPassword)
          Changes private key password.
 boolean containsKey(long keyId)
          Returns true if a public or private key with the specified Key Id exists in this KeyStore

Example:
 boolean containsKey(java.lang.String userId)
          Returns true if public or private key with the specified User ID exists in this KeyStore

Example:
 boolean containsPrivateKey(long keyId)
          Returns true if a private key with the specified Key Id exists in this KeyStore

Example:
 boolean containsPrivateKey(java.lang.String userId)
          Returns true if a private key with the specified User ID exists in this KeyStore

Example:
 boolean containsPublicKey(long keyId)
          Returns true if a public key with the specified Key Id exists in this KeyStore

Example:
 boolean containsPublicKey(java.lang.String userId)
          Returns true if a public key with the specified User ID exists in this KeyStore

Example:
 void deleteKeyPair(long keyId)
          Deletes public and private key pair from this key store that have the specified Key Id.
 boolean deleteKeyPair(java.lang.String userId)
          Deletes the first public and private key pair from this key store that have the specified User Id.
 boolean deletePrivateKey(long keyId)
          Deletes the first private key with the specified User Id from this KeyStore.
 boolean deletePrivateKey(java.lang.String userId)
          Deletes the first private key with the specified User Id from this KeyStore.
 boolean deletePublicKey(long keyId)
          Deletes the first public key with the specified Key Id from this KeyStore.
 boolean deletePublicKey(java.lang.String userId)
          Deletes the first public key with the specified User Id from this KeyStore.
 void exportKeyRing(java.lang.String fileName, java.lang.String userId)
          Exports Key ring (both public and private key) located in this Key store in one file.
 void exportPrivateKey(java.lang.String fileName, long keyId, boolean asciiArmored)
          Exports private key from this KeyStore instance, into a file.
 void exportPrivateKey(java.lang.String fileName, java.lang.String userId, boolean asciiArmored)
          Exports private key from this OpenPGP key store, into a file.
 void exportPublicKey(java.lang.String fileName, long keyId, boolean asciiArmored)
          Exports public key from this KeyStore instance into a file.
 void exportPublicKey(java.lang.String fileName, java.lang.String userId, boolean asciiArmored)
          Exports public key from this KeyStore instance into a file.
 void generateKeyPair(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.
 void 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)
          Generates OpenPGP key pair (public and private key).
 void 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)
          Generates OpenPGP Key pair (public and private key).
 void 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 OpenPGP Key pair (public and private key).
 java.lang.String[] getKeyHexIds()
          Returns array of Hexadecimal string representation of the key Id's for the keys contained in this KeyStore instance.
 long getKeyIdForKeyIdHex(java.lang.String keyIdHex)
          Returns the real Key ID (of type long) corresponding to this Hexadecimal Key Id.
 long getKeyIdForUserId(java.lang.String userId)
          Returns the real Key ID (of type long) corresponding to this User Id.
 KeyPairInformation[] getKeys()
          Returns the key pairs stored in this OpenPGP key store.
 java.lang.String[] getUserIds()
          Returns array of Strings representing all user Id's contained in this KeyStore instance.
 void importKeyRing(java.lang.String keyRingFileName)
          Imports OpenPGP key ring into this KeyStore instance.
 void importPrivateKey(java.lang.String privateKeyFileName)
          Imports a private key file into this KeyStore instance.
 void importPrivateKey(java.lang.String privateKeyFileName, java.lang.String password)
          Imports private key file into this KeyStore instance.
 void importPublicKey(java.lang.String publicKeyFileName)
          Imports Public key into this KeyStore instance.
 void importPublickKey(java.lang.String publicKeyFileName)
          Deprecated. Replaced by importPublicKey(String)
 boolean isAutoSave()
          Returns is auto save on.
 boolean isBackupOnSave()
          Returns should backups be made on save.
 void listKeys()
          Lists keys stored in this OpenPGP key store to Stestem.out like PGP (r) 6.5

Example:
 void save()
          Saves the backing file of this KeyStore instance to disk.
 void setAutoSave(boolean save)
          Sets the KeyStore to call save() automatically after each operation that modifies it (for example import key, delete key, etc.)
 void setBackupOnSave(boolean backupOnSave)
          Sets backup file be created (with extension .bak) for this KeyStore on save() operations.
 void signPublicKey(long keyId, long signKeyId, java.lang.String signPassword)
          Signs a public key with a private key.
 void signPublicKey(java.lang.String keyUserId, java.lang.String signKeyUserId, java.lang.String signPassword)
          Signs a public key with a private key.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ELGAMAL

public static final java.lang.String ELGAMAL
ElGamal Algorithm constant, implementation of DH/DSS (encryption)

See Also:
Constant Field Values

DSA

public static final java.lang.String DSA
DSA Algorithm constant, implementation of DH/DSS (signature)

See Also:
Constant Field Values

RSA

public static final java.lang.String RSA
RSA Algorithm constant

See Also:
Constant Field Values
Constructor Detail

KeyStore

public KeyStore(java.lang.String keystoreFileName,
                java.lang.String keystorePassword)
         throws java.io.IOException,
                PGPException
Creates a new Key Store object.
NOTE: If the key store file does not exist, it will be created on first key manipulation operation.

Parameters:
keystoreFileName - File name of the Key store (absolute or relative path)
keystorePassword - Password for the Key store.
Throws:
java.io.IOException - if an I/O error occurs
org.bouncycastle.openpgp.PGPException - if PGP object different from public or private key ring is found in this file
PGPException
Method Detail

getUserIds

public java.lang.String[] getUserIds()
Returns array of Strings representing all user Id's contained in this KeyStore instance.

Returns:
array of Strings representing all user Id's contained in this KeyStore instance

getKeyHexIds

public java.lang.String[] getKeyHexIds()
Returns array of Hexadecimal string representation of the key Id's for the keys contained in this KeyStore instance.

This hexadecimal value is composed of the lower four bytes of the real key Id (8 bytes) and is the same as the one displayed by PGP (r) and GnuPG.

Returns:
array of Hexadecimal string representation of the key Id's for the keys contained in this KeyStore instance

getKeyIdForUserId

public long getKeyIdForUserId(java.lang.String userId)
Returns the real Key ID (of type long) corresponding to this User Id.

Parameters:
userId - User Id of the key owner.
Returns:
the real Key ID (of type long) corresponding to this User Id. If there is no key with such User Id -1 is returned.

getKeyIdForKeyIdHex

public long getKeyIdForKeyIdHex(java.lang.String keyIdHex)
Returns the real Key ID (of type long) corresponding to this Hexadecimal Key Id.

This example shows how to get the real key Id of type long for given hex key.
 import com.didisoft.pgp.KeyStore;
 
 public class KeyIdHexDemo {
  public static void main(String[] args) throws Exception{
    // initialize a KeyStore instance
    KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
    
    // get the key id corresponding to this hexadecimal key id 
    String hexKeyId = "74BB3286";               
    long keyId = keyStore.getKeyIdForKeyIdHex(hexKeyId);
    if (keyId == -1) {
      System.out.println("No key found with Key Id " + hexKeyId);
      return;
    }
    
    // ... Now we can use keyId in the PGPLib methods 
    // that accept KeyStore object and key id.   
  }
 }

Parameters:
keyIdHex - Hexadecimal Key Id used by other OpenPG software (e.g. PGP (r) and GnuPG).
Returns:
the real Key ID (of type long) corresponding to this Hexadecimal Key Id. If there is no key with such Hexadecimal Key Id -1 is returned.

addCertification

public void addCertification(long keyId,
                             long certKeyId,
                             java.lang.String certPassword,
                             java.lang.String userID)
                      throws PGPException
Appends certification to a given OpenPGP key in the key store.

Parameters:
keyId - Key ID of the key to receive this certification
certKeyId - certifying key ID
certPassword - certifying key password
userID - user id giving the certification
Throws:
NoPublicKeyFoundException - No public key has been found with such Key Id
NoPrivateKeyFoundException - No private key has been found with such Key Id
org.bouncycastle.openpgp.PGPException - OpenPGP signing error
PGPException

signPublicKey

public void signPublicKey(long keyId,
                          long signKeyId,
                          java.lang.String signPassword)
                   throws PGPException
Signs a public key with a private key.

When you import keys into your KeyStore and are sure that the trusted third party is really the person they claim to be, you can sign his/her keys. Signing a key certifies that you know the owner of the keys.

This is equivalent to the GnuPG command:
gpg --local-user certKeyId --sign-key keyId

Example usage:
 import com.didisoft.pgp.*;
 
 public class SignKeyDemo { 
  public static void main(String[] a) throws Exception {
      KeyStore keyStore = new KeyStore("mykeys.keystore", "password");
  
      long partnerKeyId = keyStore.getKeyIdForKeyIdHex("2EDE06AC");
      long mySecretKeyId = keyStore.getKeyIdForKeyIdHex("2EDE06AC");
      
      keyStore.signPublicKey(partnerKeyId, mySecretKeyId, "my secret key password");
  }
 } 
 

Parameters:
keyId - Key ID of the public key to be signed
signKeyId - Key ID of the private key to sign the public key with
signPassword - Password of the own private key
Throws:
NoPublicKeyFoundException - No public key has been found with such Key Id
NoPrivateKeyFoundException - No private key has been found with such Key Id
org.bouncycastle.openpgp.PGPException - OpenPGP signing error
PGPException

signPublicKey

public void signPublicKey(java.lang.String keyUserId,
                          java.lang.String signKeyUserId,
                          java.lang.String signPassword)
                   throws PGPException
Signs a public key with a private key.

When you import keys into your KeyStore and are sure that trusted third party is really the person they claim, you can sign his/her keys. Signing a key certifies that you know the owner of the keys.

This is equivalent to the GnuPG command:
gpg --local-user signKeyUserId --sign-key keyUserId

Example usage:
 import com.didisoft.pgp.*;
 
 public class SignKeyDemo { 
  public static void main(String[] a) throws Exception {
      KeyStore keyStore = new KeyStore("mykeys.keystore", "password");
  
      String partnerUserId = "Partner Company ACM";
      long mySecretKeyUserId = "My Company my@company.com";
      
      keyStore.signPublicKey(partnerUserId, mySecretKeyUserId, "my secret key password");
  }
 } 
 

Parameters:
keyUserId - User Id of the public key to be signed
signKeyUserId - User Id of the private key to sign the public key with
signPassword - Password of the own private key
Throws:
org.bouncycastle.openpgp.PGPException
PGPException

deleteKeyPair

public boolean deleteKeyPair(java.lang.String userId)
                      throws PGPException
Deletes the first public and private key pair from this key store that have the specified User Id.
If more than one key pair exist with the supplied User Id, only the first one is affected.
If no key exists with the specified userId, no changes to the key store occur.
This method is equivalent to the GnuPG commands:
gpg --delete-secret-key [userId]
gpg --delete-key [userId]

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class DeleteKeyPair {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // delete the key pair with this User Id
   ks.deleteKeyPair("test@gmail.com");
  }
 }
 

Parameters:
userId - User Id of the form "name (comment) <email address>"
Returns:
true if key pair is deleted, false if there is no key pair with the specified User Id
Throws:
PGPException

deletePrivateKey

public boolean deletePrivateKey(java.lang.String userId)
                         throws PGPException
Deletes the first private key with the specified User Id from this KeyStore.
If more than one key pair exist with the supplied User Id, only the first one is affected.
If no key exists, no changes are made to the KeyStore occur.
This method is equivalent to the GnuPG commands:
gpg --delete-secret-key [userId]

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class DeletePrivateKeyDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // delete the private key with this User Id
   ks.deletePrivateKey("test@gmail.com");
  }
 }
 

Parameters:
userId - User Id of the form "name (comment) <email address>"
Returns:
true if a key is deleted, false if there is no key with the specified User Id
Throws:
PGPException

deletePrivateKey

public boolean deletePrivateKey(long keyId)
                         throws PGPException
Deletes the first private key with the specified User Id from this KeyStore.
If more than one private key exist with the supplied User Id, only the first one is affected.
If no key exists, no changes are made to the KeyStore occur.
This method is equivalent to the GnuPG commands:
gpg --delete-secret-key [userId]

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class DeletePrivateKeyDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // delete the private key with this Key Id
   long privateKeyId = ks.getKeyIdForKeyIdHex("2EDE06AC");
   ks.deletePrivateKey(privateKeyId);
  }
 }
 

Parameters:
keyId - Key Id of the private key to be deleted
Returns:
true if a key is deleted, false if there is no key with the specified User Id
Throws:
PGPException

deletePublicKey

public boolean deletePublicKey(java.lang.String userId)
                        throws PGPException
Deletes the first public key with the specified User Id from this KeyStore.
If more than one public key exist with the supplied User Id, only the first one is affected.
If no key exists, no changes are made to the KeyStore.
This method is equivalent to the GnuPG commands:
gpg --delete-key [userId]

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class DeletePublicKeyDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // delete the public key with this User Id
   ks.deletePublicKey("test@gmail.com");
  }
 }
 

Parameters:
userId - User Id of the form "name (comment) <email address>"
Returns:
true if a key is deleted, false if there is no key with the specified User Id
Throws:
PGPException

deletePublicKey

public boolean deletePublicKey(long keyId)
                        throws PGPException
Deletes the first public key with the specified Key Id from this KeyStore.
If more than one key exist with the supplied Key Id, only the first one is affected.
If no key exists, no changes are made to the KeyStore.
This method is equivalent to the GnuPG commands:
gpg --delete-key [keyId]

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class DeletePublicKeyDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // delete the public key with this Key Id
   long privateKeyId = ks.getKeyIdForKeyIdHex("2EDE06AC");
   ks.deletePublicKey(privateKeyId);
  }
 }
 

Parameters:
keyId - Key Id of the public key to be deleted
Returns:
true if a key is deleted, false if there is no key with the specified User Id
Throws:
PGPException

deleteKeyPair

public void deleteKeyPair(long keyId)
                   throws PGPException
Deletes public and private key pair from this key store that have the specified Key Id.

This method is equivalent to the GnuPG commands:
gpg --delete-secret-key [keyId]
gpg --delete-key [keyId]

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class DeleteKeyPairDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // delete the key pair with this Key Id
   long keyId = ks.getKeyIdForKeyIdHex("2EDE06AC");
   ks.deleteKeyPair(keyId);
  }
 }
 

Parameters:
keyId - Key ID of the key pair
Throws:
PGPException

changePrivateKeyPassword

public boolean changePrivateKeyPassword(java.lang.String userId,
                                        java.lang.String oldPassword,
                                        java.lang.String newPassword)
                                 throws WrongPasswordException,
                                        PGPException
Changes private key password.
If more than one key pairs exist with the supplied userId, only the first one is affected.

This method is equivalent to the GnuPG commands:
gpg --edit-key [userId]
Command>passwd

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class ChangePrivateKeyPasswordDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // change the key password
   ks.changePrivateKeyPassword("My Company", "old password", "new password");
  }
 }
 

Parameters:
userId - private key User Id of the form "name (comment) <email address>"
oldPassword - current password of the private key
newPassword - new password of the private key
Returns:
true if private key password is changed, false if there is no key with the specified userId
Throws:
WrongPasswordException - if the old password is incorrect
org.bouncycastle.openpgp.PGPException - general error
PGPException

changePrivateKeyPassword

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

This method is equivalent to the GnuPG commands:
gpg --edit-key [keyId]
Command>passwd

Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class ChangePrivateKeyPasswordDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // change the key password
   long keyId = ks.getKeyIdForKeyIdHex("2EDE06AC");
   ks.changePrivateKeyPassword(keyId, "old password", "new password");
  }
 }
 

Parameters:
keyId - Key Id of the private key
oldPassword - current password of the private key
newPassword - new password of the private key
Throws:
NoPrivateKeyFoundException - no private key with the specified Key Id has been found
WrongPasswordException - if the old password is incorrect
org.bouncycastle.openpgp.PGPException - general error
PGPException
See Also:
getKeyIdForKeyIdHex(String)

generateKeyPair

public void generateKeyPair(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.

Parameters:
keySize - Size of the keys
minimum key size is 512 bits
highest suggested key size is 4096 bits


Example usage:
 import com.didisoft.pgp.KeyStore;
 
 public class GenerateRSAKeyPairDemo {
  public static void main(String[] args) throws Exception{
   // initialize the KeyStore instance
   KeyStore ks = new KeyStore("pgp.keystore", "changeit");
 
   // change the key password
   int keySize = 2048;
   String userId = "My Company";
   ks.generateKeyPair(keyId, userId, "key password");
   
   // the public key can now be exported and sent to our partners
  }
 }
 
userId - User Id of the form "name (comment) <email address>"
password - Secret key password
Throws:
org.bouncycastle.openpgp.PGPException - Key generation error
PGPException

generateKeyPair

public void 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)
                     throws PGPException
Generates OpenPGP key pair (public and private key).
The newly generated key is stored in this KeyStore instance.
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 {
     // initialize the KeyStore where the key will be generated
     KeyStore ks = new KeyStore("pgp.keystore", "changeit");
         
     // 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";
  
     int keySizeInBits = 2048;
     ks.generateKeyPair(keySizeInBits, 
                      userId, 
                      KeyAlgorithm.RSA, 
                      privateKeyPassword, 
                      compressions, 
                      hashingAlgorithms, 
                      cyphers);       
  }
 }
 

Parameters:
keySize - Size of the keys in bits
minimum key size is 512
highest suggested key size is 4096 bits for RSA and 4096 for ELGAMAL
userId - User Id of the form "name (comment) <email address>"
keyAlgorithm - Key algorithm. Possible values: ELGAMAL, RSA
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
Throws:
org.bouncycastle.openpgp.PGPException - Key generation error CompressionAlgorithm
PGPException

generateKeyPair

public void 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)
                     throws PGPException
Generates OpenPGP Key pair (public and private key).
The newly generated key is stored in this KeyStore instance.
Note: for key size larger than 2048 bits key generation will take a few moments.

Parameters:
keySize - Size of the keys in bits
minimum key size is 512
highest suggested key size is 4096 bits for RSA and 4096 for ELGAMAL
userId - User Id of the form "name (comment) <email address>"
keyAlgorithm - Key algorithm. Possible values: ELGAMAL, RSA
password - Secret key password.
compressionTypes - Compression algorithms supported by the key.
Comma separated list of one or more of: ZLIB, ZIP, UNCOMPRESSED, BZIP2
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
Throws:
org.bouncycastle.openpgp.PGPException - Key generation error
PGPException

generateKeyPair

public void 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 OpenPGP Key pair (public and private key).
The newly generated key is stored in this KeyStore instance.
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 {
     // initialize the KeyStore where the key will be generated
     KeyStore ks = new KeyStore("pgp.keystore", "changeit");
         
     // 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;
     ks.generateKeyPair(keySizeInBits, 
                      userId, 
                      KeyAlgorithm.RSA, 
                      privateKeyPassword, 
                      compressions, 
                      hashingAlgorithms, 
                      cyphers,
                      keyExpiresAfter);       
  }
 }
 

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.
Comma separated list of one or more of: ZLIB, ZIP, UNCOMPRESSED, BZIP2
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, IDEA, SAFER
expirationAfterDays - Number of days the key will be valid. For example 365 for one year. Use 0 (zero) for no expiration date.
Throws:
org.bouncycastle.openpgp.PGPException - Key generation error
PGPException

exportKeyRing

public void exportKeyRing(java.lang.String fileName,
                          java.lang.String userId)
                   throws NoPublicKeyFoundException,
                          java.io.IOException
Exports Key ring (both public and private key) located in this Key store in one file.
The result file is in ASCII armored format. The private key is exported only if exists.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ExportKeyPairDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // export key pair with this User Id
         keyStore.exportKeyRing("keypair.asc", "demo@didisoft.com");     
     }
 }
 

Parameters:
fileName - File name where the key ring will be exported (absolute or relative path)
userId - User Id of the form "name (comment) <email address>"
Throws:
NoPublicKeyFoundException - if there is no such key
java.io.IOException - I/O error saving the key ring

exportPublicKey

public void exportPublicKey(java.lang.String fileName,
                            java.lang.String userId,
                            boolean asciiArmored)
                     throws PGPException,
                            java.io.IOException
Exports public key from this KeyStore instance into a file.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ExportPublicKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // should the exported key be in ASCII form (true), or binary (false)
         boolean asciiArmored = true;
         
         // export the public key with this User Id
         keyStore.exportPublicKey("public_key.asc", "demo@didisoft.com", asciiArmored);     
     }
 }
 

Parameters:
fileName - File name where the public key will be exported (absolute or relative path)
userId - User Id of the form "name (comment) <email address>"
asciiArmored - if true, out is ASCII armored, when false binary file format is used.
Throws:
org.bouncycastle.openpgp.PGPException - if there is no such key
java.io.IOException - if an I/O error occurs.
PGPException

exportPublicKey

public void exportPublicKey(java.lang.String fileName,
                            long keyId,
                            boolean asciiArmored)
                     throws NoPublicKeyFoundException,
                            java.io.IOException
Exports public key from this KeyStore instance into a file.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ExportPublicKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // should the exported key be in ASCII form (true), or binary (false)
         boolean asciiArmored = true;
         
         long keyId = keyStore.getKeyIdForKeyIdHex("2EDE06AC");
         // export key pair with this User Id
         keyStore.exportPublicKey("public_key.asc", keyId, asciiArmored);     
     }
 }
 

Parameters:
fileName - File name where the public key will be exported (absolute or relative path)
keyId - Key Id of the Public Key
asciiArmored - if true, out is ASCII armored
Throws:
NoPublicKeyFoundException - if there is no such key
java.io.IOException - if an I/O error occurs.

exportPrivateKey

public void exportPrivateKey(java.lang.String fileName,
                             java.lang.String userId,
                             boolean asciiArmored)
                      throws NoPrivateKeyFoundException,
                             java.io.IOException
Exports private key from this OpenPGP key store, into a file.
If more than one key pairs exist with the supplied userId, only the first one is exported.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ExportPrivateKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // should the exported key be in ASCII form (true), or binary (false)
         boolean asciiArmored = true;
         
         // export key pair with this User Id
         keyStore.exportPrivateKey("private_key.asc", "demo@didisoft.com", asciiArmored);     
     }
 }
 

Parameters:
fileName - File name where the key will be exported (absolute or relative path)
userId - User Id of the form "name (comment) <email address>"
asciiArmored - if true, out is ASCII armored
Throws:
NoPrivateKeyFoundException - if there is no such key
java.io.IOException - if an I/O error occurs.

exportPrivateKey

public void exportPrivateKey(java.lang.String fileName,
                             long keyId,
                             boolean asciiArmored)
                      throws NoPrivateKeyFoundException,
                             java.io.IOException
Exports private key from this KeyStore instance, into a file.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ExportPrivateKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // should the exported key be in ASCII form (true), or binary (false)
         boolean asciiArmored = true;
         
         long keyId = keyStore.getKeyIdForKeyIdHex("2EDE06AC");
         // export key pair with this User Id
         keyStore.exportPrivateKey("private_key.asc", keyId, asciiArmored);     
     }
 }
 

Parameters:
fileName - File name where the key will be exported (absolute or relative path)
keyId - Key Id of the corresponding Public Key
asciiArmored - if true, out is ASCII armored
Throws:
NoPrivateKeyFoundException - if there is no such key
java.io.IOException - if an I/O error occurs.

importPublickKey

public void importPublickKey(java.lang.String publicKeyFileName)
                      throws java.io.IOException,
                             PGPException
Deprecated. Replaced by importPublicKey(String)

Imports Public key into this Key store instance. If the key store does not exist, it is created.

Parameters:
publicKeyFileName - FileName of the Public key to be imported (absolute or relative path)
Throws:
java.io.IOException - if an I/O error occurs (for example file not found)
org.bouncycastle.openpgp.PGPException - if PGP object different from public or private key is found in this file
PGPException
See Also:
importKeyRing(String), exportKeyRing(String, String)

importPublicKey

public void importPublicKey(java.lang.String publicKeyFileName)
                     throws java.io.IOException,
                            PGPException
Imports Public key into this KeyStore instance. If the KeyStore file does not exit, it is created.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ImportKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // import key 
         keyStore.importPublicKey("public_key.asc");     
     }
 }
 

Parameters:
publicKeyFileName - FileName of the Public key to be imported (absolute or relative path)
Throws:
java.io.IOException - if an I/O error occurs (for example file not found)
org.bouncycastle.openpgp.PGPException - if PGP object different from public or private key is found in this file
PGPException

importKeyRing

public void importKeyRing(java.lang.String keyRingFileName)
                   throws java.io.IOException,
                          PGPException
Imports OpenPGP key ring into this KeyStore instance. If the KeyStore file does not exist, it is created.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ImportKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // import key 
         keyStore.importKeyRing("mykey.asc");     
     }
 }
 

Parameters:
keyRingFileName - FileName of the key ring to be imported (absolute or relative path). Most common file name extensions are .asc, .pkr, .skr, .pgp, .pgpkey
Throws:
java.io.IOException - if an I/O error occurs (for example file not found)
org.bouncycastle.openpgp.PGPException - if PGP object different from public or private key is found in this file
PGPException
See Also:
exportKeyRing(String, String)

importPrivateKey

public void importPrivateKey(java.lang.String privateKeyFileName)
                      throws java.io.IOException,
                             PGPException
Imports a private key file into this KeyStore instance. If the KeyStore file does not exist, it is created.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ImportKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // import key 
         keyStore.importPrivateKey("mykey.asc");     
     }
 }
 

Parameters:
privateKeyFileName - File of the Private key to be imported (absolute or relative path)
Throws:
java.io.IOException - if an I/O error occurs (for example file not found)
org.bouncycastle.openpgp.PGPException - if PGP object different from public or private key is found in this file
PGPException

importPrivateKey

public void importPrivateKey(java.lang.String privateKeyFileName,
                             java.lang.String password)
                      throws java.io.IOException,
                             PGPException
Imports private key file into this KeyStore instance. If the KeyStore file does not exit, it is created.

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ImportKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         // import key 
         keyStore.importPrivateKey("mykey.asc", "key password");     
     }
 }
 

Parameters:
privateKeyFileName - File of the Private key to be imported (absolute or relative path)
password - Password of the private key
Throws:
java.io.IOException - if an I/O error occurs (for example file not found)
org.bouncycastle.openpgp.PGPException - if PGP object different from public or private key is found in this file
PGPException

listKeys

public void listKeys()
Lists keys stored in this OpenPGP key store to Stestem.out like PGP (r) 6.5

Example:
 import com.didisoft.pgp.KeyStore;
 import com.didisoft.pgp.PGPLib;
 
 public class ListKeys {
  public static void main(String[] args) throws Exception{
    KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");               
    keyStore.listKeys();
  }
 }
 

See Also:
getKeys()

getKeys

public KeyPairInformation[] getKeys()
Returns the key pairs stored in this OpenPGP key store.

Returns:
Array of KeyPairInformation objects holding the key rings contained in this key store.

containsPublicKey

public boolean containsPublicKey(java.lang.String userId)
Returns true if a public key with the specified User ID exists in this KeyStore

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ContainsPublicKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         boolean keyExists = keyStore.containsPublicKey("demo@didisoft.com");     
     }
 }
 

Parameters:
userId - User ID of the form "name (comment) <email address>"
Returns:
true if a public key with the specified User ID exists in this Key Store
See Also:
getKeyIdForUserId(String)

containsPrivateKey

public boolean containsPrivateKey(java.lang.String userId)
Returns true if a private key with the specified User ID exists in this KeyStore

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ContainsPrivateKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         boolean keyExists = keyStore.containsPrivateKey("demo@didisoft.com");     
     }
 }
 

Parameters:
userId - User ID of the form "name (comment) <email address>"
Returns:
true if a public key with the specified User ID exists in this Key Store
See Also:
getKeyIdForUserId(String)

containsKey

public boolean containsKey(java.lang.String userId)
Returns true if public or private key with the specified User ID exists in this KeyStore

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ContainsKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         boolean keyExists = keyStore.containsKey("demo@didisoft.com");     
     }
 }
 

Parameters:
userId - User ID of the form "name (comment) <email address>"
Returns:
true if a public or private key with the specified User ID exists in this Key Store
See Also:
getKeyIdForUserId(String)

containsKey

public boolean containsKey(long keyId)
Returns true if a public or private key with the specified Key Id exists in this KeyStore

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ContainsKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         long keyId = keyStore.getKeyIdForKeyIdHex("2EDE06AC");
         boolean keyExists = keyStore.containsKey(keyId);     
     }
 }
 

Parameters:
keyId - Key Id of type long
Returns:
true if key with the specified Key Id exists in this Key Store
See Also:
getKeyIdForKeyIdHex(String)

containsPrivateKey

public boolean containsPrivateKey(long keyId)
Returns true if a private key with the specified Key Id exists in this KeyStore

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ContainsPrivateKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         long keyId = keyStore.getKeyIdForKeyIdHex("2EDE06AC");
         boolean keyExists = keyStore.containsPrivateKey(keyId);     
     }
 }
 

Parameters:
keyId - Key Id of type long
Returns:
true if a private key with the specified Key Id exists in this Key Store
See Also:
getKeyIdForKeyIdHex(String)

containsPublicKey

public boolean containsPublicKey(long keyId)
Returns true if a public key with the specified Key Id exists in this KeyStore

Example:
 import com.didisoft.pgp.KeyStore;
 
 public class ContainsKeyDemo {
     public static void main(String[] args) throws Exception{
         // initialize the key store
         KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
         
         long keyId = keyStore.getKeyIdForKeyIdHex("2EDE06AC");
         boolean keyExists = keyStore.containsPublicKey(keyId);     
     }
 }
 

Parameters:
keyId - Key Id of type long
Returns:
true if a public key with the specified Key Id exists in this Key Store
See Also:
getKeyIdForKeyIdHex(String)

setAutoSave

public void setAutoSave(boolean save)
Sets the KeyStore to call save() automatically after each operation that modifies it (for example import key, delete key, etc.)

Parameters:
save - if true this KeyStore instance calls automatically it's save() method.
See Also:
save(), isAutoSave()

isAutoSave

public boolean isAutoSave()
Returns is auto save on. (true by default)

Returns:
true if auto save mode is on, otherwise false
See Also:
setAutoSave(boolean), save()

isBackupOnSave

public boolean isBackupOnSave()
Returns should backups be made on save. (true by default)

Returns:
true if backup file is created on save, otherwise false
See Also:
setBackupOnSave(boolean), save()

setBackupOnSave

public void setBackupOnSave(boolean backupOnSave)
Sets backup file be created (with extension .bak) for this KeyStore on save() operations.

Parameters:
backupOnSave - if true a backup file (with extension .bak) is created on each call to the save() method.
See Also:
isBackupOnSave(), save()

save

public void save()
          throws PGPException
Saves the backing file of this KeyStore instance to disk.

If isAutoSave() is true there is no need to be called explicitly, because it is called implicitly by all methods that change the state of this KeyStore instance.

Throws:
org.bouncycastle.openpgp.PGPException - error saving the state of this key store. Use the method getUnderlyingException() to see the cause.
PGPException


Copyright © 2006-2010 DidiSoft Eood All Rights Reserved.