|
| 1 | +/* |
| 2 | + Copyright (C) 2017 Georg Schmidt <gs-develop@gs-sys.de> |
| 3 | + This program is free software: you can redistribute it and/or modify |
| 4 | + it under the terms of the GNU General Public License as published by |
| 5 | + the Free Software Foundation, either version 3 of the License, or |
| 6 | + (at your option) any later version. |
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | + You should have received a copy of the GNU General Public License |
| 12 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 13 | +*/ |
| 14 | + |
| 15 | +package de.gs_sys.gpg.gui; |
| 16 | + |
| 17 | +import de.gs_sys.basics.data.InputStreamToString; |
| 18 | +import de.gs_sys.basics.file.StringAndFiles; |
| 19 | + |
| 20 | +import javax.swing.*; |
| 21 | +import java.awt.*; |
| 22 | +import java.awt.event.ActionEvent; |
| 23 | +import java.awt.event.ActionListener; |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Georg Schmidt |
| 29 | + * @version 1.0 vom 30.04.2017 |
| 30 | + */ |
| 31 | + |
| 32 | +public class KeyGen extends JFrame { |
| 33 | + private JTextField tf_name = new JTextField(); |
| 34 | + private JLabel jLabel1 = new JLabel(); |
| 35 | + private JLabel jLabel2 = new JLabel(); |
| 36 | + private JTextField tf_mail = new JTextField(); |
| 37 | + private JLabel jLabel3 = new JLabel(); |
| 38 | + private JTextField tf_password = new JTextField(); |
| 39 | + private JLabel jLabel4 = new JLabel(); |
| 40 | + private JLabel jLabel5 = new JLabel(); |
| 41 | + private JComboBox<String> cb_keysize = new JComboBox<String>(); |
| 42 | + private DefaultComboBoxModel<String> cb_keysizeModel = new DefaultComboBoxModel<String>(); |
| 43 | + private JButton b_save = new JButton(); |
| 44 | + private JButton b_exit = new JButton(); |
| 45 | + private JLabel jLabel6 = new JLabel(); |
| 46 | + private JTextArea ta_log = new JTextArea(""); |
| 47 | + private JScrollPane ta_logScrollPane = new JScrollPane(ta_log); |
| 48 | + |
| 49 | + public KeyGen() { |
| 50 | + super(); |
| 51 | + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 52 | + int frameWidth = 300; |
| 53 | + int frameHeight = 295; |
| 54 | + setSize(frameWidth, frameHeight); |
| 55 | + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); |
| 56 | + int x = (d.width - getSize().width) / 2; |
| 57 | + int y = (d.height - getSize().height) / 2; |
| 58 | + setLocation(x, y); |
| 59 | + setTitle("Generate a GPG Key"); |
| 60 | + setResizable(false); |
| 61 | + Container cp = getContentPane(); |
| 62 | + cp.setLayout(null); |
| 63 | + |
| 64 | + tf_name.setBounds(80, 16, 193, 25); |
| 65 | + cp.add(tf_name); |
| 66 | + jLabel1.setBounds(8, 16, 67, 25); |
| 67 | + jLabel1.setText("Name"); |
| 68 | + cp.add(jLabel1); |
| 69 | + jLabel2.setBounds(8, 48, 67, 25); |
| 70 | + jLabel2.setText("E-Mail"); |
| 71 | + cp.add(jLabel2); |
| 72 | + tf_mail.setBounds(80, 48, 193, 25); |
| 73 | + cp.add(tf_mail); |
| 74 | + jLabel3.setBounds(8, 80, 67, 25); |
| 75 | + jLabel3.setText("Password"); |
| 76 | + cp.add(jLabel3); |
| 77 | + tf_password.setBounds(80, 80, 193, 25); |
| 78 | + cp.add(tf_password); |
| 79 | + jLabel4.setBounds(8, 112, 67, 25); |
| 80 | + jLabel4.setText("Key size"); |
| 81 | + cp.add(jLabel4); |
| 82 | + |
| 83 | + jLabel5.setBounds(200, 112, 43, 25); |
| 84 | + jLabel5.setText("bits"); |
| 85 | + cp.add(jLabel5); |
| 86 | + cb_keysize.setModel(cb_keysizeModel); |
| 87 | + cb_keysize.setBounds(80, 112, 113, 25); |
| 88 | + cp.add(cb_keysize); |
| 89 | + b_save.setBounds(72, 216, 145, 25); |
| 90 | + b_save.setText("Generate"); |
| 91 | + b_save.setMargin(new Insets(2, 2, 2, 2)); |
| 92 | + b_save.addActionListener(new ActionListener() { |
| 93 | + public void actionPerformed(ActionEvent evt) { |
| 94 | + b_save_ActionPerformed(evt); |
| 95 | + } |
| 96 | + }); |
| 97 | + cp.add(b_save); |
| 98 | + b_exit.setBounds(224, 216, 49, 25); |
| 99 | + b_exit.setText("Exit"); |
| 100 | + b_exit.setMargin(new Insets(2, 2, 2, 2)); |
| 101 | + b_exit.addActionListener(new ActionListener() { |
| 102 | + public void actionPerformed(ActionEvent evt) { |
| 103 | + b_exit_ActionPerformed(evt); |
| 104 | + } |
| 105 | + }); |
| 106 | + cp.add(b_exit); |
| 107 | + jLabel6.setBounds(8, 216, 51, 25); |
| 108 | + jLabel6.setText("Ver 1.0"); |
| 109 | + cp.add(jLabel6); |
| 110 | + ta_logScrollPane.setBounds(8, 144, 265, 65); |
| 111 | + cp.add(ta_logScrollPane); |
| 112 | + |
| 113 | + cb_keysize.addItem("1024"); |
| 114 | + cb_keysize.addItem("2048"); |
| 115 | + cb_keysize.addItem("4096"); |
| 116 | + /* |
| 117 | + cb_keysize.addItem("8192"); |
| 118 | + cb_keysize.addItem("16384"); |
| 119 | + cb_keysize.addItem("32768"); |
| 120 | + cb_keysize.addItem("65536"); |
| 121 | + */ |
| 122 | + |
| 123 | + setVisible(true); |
| 124 | + } // end of public KeyGen |
| 125 | + |
| 126 | + public static void main(String[] args) { |
| 127 | + new KeyGen(); |
| 128 | + } // end of main |
| 129 | + |
| 130 | + public void b_save_ActionPerformed(ActionEvent evt) { |
| 131 | + |
| 132 | + String keysize = cb_keysize.getSelectedItem().toString(); |
| 133 | + |
| 134 | + String name = tf_name.getText(); |
| 135 | + if (!name.isEmpty()) |
| 136 | + name = " Name-Real: " + name + "\n"; |
| 137 | + |
| 138 | + String mail = tf_mail.getText(); |
| 139 | + if (!mail.isEmpty()) |
| 140 | + mail = " Name-Email: " + mail + "\n"; |
| 141 | + |
| 142 | + String password = tf_password.getText(); |
| 143 | + if (!password.isEmpty()) { |
| 144 | + password = " Passphrase: " + password + "\n"; |
| 145 | + } else { |
| 146 | + password = "%no-protection\n%no-ask-passphrase\n"; |
| 147 | + } |
| 148 | + |
| 149 | + if (name.isEmpty() && mail.isEmpty()) { |
| 150 | + ta_log.append("Name or Mail must be set! Try again!\n"); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + String config = "%echo Generating a basic OpenPGP key\n" + |
| 155 | + " Key-Type: RSA\n" + |
| 156 | + " Key-Length: " + keysize + "\n" + |
| 157 | + " Subkey-Type: RSA\n" + |
| 158 | + " Subkey-Length: " + keysize + "\n" + |
| 159 | + " Preferences: SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed\n" + |
| 160 | + name + |
| 161 | + mail + |
| 162 | + " Expire-Date: 10y\n" + |
| 163 | + password + |
| 164 | + " # Do a commit here, so that we can later print \"done\"\n" + |
| 165 | + " %commit\n" + |
| 166 | + " %echo done"; |
| 167 | + |
| 168 | + try { |
| 169 | + StringAndFiles.toFile("tmp5456.del", config); |
| 170 | + |
| 171 | + Process p = Runtime.getRuntime().exec("gpg2 --batch --gen-key tmp5456.del"); |
| 172 | + |
| 173 | + while (p.isAlive()) ; |
| 174 | + |
| 175 | + if (p.exitValue() == 0) |
| 176 | + ta_log.append("Key for " + tf_name.getText() + " " + tf_mail.getText() + " successfully generated!\n"); |
| 177 | + else |
| 178 | + ta_log.append("Key for " + tf_name.getText() + " " + tf_mail.getText() + " failed!\n" + InputStreamToString.toString(p.getErrorStream()) + "\n"); |
| 179 | + |
| 180 | + |
| 181 | + new File("tmp5456.del").delete(); |
| 182 | + } catch (IOException e) { |
| 183 | + e.printStackTrace(); |
| 184 | + } |
| 185 | + } // end of b_save_ActionPerformed |
| 186 | + |
| 187 | + public void b_exit_ActionPerformed(ActionEvent evt) { |
| 188 | + System.exit(0); |
| 189 | + } // end of b_exit_ActionPerformed |
| 190 | +} // end of class KeyGen |
0 commit comments