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 13 public class CryptoKeySelector { 14 15 18 public static java.security.Key selectPublicKey(String message, String type) throws java.security.GeneralSecurityException { 19 20 Set publicKeys = Pooka.getCryptoManager().publicKeyAliases(type); 21 String 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 31 public static java.security.Key selectPrivateKey(String message, String type) throws java.security.GeneralSecurityException { 32 33 Set privateKeys = Pooka.getCryptoManager().privateKeyAliases(type); 34 String 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 returnValue = null; 39 try { 40 returnValue = Pooka.getCryptoManager().getPrivateKey(alias); 41 } catch (java.security.UnrecoverableKeyException uke) { 42 char[] password = showPassphraseDialog(alias); 43 returnValue = Pooka.getCryptoManager().getPrivateKey(alias, password); 44 } 45 46 return returnValue; 47 } 48 49 52 public static String showKeySet(Set keys, String message, String 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 JLabel label = new JLabel(message); 61 Object [] messageComponents = new Object [] { 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 selectAlias = (String )displayList.getSelectedValue(); 66 return selectAlias; 67 } 68 69 return null; 70 } 71 72 75 public static char[] showPassphraseDialog(String alias) { 76 JPasswordField field = new JPasswordField(); 77 JLabel label = new JLabel(Pooka.getProperty("Pooka.crypto.passphrase.message", "Enter passphrase for key ") + alias); 79 Object [] messageComponents = new Object [] { 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 97 } 98 99 100 101 } 102 | Popular Tags |