⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myframe.java

📁 计算机安全的实验:加密
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			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 + -