📄 sshkeygenerationdialog.java
字号:
} public static KeyPair generateKeyPair(String alg, int bits) throws NoSuchAlgorithmException { KeyPairGenerator kpg = KeyPairGenerator.getInstance(alg); kpg.initialize(bits, client.secureRandom()); return kpg.generateKeyPair(); } private static void saveKeyPair(KeyPair kp) throws IOException, SSH2Exception, NoSuchAlgorithmException { String passwd = pwdText.getText(); String fileName = fileText.getText(); String subject = client.propsHandler.getProperty("usrname"); String comment = commText.getText(); String pubKeyStr = null; if(subject == null) { subject = SSH.VER_MINDTERM; } if("RSA (ssh1)".equals(choiceType.getSelectedItem())) { pubKeyStr = SSH.generateKeyFiles((RSAPrivateCrtKey)kp.getPrivate(), expandFileName(fileName), passwd, comment); } else { SSH2PublicKeyFile pkif = new SSH2PublicKeyFile(kp.getPublic(), subject, comment); // When key is unencrypted OpenSSH doesn't tolerate headers... // if(passwd == null || passwd.length() == 0) { subject = null; comment = null; } SSH2KeyPairFile kpf = new SSH2KeyPairFile(kp, subject, comment); kpf.store(expandFileName(fileName), SSH.secureRandom(), passwd); pubKeyStr = pkif.store(expandFileName(fileName + ".pub"), !cbOpenSSH.getState()); } Terminal term = client.sshStdIO.getTerminal(); if(term instanceof TerminalWin) { ((TerminalWin)term).getClipboard().setSelection(pubKeyStr); } okBut.setEnabled(true); pwdText.setText(""); pwdText2.setText(""); progBar.setValue(0); setDefaultFileName(client); } private static void resetValues() { okBut.setEnabled(true); choiceBits.select("1024"); setDefaultFileName(client); generatedAndSaved = false; pwdText.setText(""); pwdText2.setText(""); descText.setText(keyGenerationHelp); okBut.setLabel("Generate"); cardLayout.show(cardPanel, "first"); RandomSeed seed = client.randomSeed(); seed.resetEntropyCount(); progBar.setValue(0); seed.addProgress(progBar); } private static boolean checkValues(String passwd, String passwd2, String fileName) { if(!passwd.equals(passwd2)) { alert("Please give same password twice"); return false; } if(fileName.length() == 0) { alert("Filename can't be empty"); return false; } OutputStream out = getOutput(fileName); if(out == null) { alert("Can't open '" + fileName + "' for saving."); return false; } try { out.close(); } catch (Exception e) { /* don't care */ } return true; } private static OutputStream getOutput(String fileName) { fileName = expandFileName(fileName); FileOutputStream f; try { f = new FileOutputStream(fileName); return f; } catch (Throwable t) { return null; } } private static String expandFileName(String fileName) { if(fileName.indexOf(File.separator) == -1) fileName = client.propsHandler.getSSHHomeDir() + fileName; return fileName; } private static Dialog editKeyDialog; private static FileDialog editKeyLoad; private static TextField fileTextEd; private static TextField pwdTextEd; private static TextField pwdText2Ed; private static TextField subjTextEd; private static TextField commTextEd; private static Label typeLbl; private static Label bitLbl; private static Checkbox cbOpenSSHEd; private static Checkbox cbSSHComEd; private static Button okButEd; private static Button cancButEd; private static SSH2KeyPairFile kpf = new SSH2KeyPairFile(); private static SSH2PublicKeyFile pkf = new SSH2PublicKeyFile(); public static void editKeyDialog(Frame par, SSHInteractiveClient cli) { parent = par; client = cli; if(editKeyLoad == null) { editKeyLoad = new FileDialog(parent, "MindTerm - Select key file to edit", FileDialog.LOAD); } editKeyLoad.setDirectory(client.propsHandler.getSSHHomeDir()); editKeyLoad.setVisible(true); String fileName = editKeyLoad.getFile(); String dirName = editKeyLoad.getDirectory(); String passwd = null; kpf = new SSH2KeyPairFile(); pkf = new SSH2PublicKeyFile(); if(fileName != null && fileName.length() > 0) { if(!dirName.endsWith(File.separator)) { dirName += File.separator; } fileName = dirName + fileName; try { pkf.load(fileName + ".pub"); } catch (Exception e) { pkf = null; } boolean retryPasswd = false; do { try { kpf.load(fileName, passwd); break; } catch(SSH2AccessDeniedException e) { /* Retry... */ retryPasswd = true; } catch(Exception e) { alert("Error loading key file: " + e.getMessage()); } } while((passwd = SSHMiscDialogs.password("MindTerm - File Password", "Please give password for " + fileName, parent)) != null); if(retryPasswd && passwd == null) { return; } } else { return; } if(pkf == null) { pkf = new SSH2PublicKeyFile(kpf.getKeyPair().getPublic(), kpf.getSubject(), kpf.getComment()); } if(editKeyDialog == null) { editKeyDialog = new Dialog(parent, "MindTerm - Publickey Keypair Edit", true); AWTGridBagContainer grid = new AWTGridBagContainer(editKeyDialog); Label label = new Label("Key type/format:"); grid.add(label, 0, 2); typeLbl = new Label("DSA"); grid.add(typeLbl, 0, 2); label = new Label("Key length (bits):"); grid.add(label, 1, 2); bitLbl = new Label("1024"); grid.add(bitLbl, 1, 2); label = new Label("Identity file:"); grid.add(label, 2, 2); fileTextEd = new TextField("", 18); grid.add(fileTextEd, 2, 2); grid.getConstraints().fill = GridBagConstraints.HORIZONTAL; label = new Label("Password:"); grid.add(label, 3, 2); pwdTextEd = new TextField("", 18); pwdTextEd.setEchoChar('*'); grid.add(pwdTextEd, 3, 2); label = new Label("Password again:"); grid.add(label, 4, 2); pwdText2Ed = new TextField("", 18); pwdText2Ed.setEchoChar('*'); grid.add(pwdText2Ed, 4, 2); label = new Label("Subject:"); grid.add(label, 5, 2); subjTextEd = new TextField("", 18); grid.add(subjTextEd, 5, 2); label = new Label("Comment:"); grid.add(label, 6, 2); commTextEd = new TextField("", 18); grid.add(commTextEd, 6, 2); cbSSHComEd = new Checkbox("SSH Comm. private file format"); grid.add(cbSSHComEd, 7, 4); cbOpenSSHEd = new Checkbox("OpenSSH public public format"); grid.add(cbOpenSSHEd, 8, 4); Panel bp = new Panel(new FlowLayout()); bp.add(okButEd = new Button("Save")); okButEd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String fName = fileTextEd.getText(); String pwd = pwdTextEd.getText(); if(checkValues(pwd, pwdText2Ed.getText(), fName)) { fName = expandFileName(fName); try { String s = subjTextEd.getText(); String c = commTextEd.getText(); pkf.setSubject(s); pkf.setComment(c); pkf.store(fName + ".pub", !cbOpenSSHEd.getState()); if(!cbSSHComEd.getState() && (pwd == null || pwd.length() == 0)) { s = null; c = null; } kpf.setSubject(s); kpf.setComment(c); kpf.store(fName, SSH.secureRandom(), pwd, cbSSHComEd.getState()); editKeyDialog.setVisible(false); } catch (Exception ee) { alert("Error saving files: " + ee.getMessage()); } } } }); bp.add(cancButEd = new Button("Cancel")); cancButEd.addActionListener(new AWTConvenience.CloseAction(editKeyDialog));; grid.add(bp, 9, GridBagConstraints.REMAINDER); editKeyDialog.addWindowListener(new AWTConvenience.CloseAdapter(cancButEd)); AWTConvenience.setBackgroundOfChildren(editKeyDialog); AWTConvenience.setKeyListenerOfChildren(editKeyDialog, new AWTConvenience.OKCancelAdapter(okButEd, cancButEd), null); editKeyDialog.pack(); } fileTextEd.setText(fileName); pwdTextEd.setText(passwd); pwdText2Ed.setText(passwd); typeLbl.setText(kpf.getAlgorithmName()); bitLbl.setText(String.valueOf(kpf.getBitLength())); subjTextEd.setText(kpf.getSubject()); commTextEd.setText(kpf.getComment()); cbSSHComEd.setState(kpf.isSSHComFormat()); cbOpenSSHEd.setState(!pkf.isSSHComFormat()); AWTConvenience.placeDialog(editKeyDialog); editKeyDialog.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -