Search in sources :

Example 1 with SecretKeyType

use of org.kse.crypto.secretkey.SecretKeyType in project keystore-explorer by kaikramer.

the class GenerateSecretKeyAction method generateSecret.

/**
 * Generate a secret key in the currently opened KeyStore.
 */
public void generateSecret() {
    try {
        int secretKeySize = applicationSettings.getGenerateSecretKeySize();
        SecretKeyType secretKeyType = applicationSettings.getGenerateSecretKeyType();
        DGenerateSecretKey dGenerateSecretKey = new DGenerateSecretKey(frame, secretKeyType, secretKeySize);
        dGenerateSecretKey.setLocationRelativeTo(frame);
        dGenerateSecretKey.setVisible(true);
        if (!dGenerateSecretKey.isSuccessful()) {
            return;
        }
        secretKeySize = dGenerateSecretKey.getSecretKeySize();
        secretKeyType = dGenerateSecretKey.getSecretKeyType();
        applicationSettings.setGenerateSecretKeySize(secretKeySize);
        applicationSettings.setGenerateSecretKeyType(secretKeyType);
        SecretKey secretKey = SecretKeyUtil.generateSecretKey(secretKeyType, secretKeySize);
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("GenerateSecretKeyAction.NewSecretKeyEntryAlias.Title"), null);
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String alias = dGetAlias.getAlias();
        if (alias == null) {
            return;
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("GenerateSecretKeyAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("GenerateSecretKeyAction.NewSecretKeyEntryAlias.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
        }
        Password password = new Password((char[]) null);
        KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
        if (type.hasEntryPasswords()) {
            DGetNewPassword dGetNewPassword = new DGetNewPassword(frame, res.getString("GenerateSecretKeyAction.NewSecretKeyEntryPassword.Title"), applicationSettings.getPasswordQualityConfig());
            dGetNewPassword.setLocationRelativeTo(frame);
            dGetNewPassword.setVisible(true);
            password = dGetNewPassword.getPassword();
            if (password == null) {
                return;
            }
        }
        if (keyStore.containsAlias(alias)) {
            keyStore.deleteEntry(alias);
            newState.removeEntryPassword(alias);
        }
        keyStore.setKeyEntry(alias, secretKey, password.toCharArray(), null);
        newState.setEntryPassword(alias, password);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("GenerateSecretKeyAction.SecretKeyGenerationSuccessful.message"), res.getString("GenerateSecretKeyAction.GenerateSecretKey.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) DGenerateSecretKey(org.kse.gui.dialogs.DGenerateSecretKey) KeyStore(java.security.KeyStore) DGetAlias(org.kse.gui.dialogs.DGetAlias) DGenerateSecretKey(org.kse.gui.dialogs.DGenerateSecretKey) SecretKey(javax.crypto.SecretKey) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) SecretKeyType(org.kse.crypto.secretkey.SecretKeyType) DGetNewPassword(org.kse.gui.password.DGetNewPassword) DGetNewPassword(org.kse.gui.password.DGetNewPassword) Password(org.kse.crypto.Password)

Example 2 with SecretKeyType

use of org.kse.crypto.secretkey.SecretKeyType in project keystore-explorer by kaikramer.

the class DViewSecretKey method populateDialog.

private void populateDialog() throws CryptoException {
    KeyInfo keyInfo = SecretKeyUtil.getKeyInfo(secretKey);
    String algorithm = keyInfo.getAlgorithm();
    // Try and get friendly algorithm name
    SecretKeyType secretKeyType = SecretKeyType.resolveJce(algorithm);
    if (secretKeyType != null) {
        algorithm = secretKeyType.friendly();
    }
    jtfAlgorithm.setText(algorithm);
    Integer keyLength = keyInfo.getSize();
    if (keyLength != null) {
        jtfKeySize.setText(MessageFormat.format(res.getString("DViewSecretKey.jtfKeySize.text"), "" + keyLength));
    } else {
        jtfKeySize.setText(MessageFormat.format(res.getString("DViewSecretKey.jtfKeySize.text"), "?"));
    }
    jtfFormat.setText(secretKey.getFormat());
    jtfEncoded.setText("0x" + new BigInteger(1, secretKey.getEncoded()).toString(16).toUpperCase());
    jtfEncoded.setCaretPosition(0);
}
Also used : BigInteger(java.math.BigInteger) KeyInfo(org.kse.crypto.KeyInfo) SecretKeyType(org.kse.crypto.secretkey.SecretKeyType) BigInteger(java.math.BigInteger)

Example 3 with SecretKeyType

use of org.kse.crypto.secretkey.SecretKeyType in project keystore-explorer by kaikramer.

the class DGenerateSecretKey method initComponents.

private void initComponents() {
    jlKeySize = new JLabel(res.getString("DGenerateSecretKey.jlKeySize.text"));
    jsKeySize = new JSpinner();
    jsKeySize.setToolTipText(res.getString("DGenerateSecretKey.jsKeySize.tooltip"));
    jpKeySize = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpKeySize.add(jlKeySize);
    jpKeySize.add(jsKeySize);
    jlKeyAlg = new JLabel(res.getString("DGenerateSecretKey.jlKeyAlg.text"));
    jcbKeyAlg = new JComboBox<SecretKeyType>();
    jcbKeyAlg.setToolTipText(res.getString("DGenerateSecretKey.jcbKeyAlg.tooltip"));
    populateKeyAlgs();
    loadKeySizes(secretKeySize);
    jcbKeyAlg.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent evt) {
            loadKeySizes(getSecretKeySize());
        }
    });
    jpKeyAlg = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpKeyAlg.add(jlKeyAlg);
    jpKeyAlg.add(jcbKeyAlg);
    jsKeySize.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent evt) {
            correctSecretKeySize();
        }
    });
    jpOptions = new JPanel(new GridLayout(2, 1, 5, 5));
    jpOptions.add(jpKeyAlg);
    jpOptions.add(jpKeySize);
    jpOptions.setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5), new EtchedBorder()));
    jbOK = new JButton(res.getString("DGenerateSecretKey.jbOK.text"));
    jbOK.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            okPressed();
        }
    });
    jbCancel = new JButton(res.getString("DGenerateSecretKey.jbCancel.text"));
    jbCancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            cancelPressed();
        }
    });
    jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_KEY);
    jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent evt) {
            cancelPressed();
        }
    });
    jpButtons = PlatformUtil.createDialogButtonPanel(jbOK, jbCancel, false);
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(jpOptions, BorderLayout.CENTER);
    getContentPane().add(jpButtons, BorderLayout.SOUTH);
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent evt) {
            closeDialog();
        }
    });
    setResizable(false);
    getRootPane().setDefaultButton(jbOK);
    pack();
}
Also used : JPanel(javax.swing.JPanel) ItemEvent(java.awt.event.ItemEvent) FlowLayout(java.awt.FlowLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) WindowAdapter(java.awt.event.WindowAdapter) GridLayout(java.awt.GridLayout) EtchedBorder(javax.swing.border.EtchedBorder) ChangeEvent(javax.swing.event.ChangeEvent) ActionListener(java.awt.event.ActionListener) BorderLayout(java.awt.BorderLayout) SecretKeyType(org.kse.crypto.secretkey.SecretKeyType) WindowEvent(java.awt.event.WindowEvent) JSpinner(javax.swing.JSpinner) ItemListener(java.awt.event.ItemListener) ChangeListener(javax.swing.event.ChangeListener) CompoundBorder(javax.swing.border.CompoundBorder) EmptyBorder(javax.swing.border.EmptyBorder) AbstractAction(javax.swing.AbstractAction)

Example 4 with SecretKeyType

use of org.kse.crypto.secretkey.SecretKeyType in project keystore-explorer by kaikramer.

the class DGenerateSecretKey method correctSecretKeySize.

private void correctSecretKeySize() {
    SecretKeyType secretKeyType = getSecretKeyType();
    int secretKeySize = getSecretKeySize();
    int validatedSecretKeySize = validateSecretKeySize(secretKeyType, secretKeySize);
    if (validatedSecretKeySize != secretKeySize) {
        jsKeySize.getModel().setValue(validatedSecretKeySize);
    }
}
Also used : SecretKeyType(org.kse.crypto.secretkey.SecretKeyType)

Example 5 with SecretKeyType

use of org.kse.crypto.secretkey.SecretKeyType in project keystore-explorer by kaikramer.

the class DGenerateSecretKey method loadKeySizes.

private void loadKeySizes(int secretKeySize) {
    SecretKeyType secretKeyType = getSecretKeyType();
    secretKeySize = validateSecretKeySize(secretKeyType, secretKeySize);
    jsKeySize.setModel(new SpinnerNumberModel(secretKeySize, secretKeyType.minSize(), secretKeyType.maxSize(), secretKeyType.stepSize()));
    jsKeySize.setEnabled(secretKeyType.maxSize() > secretKeyType.minSize());
}
Also used : SpinnerNumberModel(javax.swing.SpinnerNumberModel) SecretKeyType(org.kse.crypto.secretkey.SecretKeyType)

Aggregations

SecretKeyType (org.kse.crypto.secretkey.SecretKeyType)6 BigInteger (java.math.BigInteger)2 KeyInfo (org.kse.crypto.KeyInfo)2 BorderLayout (java.awt.BorderLayout)1 FlowLayout (java.awt.FlowLayout)1 GridLayout (java.awt.GridLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 KeyStore (java.security.KeyStore)1 SecretKey (javax.crypto.SecretKey)1 AbstractAction (javax.swing.AbstractAction)1 JButton (javax.swing.JButton)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JSpinner (javax.swing.JSpinner)1 SpinnerNumberModel (javax.swing.SpinnerNumberModel)1