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

📄 ass1.java

📁 Java实现传统算法加密解密
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * 
 * @author Neng
 * 
 */
public class Ass1 extends JFrame implements ActionListener {

	private JLabel head;
	private JLabel hint1;
	// text field to input file name
	private JTextField fileText;

	private JLabel hint2;
	// text field to input key for algorithm
	private JTextField keyText;

	private JComboBox cb;
	private JButton encryptb;
	private JButton decryptb;

	private File inFile;

	private int choice;
	private String key;
	private Monoalphabetic m1;
	private Vigenere v1;
	private Playfair p1;

	public Ass1() {
		m1 = new Monoalphabetic();
		v1 = new Vigenere();
		p1 = new Playfair();

		head = new JLabel("                    Cryptographic Ciphers by Neng");

		hint1 = new JLabel("Please input the path of you fileText:");
		fileText = new JTextField(
				"For example:  C:/floder1/floder2/fileName.txt");

		hint2 = new JLabel("Please input key:");
		keyText = new JTextField("");

		cb = new JComboBox();
		cb.addItem("Choose an algorithm");
		cb.addItem("Monoalphabetic");
		cb.addItem("Polyalphabetic");
		cb.addItem("Playfair");
		cb.addActionListener(this);

		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(5, 1));
		p1.add(hint1);
		p1.add(fileText);
		p1.add(hint2);
		p1.add(keyText);
		p1.add(cb);

		encryptb = new JButton("Encrypt");
		decryptb = new JButton("Decrypt");
		encryptb.addActionListener(this);
		decryptb.addActionListener(this);
		JPanel p2 = new JPanel();
		p2.add(encryptb);
		p2.add(decryptb);

		this.add(head, "North");
		this.add(p1, "Center");
		this.add(p2, "South");
		this.setSize(400, 250);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getPreferredSize();
		setLocation(screenSize.width / 2 - (frameSize.width / 2),
				screenSize.height / 2 - (frameSize.height / 2));
		this.setDefaultCloseOperation(3);
		this.setVisible(true);

	}

	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == cb)
			choice = cb.getSelectedIndex();

		if (ae.getSource() == encryptb) {
			key = keyText.getText();

			try {
				String filename = fileText.getText();
				inFile = new File(filename);
				if (choice == 0)
					JOptionPane.showMessageDialog(null,
							"Please choose an algorithm.");
				else if (choice == 1)
					m1.encrypt(inFile);
				else {
					if (keyText.getText().equals(""))
						JOptionPane.showMessageDialog(null,
						"Please input the key");
					else {
						if (choice == 2)
							v1.encrypt(inFile, key);
						else if (choice == 3)
							p1.encrypt(inFile, key);

					}
				}
			} catch (Exception e) {
				System.err.println(e.toString());
			}

		}
		if (ae.getSource() == decryptb) {
			try {
				if (choice == 0)
					JOptionPane.showMessageDialog(null,
							"Please choose an algorithm.");
				if (choice == 1)
					m1.decrypt();
				else if (choice == 2)
					v1.decrypt();
				else if (choice == 3)
					p1.decrypt();

			} catch (Exception e) {
				System.err.println(e.toString());
			}

		}

	}

	public static void main(String[] args) throws Exception {

		Ass1 as = new Ass1();

	}

}

⌨️ 快捷键说明

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