?? myframe.java
字號:
chooser.setCurrentDirectory(new File("."));
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(deskey);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
class DH_Alice extends JPanel implements ActionListener{
JTextArea myInfoArea = new JTextArea();
JButton kpairGen=new JButton("KpairGen");
JButton desKeyGen=new JButton("DesKeyGen");
JButton openBobPKey=new JButton("OpenBobPKey");
JButton openKpair=new JButton("OpenKpair");
KeyPair aliceKpair=null;
PublicKey bobPubKey=null;
DH_Alice(){
this.setLayout(new BorderLayout());
JPanel buttons=new JPanel();
buttons.add(kpairGen);
buttons.add(openBobPKey);
buttons.add(openKpair);
buttons.add(desKeyGen);
this.add(new JScrollPane(myInfoArea),BorderLayout.CENTER);
this.add(buttons,BorderLayout.SOUTH);
kpairGen.addActionListener(this);
desKeyGen.addActionListener(this);
openBobPKey.addActionListener(this);
openKpair.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("KpairGen")) {
myInfoArea.setText("第一步:Alice產生 DH密鑰對并保存密鑰對及公鑰\n"+
"下一步:Bob從alice發送來的公鑰中讀出DH密鑰對的初始參數生成Bob的DH密鑰對");
KpairGen();
} else if (command.equals("OpenBobPKey")) {
myInfoArea.setText("第五步:Alice讀出bob的公鑰\n"+
"下一步:Alice讀出自己的密鑰對");
openBobPKey();
} else if (command.equals("OpenKpair")) {
myInfoArea.setText("第六步:Alice讀出自己的密鑰對\n"+
"下一步:Alice根據bob的公鑰生成本地的DES密鑰");
openKpair();
} else if(command.equals("DesKeyGen")){
myInfoArea.setText("最后一步:Alice根據bob的公鑰生成本地的DES密鑰\n");
desKeyGen();
}
}
private void desKeyGen() {
KeyAgreement aliceKeyAgree;
try {
aliceKeyAgree = KeyAgreement.getInstance("DH");
aliceKeyAgree.init(aliceKpair.getPrivate());
aliceKeyAgree.doPhase(bobPubKey, true);
SecretKey aliceDesKey = aliceKeyAgree.generateSecret("DES");
myInfoArea.append("aliceDesKey: "+Arrays.toString(aliceDesKey.getEncoded()));
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("aliceDesKeyGen");
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(aliceDesKey);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
private void openKpair() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int i = chooser.showOpenDialog(this);
chooser.setDialogTitle("Open Alice KeyPair");
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
aliceKpair=(KeyPair)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
private void openBobPKey() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int i = chooser.showOpenDialog(this);
chooser.setDialogTitle("open bob public key");
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
bobPubKey=(PublicKey)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
private void KpairGen() {
KeyPairGenerator aliceKpairGen;
try {
aliceKpairGen = KeyPairGenerator.getInstance("DH");
aliceKpairGen.initialize(512);
KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("SaveAliceKeyPair");
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(aliceKpair);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
PublicKey alicePubKeyEnc = aliceKpair.getPublic();
chooser.setDialogTitle("SaveAlicePublicKey");
i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(alicePubKeyEnc);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
} catch (NoSuchAlgorithmException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
class DH_Bob extends JPanel implements ActionListener{
JTextArea myInfoArea = new JTextArea();
JButton openPKey=new JButton("OpenPKey");//open alice's public key
JButton KpairGen=new JButton("BobKpairGen");//從alice發送來的公鑰中讀出DH密鑰對的初始參數生成Bob的DH密鑰對并保存自己的公鑰。
JButton DesKeyGen=new JButton("DesKeyGen");//Alice的公鑰生成本地的DES密鑰
PublicKey alicePKey=null;
KeyPair bobKpair=null;
DH_Bob(){
this.setLayout(new BorderLayout());
JPanel buttons=new JPanel();
buttons.add(openPKey);
buttons.add(KpairGen);
buttons.add(DesKeyGen);
this.add(new JScrollPane(myInfoArea),BorderLayout.CENTER);
this.add(buttons,BorderLayout.SOUTH);
openPKey.addActionListener(this);
KpairGen.addActionListener(this);
DesKeyGen.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("OpenPKey")) {
myInfoArea.setText("第二步:Bob讀出alice發送來的公鑰\n"+
"下一步:Bob從alice發送來的公鑰中讀出DH密鑰對的初始參數生成Bob的DH密鑰對");
openKey();
} else if (command.equals("BobKpairGen")) {
myInfoArea.setText("第三步:Bob從alice發送來的公鑰中讀出DH密鑰對的初始參數生成Bob的DH密鑰對并保存自己的public key\n"+
"下一步:Bob根據Alice的公鑰生成本地的DES密鑰");
bobKpairGen();
} else if (command.equals("DesKeyGen")) {
myInfoArea.setText("第四步:Bob根據Alice的公鑰生成本地的DES密鑰\n"+
"下一步:Alice根據bob的公鑰生成本地的DES密鑰");
desKeyGen();
}
}
private void desKeyGen() {
KeyAgreement bobKeyAgree;
try {
bobKeyAgree = KeyAgreement.getInstance("DH");
bobKeyAgree.init(bobKpair.getPrivate());
bobKeyAgree.doPhase(alicePKey, true);
SecretKey bobDesKey = bobKeyAgree.generateSecret("DES");
myInfoArea.append("bobDesKey: "+Arrays.toString(bobDesKey.getEncoded()));
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("save bob DesKey");
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(bobDesKey);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
} catch (NoSuchAlgorithmException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (InvalidKeyException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void bobKpairGen() {
DHParameterSpec dhParamSpec = ((DHPublicKey)alicePKey).getParams();
KeyPairGenerator bobKpairGen;
try {
bobKpairGen = KeyPairGenerator.getInstance("DH");
bobKpairGen.initialize(dhParamSpec);
bobKpair = bobKpairGen.generateKeyPair();
PublicKey bobPKey=bobKpair.getPublic();
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("Save Bob Public Key");
int i = chooser.showSaveDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(
new FileOutputStream(chooser.getSelectedFile()));
oos.writeObject(bobPKey);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
} catch (NoSuchAlgorithmException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void openKey() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setDialogTitle("open alice public key");
int i = chooser.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
alicePKey=(PublicKey)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -