📄 addrsakeydialog.java
字号:
package ly.util;
import java.awt.BorderLayout;
import java.io.*;
import javax.swing.JPanel;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.sql.*;
import java.security.*;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
public class AddRSAKeyDialog extends JDialog {
private RSAPublicKey pubKey =null;
private RSAPrivateKey priKey =null;
private RSAPublicKey recoveryPubKey =null;
private RSAPrivateKey recoveryPriKey = null;
private JPanel jContentPane = null;
private DSBLMainFrame father;
private JPanel jPanel = null;
private JScrollPane jScrollPane = null;
private JTextPane jTextPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JTextField jTextField2 = null;
private JTextField jTextField3 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JLabel jLabel4 = null;
public AddRSAKeyDialog(Frame owner, DSBLMainFrame ft, String title,
boolean modal) throws HeadlessException {
super(owner, title, modal);
father = ft;
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(481, 341);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel4 = new JLabel();
jLabel4.setBounds(new java.awt.Rectangle(19, 138, 141, 18));
jLabel4.setText("RSA密钥备注与描述:");
jLabel3 = new JLabel();
jLabel3.setBounds(new java.awt.Rectangle(18, 97, 89, 18));
jLabel3.setText("RSA密钥名称:");
jLabel2 = new JLabel();
jLabel2.setBounds(new java.awt.Rectangle(19, 69, 87, 18));
jLabel2.setText("RSA密钥长度:");
jLabel1 = new JLabel();
jLabel1.setBounds(new java.awt.Rectangle(19, 42, 67, 18));
jLabel1.setText("RSA私钥:");
jLabel = new JLabel();
jLabel.setBounds(new java.awt.Rectangle(19, 14, 62, 18));
jLabel.setText("RSA公钥:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJScrollPane(), null);
jPanel.add(jLabel, null);
jPanel.add(jLabel1, null);
jPanel.add(jLabel2, null);
jPanel.add(jLabel3, null);
jPanel.add(getJTextField(), null);
jPanel.add(getJTextField1(), null);
jPanel.add(getJTextField2(), null);
jPanel.add(getJTextField3(), null);
jPanel.add(getJButton(), null);
jPanel.add(getJButton1(), null);
jPanel.add(getJButton2(), null);
jPanel.add(jLabel4, null);
}
return jPanel;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new java.awt.Rectangle(17, 156, 442, 91));
jScrollPane.setViewportView(getJTextPane());
}
return jScrollPane;
}
/**
* This method initializes jTextPane
*
* @return javax.swing.JTextPane
*/
private JTextPane getJTextPane() {
if (jTextPane == null) {
jTextPane = new JTextPane();
jTextPane.setText("");
}
return jTextPane;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new java.awt.Rectangle(85, 11, 363, 22));
jTextField.setEditable(false);
jTextField.setText("");
}
return jTextField;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new java.awt.Rectangle(85, 39, 364, 22));
jTextField1.setEditable(false);
jTextField1.setText("");
}
return jTextField1;
}
/**
* This method initializes jTextField2
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField2() {
if (jTextField2 == null) {
jTextField2 = new JTextField();
jTextField2.setBounds(new java.awt.Rectangle(106, 67, 47, 22));
jTextField2.setText("1024");
}
return jTextField2;
}
/**
* This method initializes jTextField3
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField3() {
if (jTextField3 == null) {
jTextField3 = new JTextField();
jTextField3.setBounds(new java.awt.Rectangle(18, 116, 292, 22));
jTextField3.setText("");
}
return jTextField3;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new java.awt.Rectangle(293, 66, 157, 20));
jButton.setText("随机生成RSA密钥");
jButton.addActionListener(new MyB2ActionListener(this));
}
return jButton;
}
private class MyB2ActionListener implements java.awt.event.ActionListener {
private AddRSAKeyDialog father;
public MyB2ActionListener(AddRSAKeyDialog ft) {
super();
father = ft;
}
public void actionPerformed(java.awt.event.ActionEvent e) {
if (Integer.parseInt(father.getJTextField2().getText()) > 0
&& Integer.parseInt(father.getJTextField2().getText()) <= 1024) {
try {
KeyPair keyPair = lyRSA.generateKeyPair(Integer
.parseInt(father.getJTextField2().getText()));
pubKey = (RSAPublicKey) keyPair.getPublic();
priKey = (RSAPrivateKey) keyPair.getPrivate();
byte[] pubModBytes = pubKey.getModulus().toByteArray();// 取得公要模
byte[] pubPubExpBytes = pubKey.getPublicExponent()
.toByteArray();// 取得公要指数
byte[] priModBytes = priKey.getModulus().toByteArray();// 取得私要模
byte[] priPriExpBytes = priKey.getPrivateExponent()
.toByteArray();// 取得私要指数
recoveryPubKey = lyRSA.generateRSAPublicKey(
pubModBytes, pubPubExpBytes);
recoveryPriKey = lyRSA.generateRSAPrivateKey(
priModBytes, priPriExpBytes);
father.getJTextField().setText(recoveryPubKey.getModulus().toString());
father.getJTextField1().setText(priKey.getPrivateExponent().toString());
} catch (Exception e1) {
JOptionPane.showMessageDialog(father, e.toString(), "错误:",
JOptionPane.ERROR_MESSAGE);
}
} else
JOptionPane.showMessageDialog(father, "密钥长度范围:1~1024,请正确输入",
"错误:", JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new java.awt.Rectangle(287, 270, 68, 21));
jButton1.setText("确定");
jButton1.addActionListener(new MyB1ActionListener(this));
}
return jButton1;
}
private class MyB1ActionListener implements java.awt.event.ActionListener {
private AddRSAKeyDialog father;
public MyB1ActionListener(AddRSAKeyDialog ft) {
super();
father = ft;
}
public void actionPerformed(java.awt.event.ActionEvent e) {
if (father.priKey!= null) {
try {
ByteArrayOutputStream bout1 = new ByteArrayOutputStream();
ObjectOutputStream oout1 = new ObjectOutputStream(
bout1);
oout1.writeObject(recoveryPubKey);
oout1.flush();
oout1.close();
ByteArrayOutputStream bout2 = new ByteArrayOutputStream();
ObjectOutputStream oout2 = new ObjectOutputStream(
bout2);
oout2.writeObject(priKey);
oout2.flush();
oout2.close();
ByteArrayInputStream pubin = new ByteArrayInputStream(
bout1.toByteArray());
ByteArrayInputStream priin = new ByteArrayInputStream(
bout2.toByteArray());
Connection con = father.father.getConnection();
PreparedStatement pst = con
.prepareStatement("insert into MyRSAKeys(name,description,RSApublicKey,RSAprivateKey,create_data) values (?,?,?,?,?)");
pst.setString(1, father.getJTextField3().getText());
pst.setString(2, father.getJTextPane().getText());
pst
.setBinaryStream(3, pubin, pubin
.available());
pst
.setBinaryStream(4, priin, priin
.available());
pst.setString(5, new java.sql.Date(
new java.util.Date().getTime())
.toString());
pst.executeUpdate();
father.father.getJTextPane().getDocument().insertString(father.father.getJTextPane().getDocument().getLength(),"(" + new java.util.Date() + ":) 以添加记录,名称为"+father.getJTextField3().getText()+"\n", father.father.sas);
father.father.jTable=father.father.freshJTable();
father.dispose();
} catch (Exception e1) {
JOptionPane.showMessageDialog(father, e
.toString(), "错误:",
JOptionPane.ERROR_MESSAGE);
}
} else
JOptionPane.showMessageDialog(father, "密钥不正确,请生成密钥!", "错误:",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new java.awt.Rectangle(365, 270, 67, 21));
jButton2.setText("取消");
jButton2.addActionListener(new MyB3ActionListener(this));
}
return jButton2;
}
private class MyB3ActionListener implements java.awt.event.ActionListener {
private AddRSAKeyDialog father;
public MyB3ActionListener(AddRSAKeyDialog ft) {
super();
father = ft;
}
public void actionPerformed(java.awt.event.ActionEvent e) {
father.dispose();
}
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -