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

📄 myframe.java

📁 计算机安全的实验:加密
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package czl;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyAgreement;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MyFrame extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	JMenuBar menuBar = new JMenuBar();
	JMenu operaters = new JMenu("Operaters");
	JMenuItem digest = new JMenuItem("Digest");
	JMenuItem signature = new JMenuItem("Signature");
	JMenuItem cipher = new JMenuItem("DESCipher");
	JMenu dhProtocol = new JMenu("DHProtocol");
	JMenuItem bobOperation=new JMenuItem("BobOperation");
	JMenuItem aliceOperation=new JMenuItem("AliceOperation");
	JPanel panel = new JPanel();

	public MyFrame() {
		this.setTitle("加密系统的设计实现");
		menuBar.add(operaters);
		operaters.add(digest);
		operaters.add(signature);
		operaters.add(cipher);
		operaters.add(dhProtocol);
		dhProtocol.add(aliceOperation);
		dhProtocol.add(bobOperation);
		
		bobOperation.addActionListener(this);
		aliceOperation.addActionListener(this);
		digest.addActionListener(this);
		signature.addActionListener(this);
		cipher.addActionListener(this);
		this.setJMenuBar(menuBar);
		add(panel);
		pack();
	}

	public static void main(String args[]) {
		JFrame frame = new MyFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		remove(panel);
		if (command.equals("Digest")) {
			panel = new MyDigest();
		} else if (command.equals("Signature")) {
			panel = new MySignature();
		} else if (command.equals("DESCipher")) {
			panel = new MyDESCipher();
		} else if (command.equals("BobOperation")) {
			panel = new DH_Bob();
		}else if (command.equals("AliceOperation")) {
			panel = new DH_Alice();
		}
		this.add(panel);
		this.setSize(800, 600);
		this.setVisible(true);
	}
}

class MyDigest extends JPanel implements ActionListener {// 消息摘要
	JTextArea myInfoArea = new JTextArea();
	JButton openMessageFile = new JButton("OpenMessage");// 打开消息
	JButton openDigestFile = new JButton("OpenDigest");// 打开消息摘要
	JButton calcuteDigest = new JButton("Digest");// 计算消息摘要
	JButton compareDigest = new JButton("Compare");// 比较消息摘要
	String message = "";//
	String messageShow = "";//
	String digest = "";//

	public MyDigest() {
		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(myInfoArea), BorderLayout.CENTER);
		JPanel buttons = new JPanel(new FlowLayout());
		buttons.add(openMessageFile);
		buttons.add(calcuteDigest);
		buttons.add(openDigestFile);
		buttons.add(compareDigest);
		openMessageFile.addActionListener(this);
		openDigestFile.addActionListener(this);
		calcuteDigest.addActionListener(this);
		compareDigest.addActionListener(this);
		this.add(buttons, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command.equals("OpenMessage")) {
			message = openFile("open message");
			myInfoArea.append(messageShow
					+ "************************************\n");
		} else if (command.equals("OpenDigest")) {
			digest = openFile("open digest");
			for (int i = 0; i < digest.length(); i++) {
				int k = digest.charAt(i);
				myInfoArea.append(Integer.toHexString(k) + " ");
			}
			myInfoArea.append("\n************************************\n");
		} else if (command.equals("Digest")) {
			digest = getDigest().toString();
			/*
			 * for(int i=0;i<digest.length();i++){ int k=digest.charAt(i);
			 * myInfoArea.append(Integer.toHexString(k)+" "); }
			 * myInfoArea.append("\n");
			 */
			saveFile(digest,"save digest");
		} else if (command.equals("Compare")) {
			String otherDigest = openFile("Open Digest");
			if (compare(otherDigest)) {
				JOptionPane.showMessageDialog(this, "消息摘要相同!");
			} else {
				JOptionPane.showMessageDialog(this, "消息摘要不相同!");
			}
		}
	}

	private boolean compare(String otherDigest) {
		return otherDigest.equals(digest);
	}

	private byte[] getDigest() {
		MessageDigest mda = null;
		try {
			mda = MessageDigest.getInstance("SHA-1");
		} catch (NoSuchAlgorithmException e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
			return null;
		}
		mda.update(message.getBytes());
		return mda.digest();
	}

	private String openFile(String label) {
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("."));
		chooser.setDialogTitle(label);
		int i = chooser.showOpenDialog(this);
		StringBuffer s = new StringBuffer();
		StringBuffer s1 = new StringBuffer();
		if (i == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			try {
				FileReader reader = new FileReader(file);
				Scanner scan = new Scanner(reader);
				while (scan.hasNextLine()) {
					String str = scan.nextLine();
					s.append(str);
					s1.append(str + "\n");
				}
				scan.close();
			} catch (FileNotFoundException e) {
				JOptionPane.showMessageDialog(this, e.getMessage());
				return "";
			}
		}
		messageShow = s1.toString();
		return s.toString();
	}

	private void saveFile(String string,String label) {
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("."));
		chooser.setDialogTitle(label);
		int i = chooser.showSaveDialog(this);
		if (i == JFileChooser.APPROVE_OPTION) {
			PrintWriter pw = null;
			try {
				pw = new PrintWriter(chooser.getSelectedFile());
				for (int j = 0; j < digest.length(); j++) {
					pw.append(digest.charAt(j));
				}
				pw.flush();
				pw.close();
			} catch (FileNotFoundException e) {
				JOptionPane.showMessageDialog(this, e.getMessage());
				pw.close();
			}
		}
	}
}

class MySignature extends JPanel implements ActionListener {// 数字签名
	JTextArea myInfoArea = new JTextArea();
	String messageShow=null;
	String message=null;
	byte[] signed=null;
	JButton getKeyPair = new JButton("GetKPair");// 生成密钥对并保存到文件
	JButton openPublicKey = new JButton("OpenPK");// 导入公钥
	JButton openPrivateKey = new JButton("OpenRK");// 导入私钥
	JButton openMessage=new JButton("OpenMessage");//
	JButton genMSignature = new JButton("GenMS");// 对消息进行数字签名
	JButton verifyMSignature = new JButton("VerifyMS");// 校验消息签名
	PublicKey pk=null;
	PrivateKey rk=null;
	public MySignature() {
		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(myInfoArea), BorderLayout.CENTER);
		JPanel buttons = new JPanel(new FlowLayout());
		buttons.add(getKeyPair);
		buttons.add(openPublicKey);
		buttons.add(openPrivateKey);
		buttons.add(openMessage);
		buttons.add(genMSignature);
		buttons.add(verifyMSignature);
		getKeyPair.addActionListener(this);
		openPublicKey.addActionListener(this);
		openPrivateKey.addActionListener(this);
		genMSignature.addActionListener(this);
		verifyMSignature.addActionListener(this);
		openMessage.addActionListener(this);
		this.add(buttons, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command.equals("GetKPair")) {
			getKPair("DSA");
		} else if (command.equals("OpenPK")) {
			getPK();
		} else if (command.equals("OpenRK")) {
			getRK();
		} else if (command.equals("OpenMessage")) {
			message=openFile("open message");
			myInfoArea.append(messageShow);
		} else if (command.equals("GenMS")) {
			genMS();
		} else if (command.equals("VerifyMS")) {
			verifyMS();
		} 
	}

	private void verifyMS() {
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("."));
		int i = chooser.showOpenDialog(this);
		chooser.setDialogTitle("get signature");
		if (i == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			try {
				ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
				signed=(byte[])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());
			}
		}
		//signed=openFile().getBytes();
		//System.out.println(signed);////
		java.security.Signature signetcheck;
		try {
			signetcheck = java.security.Signature.getInstance("DSA");
			signetcheck.initVerify(pk);//初始一个Signature对象,
			signetcheck.update(message.getBytes()); 
			if (signetcheck.verify(signed)) //并用公钥和签名进行验证
				JOptionPane.showMessageDialog(this, "签名正常!");
			else JOptionPane.showMessageDialog(this, "签名不正常!");
		} catch (NoSuchAlgorithmException e) {
			JOptionPane.showMessageDialog(this, "1"+e.getMessage());
		} catch (InvalidKeyException e) {
			JOptionPane.showMessageDialog(this, "2"+e.getMessage());
		} catch (SignatureException e) {
			JOptionPane.showMessageDialog(this, "3"+e.getMessage());
		} 
		
	}

	private void genMS() {
		Signature signet;
		try {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -