Single

TXT文本的加解密

半年前的作品,留给有缘人。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class TXT {
	static String PassWord = "";

	public static void main(String[] args) {
		GUI();
	}

	static JFrame jfm = new JFrame("文本文件的加密与解密");
	private JButton encryption, decrypt;

	public static void GUI() {

		jfm.setSize(700, 600);
		jfm.setLocationRelativeTo(null);
		jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JLabel tips = new JLabel();
		tips.setBounds(50, 350, 600, 50);
		jfm.add(tips);
		JFileChooser jfmfile = new JFileChooser();
		// 功能区
		JPanel operation = new JPanel();
		operation.setBounds(100, 450, 500, 50);
		jfm.add(operation);
		jfm.setLayout(null);
		GridLayout grid = new GridLayout(1, 3, 10, 10);
		operation.setLayout(grid);
		JLabel tip = new JLabel("请输入加密密码");
		JButton encryption = new JButton("加密");
		encryption.setFont(new Font("Dialog",1,15));

		JButton decrypt = new JButton("解密");
		decrypt.setFont(new Font("Dialog",1,15));
		JPasswordField password = new JPasswordField();
		password.setSize(100, 50);
		operation.add(tip);
		operation.add(password);
		operation.add(encryption);
		operation.add(decrypt);
		// 文本区
		JTextArea text = new JTextArea();
		text.setBounds(50, 50, 600, 300);
		jfm.add(text);
		// 加密操作
		encryption.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				char[] pass = password.getPassword();
				String key = new String(pass);
				String str = text.getText();
				byte[] b = encryption(str, key);
				String txt = new String(b);
				text.setText(txt);
			}
		});
		// 解密操作
		decrypt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				char[] pass = password.getPassword();
				String key = new String(pass);
				String str = text.getText();
				byte[] b = decrypt(str, key);
				String txt = new String(b);
				text.setText(txt);
			}
		});
		// 菜单栏
		JMenuBar menu = new JMenuBar();
		JMenu file = new JMenu("文件");
		JMenuItem openfile = new JMenuItem("打开文件");
		JMenuItem savefile = new JMenuItem("保存文件");
		file.add(openfile);
		file.add(savefile);
		// 打开文件事件
		openfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				// 文件过滤器 待优化

				int status = jfmfile.showOpenDialog(jfmfile);
				if (status != JFileChooser.APPROVE_OPTION)
					text.setText("没有打开文件");
				else {
					try {
						File file1 = jfmfile.getSelectedFile();
						Scanner scan = new Scanner(file1);
						String info = "";
						String state = scan.nextLine();
						if (state.equals("state=1")) {
							int state1 = 1;
							PassWord = scan.nextLine();
							byte[] Key = PassWord.getBytes();
							for (int i = 0; i < Key.length; i++) {
								Key[i] = (byte) (Key[i] ^ 1);
							}
							PassWord = new String(Key);
							String date = scan.nextLine();
							String inputValue = JOptionPane.showInputDialog(jfm, "请输入打开密码", "请输入密码", 0);
							if (PassWord.equals(inputValue)) {
								while (scan.hasNext()) {

									String str = scan.nextLine();

									info += str;
								}
								text.setText(info);
								password.setText(PassWord);
								tips.setText("上次加密时间为" + date);
							} else {
								JOptionPane.showMessageDialog(null, "密码错误,无法打开文件!", "密码错误", JOptionPane.ERROR_MESSAGE);
							}
						} else {
							String str = state;
							info += str + "\r\n";
							while (scan.hasNext()) {

								str = scan.nextLine();

								info += str + "\r\n";
							}
							text.setText(info);
						}

					} catch (FileNotFoundException ioe) {
					}
					;
				}
			}
		});

		// 保存文件事件
		savefile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int re = jfmfile.showSaveDialog(jfmfile);
				if (re == JFileChooser.APPROVE_OPTION) {
					try {
						File file2 = jfmfile.getSelectedFile();
						FileOutputStream f = new FileOutputStream(file2);
						BufferedOutputStream out = new BufferedOutputStream(f);
						String state = "state=1";
						char[] temp = password.getPassword();
						String filepass = new String(temp);
						byte[] key = filepass.getBytes();
						for (int i = 0; i < key.length; i++) {
							key[i] = (byte) (key[i] ^ 1);
						}
						filepass = new String(key);
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						String time = format.format(new Date());
						String file = state + "\r\n" + filepass + "\r\n" + time + "\r\n" + text.getText();
						byte[] b = file.getBytes();
						out.write(b, 0, b.length);
						out.close();
					} catch (IOException ioe) {
					}
					;
				}
			}
		});
		menu.add(file);
		jfm.setJMenuBar(menu);
		jfm.setResizable(false);
		jfm.setVisible(true);

	}

	// 加密
	private static byte[] encryption(String str, String key) {
		byte[] b = str.getBytes();
		if (key.equals("")) {
			JOptionPane.showMessageDialog(null, "未输入密码,无法加密文件!", "未输入密码", JOptionPane.ERROR_MESSAGE);
		} else {
			b = str.getBytes();
			for (int i = 0; i < b.length; i++) {
				b[i] = (byte) (b[i] ^ i);
			}
		}
		return b;
	}

	// 解密
	private static byte[] decrypt(String str, String key) {
		byte[] b = str.getBytes();
		if (key.equals("")) {
			JOptionPane.showMessageDialog(null, "未输入密码,无法解密文件!", "未输入密码", JOptionPane.ERROR_MESSAGE);
		} else {
			b = str.getBytes();
			for (int i = 0; i < b.length; i++) {
				b[i] = (byte) (b[i] ^ i);
			}
		}
		return b;
	}
}

暂无评论

发表评论