Next: , Previous: , Up: Kawa Scheme   [Contents][Index]


18.6.8.5 Kawa Implementation of Asymmetric Encryption Keys Via KeyPairGenerator

AsymmetricKeyMaker’ is an excellent example for several reasons. It is a command line program takes user input and returns a result. It utilizes the Java exception system and gives us an opportunity to deal with both abstract and concrete classes.

Example of standalone executable program duplicating the functionality of the January 16, 2004 ’Core Java Technologies Tech Tips’ article ’Asymmetric Encryption Keys With the KeyPairGenerator’. This example assumes the reader has obtained their own copy of that document and will reference it and the Java source for our purpose of reproducing the functionality. It is not reproduced here due to the lengthy and confusing amount of copyright and terms of use information Sun uses on all their documentation. The Sun source for the ‘AsymmetricKeyMaker’ program is 25 lines, so we can reference things by line number.

; -*-scheme-*- Copyright © Robert D. Skeels 02/07/2004 <athene@earthlink.net>

(module-name <AsymmetricKeyMaker>)

(define-namespace key-pair-generator "class:java.security.KeyPairGenerator")

(define gen-keys
  (lambda (algorithm)
    (let* ((our-kpg (key-pair-generator:getInstance algorithm))
           (our-key-pair (invoke our-kpg 'generateKeyPair))
           (writeln (lambda (args) (display args) (newline))))
           (writeln (invoke our-key-pair 'getPublic))
           (writeln (invoke our-key-pair 'getPrivate)))))

(define try-gen-keys
  (lambda (algorithm)
    (try-catch (gen-keys algorithm)
        (ex <java.security.NoSuchAlgorithmException>
          (begin
          (display  "usage: java AsymmetricKeyMaker <RSA | DSA>")
          (newline))))))

(if (> (vector-length command-line-arguments) 0)
  (try-gen-keys (vector-ref command-line-arguments 0)))
KeyPairGenerator generator =
                 KeyPairGenerator.getInstance("RSA");
 generator.initialize(1024);
 KeyPair keyPair = generator.generateKeyPair();

import java.security.KeyPairGenerator;
 import java.security.NoSuchAlgorithmException;
 import java.security.KeyPair;

 public class AsymmetricKeyMaker {

    public static void main(String[] args) {
      String algorithm = "";
      if (args.length == 1) algorithm = args[0];

      try {
        KeyPair keyPair = KeyPairGenerator
                             .getInstance(algorithm)
                             .generateKeyPair();

        System.out.println(keyPair.getPublic());
        System.out.println(keyPair.getPrivate());

      } catch (NoSuchAlgorithmException e) {
        System.err.println(
          "usage: java AsymmetricKeyMaker <RSA | DSA>");
      }

    }
 }

Next: JEmacs—The Java/Scheme-based Emacs, Previous: Sending Options to Kawa in a Command Script, Up: Kawa Scheme   [Contents][Index]