コード例 #1
0
  public static ExtendedKeyStore readKeyRing(String filename) {

    ExtendedKeyStore ring = null;

    try {

      FileInputStream in = new FileInputStream(filename);

      ring = (ExtendedKeyStore) ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
      ring.load(in, null);

      in.close();

    } catch (IOException ioe) {
      System.err.println(
          "IOException... You did remember to run the "
              + "GenerateAndWriteKey example first, right?");
      ioe.printStackTrace();
      System.exit(-1);
    } catch (NoSuchAlgorithmException nsae) {
      System.err.println(
          "Cannot find the OpenPGP KeyRing. "
              + "This usually means that the Cryptix OpenPGP provider is not "
              + "installed correctly.");
      nsae.printStackTrace();
      System.exit(-1);
    } catch (KeyStoreException kse) {
      System.err.println("Reading keyring failed.");
      kse.printStackTrace();
      System.exit(-1);
    } catch (CertificateException ce) {
      System.err.println("Reading keyring failed.");
      ce.printStackTrace();
      System.exit(-1);
    }

    return ring;
  }
コード例 #2
0
  public static void writeMsg() {
    Message msg = null;

    try {
      String data = "This is a test message.\n" + "This is another line.\n";
      LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP");
      lmb.init(data);
      msg = lmb.build();
    } catch (NoSuchAlgorithmException nsae) {
      System.err.println(
          "Cannot find the OpenPGP LiteralMessageBuilder."
              + " This usually means that the Cryptix OpenPGP provider is not "
              + "installed correctly.");
      nsae.printStackTrace();
      System.exit(-1);
    } catch (MessageException me) {
      System.err.println("Creating the literal message failed.");
      me.printStackTrace();
      System.exit(-1);
    }

    // **********************************************************************
    // Sign the message.
    //
    // Note that signing usually comes before encryption, such that
    // unauthorized parties cannot see who signed the message.
    // **********************************************************************
    try {

      SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP");

      // use the following line for compatibility with older PGP versions

      // SignedMessageBuilder smb =
      //     SignedMessageBuilder.getInstance("OpenPGP/V3");

      smb.init(msg);
      smb.addSigner(clientPrivateKey, "TestingPassphrase".toCharArray());

      msg = smb.build();

    } catch (NoSuchAlgorithmException nsae) {
      System.err.println(
          "Cannot find the OpenPGP SignedMessageBuilder. "
              + "This usually means that the Cryptix OpenPGP provider is not "
              + "installed correctly.");
      nsae.printStackTrace();
      System.exit(-1);
    } catch (UnrecoverableKeyException uke) {
      System.err.println("Incorrect passphrase.");
      uke.printStackTrace();
      System.exit(-1);
    } catch (MessageException me) {
      System.err.println("Generating the message failed.");
      me.printStackTrace();
      System.exit(-1);
    }

    // **********************************************************************
    // Armour the message and write it to disk
    // **********************************************************************
    try {

      PGPArmouredMessage armoured;

      armoured = new PGPArmouredMessage(msg);
      FileOutputStream out = new FileOutputStream("signed.asc");
      out.write(armoured.getEncoded());
      out.close();

    } catch (MessageException me) {
      System.err.println("Writing the encrypted message failed.");
      me.printStackTrace();
      System.exit(-1);
    } catch (IOException ioe) {
      System.err.println("Writing the encrypted message failed.");
      ioe.printStackTrace();
      System.exit(-1);
    }

    // **********************************************************************
    // Encrypt the message.
    // **********************************************************************
    try {

      EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP");
      emb.init(msg);
      emb.addRecipient(serverPublicKey);
      msg = emb.build();

    } catch (NoSuchAlgorithmException nsae) {
      System.err.println(
          "Cannot find the OpenPGP "
              + "EncryptedMessageBuilder. "
              + "This usually means that the Cryptix OpenPGP provider is not "
              + "installed correctly.");
      nsae.printStackTrace();
      System.exit(-1);
    } catch (MessageException me) {
      System.err.println("Creating the encrypted message failed.");
      me.printStackTrace();
      System.exit(-1);
    }

    // **********************************************************************
    // Armour the message and write it to disk
    // **********************************************************************
    try {

      PGPArmouredMessage armoured;

      armoured = new PGPArmouredMessage(msg);
      FileOutputStream out = new FileOutputStream("encrypted-signed.asc");
      out.write(armoured.getEncoded());
      out.close();

    } catch (MessageException me) {
      System.err.println("Writing the encrypted message failed.");
      me.printStackTrace();
      System.exit(-1);
    } catch (IOException ioe) {
      System.err.println("Writing the encrypted message failed.");
      ioe.printStackTrace();
      System.exit(-1);
    }
  }