KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > crypto > CryptoKeySelector


1 package net.suberic.pooka.gui.crypto;
2
3 import net.suberic.pooka.*;
4 import net.suberic.crypto.*;
5
6 import javax.swing.*;
7
8 import java.util.*;
9
10 /**
11  * Selects a Key.
12  */

13 public class CryptoKeySelector {
14
15   /**
16    * Selects a public key.
17    */

18   public static java.security.Key JavaDoc selectPublicKey(String JavaDoc message, String JavaDoc type) throws java.security.GeneralSecurityException JavaDoc {
19
20     Set publicKeys = Pooka.getCryptoManager().publicKeyAliases(type);
21     String JavaDoc alias = showKeySet(publicKeys, message, Pooka.getProperty("Pooka.crypto.publicKey.title", "Select public key."));
22     if (alias == null)
23       return null;
24     
25     return Pooka.getCryptoManager().getPublicKey(alias);
26   }
27
28   /**
29    * Selects a private key.
30    */

31   public static java.security.Key JavaDoc selectPrivateKey(String JavaDoc message, String JavaDoc type) throws java.security.GeneralSecurityException JavaDoc {
32
33     Set privateKeys = Pooka.getCryptoManager().privateKeyAliases(type);
34     String JavaDoc alias = showKeySet(privateKeys, message, Pooka.getProperty("Pooka.crypto.privateKey.title", "Select private key."));
35     if (alias == null)
36       return null;
37
38     java.security.Key JavaDoc returnValue = null;
39     try {
40       returnValue = Pooka.getCryptoManager().getPrivateKey(alias);
41     } catch (java.security.UnrecoverableKeyException JavaDoc uke) {
42       char[] password = showPassphraseDialog(alias);
43       returnValue = Pooka.getCryptoManager().getPrivateKey(alias, password);
44     }
45     
46     return returnValue;
47   }
48     
49   /**
50    * Shows a dialog for selecting a key from a set.
51    */

52   public static String JavaDoc showKeySet(Set keys, String JavaDoc message, String JavaDoc title) {
53
54     Vector keyList = new Vector(keys);
55     JList displayList = new JList(keyList);
56     if (displayList.getModel().getSize() > 0)
57       displayList.setSelectedIndex(0);
58
59     //int value = JOptionPane.showConfirmDialog(Pooka.getMainPanel(), new JScrollPane(displayList), title, JOptionPane.YES_NO_OPTION);
60
JLabel label = new JLabel(message);
61     Object JavaDoc[] messageComponents = new Object JavaDoc[] { label, new JScrollPane(displayList) };
62     
63     int value = Pooka.getUIFactory().showConfirmDialog(messageComponents, title, JOptionPane.OK_CANCEL_OPTION);
64     if (value != JOptionPane.CANCEL_OPTION) {
65       String JavaDoc selectAlias = (String JavaDoc)displayList.getSelectedValue();
66       return selectAlias;
67     }
68
69     return null;
70   }
71
72   /**
73    * Shows a dialog for selecting a password.
74    */

75   public static char[] showPassphraseDialog(String JavaDoc alias) {
76     JPasswordField field = new JPasswordField();
77     //int value = JOptionPane.showConfirmDialog(Pooka.getMainPanel(), field, "Enter passphrase", JOptionPane.YES_NO_OPTION);
78
JLabel label = new JLabel(Pooka.getProperty("Pooka.crypto.passphrase.message", "Enter passphrase for key ") + alias);
79     Object JavaDoc[] messageComponents = new Object JavaDoc[] { label, field };
80     field.setRequestFocusEnabled(true);
81
82     int value = Pooka.getUIFactory().showConfirmDialog(messageComponents, Pooka.getProperty("Pooka.crypto.passphrase.title", "Enter passphrase"), JOptionPane.OK_CANCEL_OPTION);
83
84     if (value != JOptionPane.CANCEL_OPTION) {
85       return field.getPassword();
86     }
87     
88     return null;
89
90     /*
91     String returnValue = Pooka.getUIFactory().showInputDialog(messageComponents, Pooka.getProperty("Pooka.crypto.passphrase.title", "Enter passphrase"));
92     if (returnValue == null)
93       return null;
94     else
95       return returnValue.toCharArray();
96     */

97   }
98
99
100   
101 }
102
Popular Tags