Import keys

Examples ›› Programs ››
Parent Previous Next

This example demonstrates how to import existing keys from standalone files into a KeyStore object.


import java.io.IOException;

import com.didisoft.pgp.*;

public class ImportKeys {

public static void main(String[] args) {

 // initialize the KeyStore. The key store file may not exist

 // and subsequent operations will create it

 KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");

 try {

   // import private key

   keyStore.importPrivateKey("private.asc");

   // import public key

   keyStore.importPublicKey("public.asc");

   // imports key ring file. The file may contain public, private or

   // both type of keys if it is in ASCII armored format

   keyStore.importKeyRing("keypair.asc");        

 } catch (PGPException e) {

   System.out.println("Error reading key files : " + e.getMessage());

 } catch (IOException e) {

   System.out.println(e.getMessage());

 }

}

}