生成密码和解密密码的小工具。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.2 KiB

import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-07-08
*/
public class PasswordManager extends JFrame {
public static void main(String... args) {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
PasswordManager passwordManager = new PasswordManager();
passwordManager.setSize(800, 400);
passwordManager.setVisible(true);
passwordManager.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
centerWindow(passwordManager);
}
private static void centerWindow(Window win) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension winSize = win.getSize();
if (winSize.height > screenSize.height) {
winSize.height = screenSize.height;
}
if (winSize.width > screenSize.width) {
winSize.width = screenSize.width;
}
win.setLocation((screenSize.width - winSize.width) / 2, (screenSize.height - winSize.height) / 2 - 20);
}
public PasswordManager() {
setTitle("9.0/10.0密码加密解密帮助工具");
final JPanel panel = (JPanel) getContentPane();
panel.setLayout(new BorderLayout());
final JTextArea inputTextArea = new JTextArea(5, 20);
JPanel inputPane = new JPanel(new BorderLayout());
inputPane.add(inputTextArea, BorderLayout.CENTER);
JButton transferButton = new JButton("加密/解密");
inputPane.add(transferButton, BorderLayout.EAST);
final JTextField inputFileTextField = new JTextField(20);
JPanel filePane = new JPanel(new BorderLayout());
filePane.add(inputFileTextField, BorderLayout.CENTER);
JButton button = new JButton("...");
filePane.add(button, BorderLayout.EAST);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r = jFileChooser.showOpenDialog(PasswordManager.this);
if (r == JFileChooser.APPROVE_OPTION) {
File file = jFileChooser.getSelectedFile();
if (file != null) {
inputFileTextField.setText(file.getAbsolutePath());
}
}
}
});
final JComboBox<String> versionComboBox = new JComboBox<>(new DefaultComboBoxModel<>(new String[]{"10.0", "9.0"}));
versionComboBox.setSelectedIndex(0);
final JTextArea outputTextArea = new JTextArea(5, 20);
transferButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String version = String.valueOf(versionComboBox.getSelectedItem());
if ("10.0".equals(version)) {
String filePath = inputFileTextField.getText();
File file = new File(filePath);
Password password = null;
if (file.exists()) {
try {
password = new Password(new String(inputStream2Bytes(new FileInputStream(file)), StandardCharsets.UTF_8));
} catch (Exception ex) {
ex.printStackTrace();
password = new Password();
}
} else {
password = new Password();
}
String inputText = inputTextArea.getText();
if (inputText != null && inputText.length() > 40) {
outputTextArea.setText(password.decrypt(inputText));
} else {
outputTextArea.setText(password.encrypt(inputText));
}
} else if ("9.0".equals(version)) {
String inputText = inputTextArea.getText();
if (inputText!= null && inputText.startsWith("___")) {
outputTextArea.setText(Password9.passwordDecode(inputText));
} else {
outputTextArea.setText(Password9.passwordEncode(inputText));
}
}
}
});
double p = TableLayout.PREFERRED;
double f = TableLayout.FILL;
double[] rowSize = new double[]{p, p, p, f};
double[] columnSize = new double[]{p, f};
JComponent[][] comps = new JComponent[][]{
{new JLabel("输入文本:"), inputPane},
{new JLabel("版本信息:"), versionComboBox},
{new JLabel("秘钥文件:"), filePane},
{new JLabel("输出文本:"), outputTextArea}
};
panel.add(TableLayoutHelper.createTableLayoutPane(comps, rowSize, columnSize), BorderLayout.CENTER);
}
public static byte[] inputStream2Bytes(InputStream in) {
if (in == null) {
return new byte[0];
}
byte[] temp = new byte[32 * 1024];
ByteArrayOutputStream bi = new ByteArrayOutputStream();
try {
int count;
while ((count = in.read(temp)) > 0) {
byte[] b4Add;
if (temp.length == count) {
b4Add = temp;
} else {
b4Add = Password.subarray(temp, 0, count);
}
bi.write(b4Add);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bi.toByteArray();
}
}