Java简单文本加解密程序 要求 1、界面上文本框输入文本,或者选择文本文件打开; 2、实现两种以上简单文本加密方法,下拉框选择; 3、点击“加密”按钮,对文本进行加密。如果是文本框输入文本,加密后的文本显示在加密后文本框里;如果是文本文件,加密后文本保存设定好的加密数据文件中; 4、点击“解密”按钮,对加密文本(来源:文本框或文本文件)进行解密; 5、系统具有良好的可扩展性,以便后期可以增加更多的加密方法。
时间: 2024-02-20 13:57:42 浏览: 77
以下是一个Java简单文本加解密程序的实现示例:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
public class TextEncryptor extends JFrame {
private JPanel contentPane;
private JTextArea textAreaPlain;
private JTextArea textAreaCipher;
private JComboBox<String> comboBoxMethod;
private JButton btnEncrypt;
private JButton btnDecrypt;
private File file;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TextEncryptor frame = new TextEncryptor();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TextEncryptor() {
setTitle("Text Encryptor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPanePlain = new JScrollPane();
contentPane.add(scrollPanePlain, BorderLayout.WEST);
textAreaPlain = new JTextArea();
scrollPanePlain.setViewportView(textAreaPlain);
JScrollPane scrollPaneCipher = new JScrollPane();
contentPane.add(scrollPaneCipher, BorderLayout.EAST);
textAreaCipher = new JTextArea();
textAreaCipher.setEditable(false);
scrollPaneCipher.setViewportView(textAreaCipher);
JPanel panelSouth = new JPanel();
contentPane.add(panelSouth, BorderLayout.SOUTH);
comboBoxMethod = new JComboBox<String>();
comboBoxMethod.addItem("Caesar Cipher");
// add more encryption methods here
panelSouth.add(comboBoxMethod);
btnEncrypt = new JButton("Encrypt");
btnEncrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String plainText = textAreaPlain.getText();
String cipherText = "";
String method = (String) comboBoxMethod.getSelectedItem();
if (method.equals("Caesar Cipher")) {
cipherText = caesarCipherEncrypt(plainText);
}
// add more encryption methods here
textAreaCipher.setText(cipherText);
if (file != null) {
saveToFile(cipherText, file.getAbsolutePath() + ".enc");
}
}
});
panelSouth.add(btnEncrypt);
btnDecrypt = new JButton("Decrypt");
btnDecrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cipherText = textAreaCipher.getText();
String plainText = "";
String method = (String) comboBoxMethod.getSelectedItem();
if (method.equals("Caesar Cipher")) {
plainText = caesarCipherDecrypt(cipherText);
}
// add more decryption methods here
textAreaPlain.setText(plainText);
if (file != null) {
saveToFile(plainText, file.getAbsolutePath().replaceAll(".enc$", ""));
}
}
});
panelSouth.add(btnDecrypt);
JButton btnOpen = new JButton("Open");
btnOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt");
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(TextEncryptor.this);
if (result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
textAreaPlain.setText(builder.toString());
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
panelSouth.add(btnOpen);
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text files", "txt");
fileChooser.setFileFilter(filter);
int result = fileChooser.showSaveDialog(TextEncryptor.this);
if (result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
saveToFile(textAreaCipher.getText(), file.getAbsolutePath());
}
}
});
panelSouth.add(btnSave);
}
private String caesarCipherEncrypt(String plainText) {
int shift = 3; // shift value for Caesar Cipher
StringBuilder builder = new StringBuilder();
for (int i = 0; i < plainText.length(); i++) {
char c = plainText.charAt(i);
if (Character.isLetter(c)) {
c = (char) ((c - 'a' + shift) % 26 + 'a');
}
builder.append(c);
}
return builder.toString();
}
private String caesarCipherDecrypt(String cipherText) {
int shift = 3; // shift value for Caesar Cipher
StringBuilder builder = new StringBuilder();
for (int i = 0; i < cipherText.length(); i++) {
char c = cipherText.charAt(i);
if (Character.isLetter(c)) {
c = (char) ((c - 'a' - shift + 26) % 26 + 'a');
}
builder.append(c);
}
return builder.toString();
}
private void saveToFile(String text, String fileName) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(text);
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
```
该程序使用Java Swing库构建GUI界面,并实现了Caesar密码加解密算法。用户可以输入文本或选择文本文件,并选择加密算法进行加密和解密操作。加密后的文本可以保存到文件中。该程序还具有可扩展性,可以添加更多的加密算法。
阅读全文