Anner
4 years ago
20 changed files with 341 additions and 569 deletions
@ -0,0 +1,36 @@
|
||||
package com.fr.password.bean; |
||||
|
||||
/** |
||||
* desc |
||||
* |
||||
* @author Anner |
||||
* created on 2020-11-10 |
||||
*/ |
||||
public class RequestBean { |
||||
private String key; |
||||
private String text; |
||||
|
||||
public RequestBean() { |
||||
} |
||||
|
||||
public RequestBean(String key, String text) { |
||||
this.key = key; |
||||
this.text = text; |
||||
} |
||||
|
||||
public String getKey() { |
||||
return key; |
||||
} |
||||
|
||||
public void setKey(String key) { |
||||
this.key = key; |
||||
} |
||||
|
||||
public String getText() { |
||||
return text; |
||||
} |
||||
|
||||
public void setText(String text) { |
||||
this.text = text; |
||||
} |
||||
} |
@ -1,9 +0,0 @@
|
||||
package com.fr.password.tool; |
||||
|
||||
import com.fr.password.tool.ui.MainFrame; |
||||
|
||||
public class Runner { |
||||
public static void main(String[] args) { |
||||
new MainFrame(); |
||||
} |
||||
} |
@ -1,101 +0,0 @@
|
||||
package com.fr.password.tool.ui; |
||||
|
||||
import cn.hutool.core.io.FileUtil; |
||||
import cn.hutool.core.util.StrUtil; |
||||
import com.fr.password.tool.factory.SecretHelper; |
||||
|
||||
import javax.swing.*; |
||||
import java.awt.event.ActionEvent; |
||||
import java.awt.event.ActionListener; |
||||
import java.io.File; |
||||
|
||||
public class FilePanel extends JPanel { |
||||
public final static FilePanel INSTANCE = new FilePanel(); |
||||
|
||||
private JLabel fileLabel = new JLabel(); |
||||
private JTextField filePathField = new JTextField(); |
||||
private JButton chooseFileButton = new JButton(); |
||||
|
||||
private EventHandler eventHandler = new EventHandler(); |
||||
|
||||
private FilePanel() { |
||||
chooseFileButton.addActionListener(eventHandler); |
||||
|
||||
fileLabel.setText(FrameConstants.SAVE_KEY); |
||||
add(fileLabel); |
||||
|
||||
filePathField.setColumns(28); |
||||
add(filePathField); |
||||
|
||||
chooseFileButton.setText(FrameConstants.CHOOSE); |
||||
add(chooseFileButton); |
||||
} |
||||
|
||||
public void hide() { |
||||
this.fileLabel.setVisible(false); |
||||
this.filePathField.setVisible(false); |
||||
this.chooseFileButton.setVisible(false); |
||||
} |
||||
|
||||
public void setLeftLabelText(String text) { |
||||
this.fileLabel.setText(text); |
||||
} |
||||
|
||||
public void setButtonText(String text) { |
||||
this.chooseFileButton.setText(text); |
||||
} |
||||
|
||||
public String getFilePath(){ |
||||
return filePathField.getText(); |
||||
} |
||||
|
||||
|
||||
class EventHandler implements ActionListener { |
||||
|
||||
@Override |
||||
public void actionPerformed(ActionEvent e) { |
||||
if (!TopPanel.INSTANCE.isChooseEncryption()) { |
||||
return; |
||||
} |
||||
String publicKey = StrUtil.EMPTY; |
||||
String privateKey = StrUtil.EMPTY; |
||||
JFileChooser fileChooser = new JFileChooser(); |
||||
fileChooser.setVisible(true); |
||||
// 保存密钥文件
|
||||
if (e.getSource() == chooseFileButton && fileLabel.getText().equals(FrameConstants.SAVE_KEY)) { |
||||
if (StrUtil.isEmpty(TextPanel.INSTANCE.getLeftText()) || StrUtil.isEmpty(TextPanel.INSTANCE.getRightText())) { |
||||
return; |
||||
} |
||||
fileChooser.setApproveButtonText(FrameConstants.SAVE); |
||||
fileChooser.setDialogTitle(FrameConstants.CHOOSE_SAVE_DIR); |
||||
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
||||
publicKey = TextPanel.INSTANCE.getLeftText(); |
||||
privateKey = TextPanel.INSTANCE.getRightText(); |
||||
String content = StrUtil.format("{}\r\n{}\r\n{}\r\n{}\r\n{}\r\n{}", |
||||
SecretHelper.BEGIN_PUBLIC_KEY, publicKey, SecretHelper.END_PUBLIC_KEY, |
||||
SecretHelper.BEGIN_PRIVATE_KEY, privateKey, SecretHelper.END_PRIVATE_KEY); |
||||
int result = fileChooser.showOpenDialog(FilePanel.INSTANCE); |
||||
if (result == JFileChooser.APPROVE_OPTION) { |
||||
String dirPath = fileChooser.getSelectedFile().getAbsolutePath(); |
||||
String keyFilePath = StrUtil.format("{}{}{}", dirPath, File.separator, SecretHelper.KEY_FILE_NAME); |
||||
filePathField.setText(keyFilePath); |
||||
FileUtil.writeUtf8String(content, keyFilePath); |
||||
} |
||||
} |
||||
|
||||
// 选择密钥文件
|
||||
if (e.getSource() == chooseFileButton && fileLabel.getText().equals(FrameConstants.CHOOSE_FILE)) { |
||||
fileChooser.setApproveButtonText(FrameConstants.CONFIRM); |
||||
fileChooser.setDialogTitle(FrameConstants.CHOOSE_KEY_DIR); |
||||
int result = fileChooser.showOpenDialog(FilePanel.INSTANCE); |
||||
if (result == JFileChooser.APPROVE_OPTION) { |
||||
String keyFilePath = fileChooser.getSelectedFile().getAbsolutePath(); |
||||
filePathField.setText(keyFilePath); |
||||
// TODO: 密钥正确性检测
|
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
} |
@ -1,35 +0,0 @@
|
||||
package com.fr.password.tool.ui; |
||||
|
||||
/** |
||||
* 主面板的常量 |
||||
*/ |
||||
public class FrameConstants { |
||||
public static final String TITLE = "加解密小工具"; |
||||
public static final String GENERATE = "密钥生成界面"; |
||||
public static final String ENCRYPT = "加密界面"; |
||||
public static final String DECRYPT = "解密界面"; |
||||
public static final String RUN_ENCRYPT = "开始加密"; |
||||
public static final String RUN_DECRYPT = "开始解密"; |
||||
public static final String CHOOSE_ENCRYPTION = "选择算法:"; |
||||
public static final String SM2 = "国密算法"; |
||||
public static final String RSA = "RSA"; |
||||
public static final String WAIT_CHOOSE = "--请选择--"; |
||||
public static final String CHOOSE_FILE = "密钥文件:"; |
||||
public static final String PUBLIC_KEY = "公钥"; |
||||
public static final String PRIVATE_KEY = "私钥"; |
||||
public static final String CHOOSE_SAVE_DIR ="选择保存的目录"; |
||||
public static final String CHOOSE_KEY_DIR ="选择密钥文件"; |
||||
public static final String CONFIRM ="确定"; |
||||
public static final String SAVE_KEY ="存储密钥:"; |
||||
public static final String SAVE ="保存"; |
||||
public static final String CHOOSE ="选择"; |
||||
public static final String GENERATE_KEY ="开始生成密钥"; |
||||
public static final String PLAIN_TEXT ="明文"; |
||||
public static final String CIPHER_TEXT ="密文"; |
||||
public static final Integer WIDTH = 500; |
||||
public static final Integer HEIGHT = 560; |
||||
public static final Integer FLOW_WIDTH = 220; |
||||
public static final Integer CHOICE_FLOW_WIDTH = 5; |
||||
public static final Integer TEXT_AREA_HEIGHT = 25; |
||||
public static final Integer TEXT_AREA_WIDTH = 20; |
||||
} |
@ -1,31 +0,0 @@
|
||||
package com.fr.password.tool.ui; |
||||
|
||||
import javax.swing.*; |
||||
import java.awt.*; |
||||
|
||||
public class MainFrame extends JFrame { |
||||
|
||||
public MainFrame() { |
||||
initInterface(); |
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
||||
setVisible(true); |
||||
} |
||||
|
||||
/** |
||||
* 初始化主界面的默认布局 |
||||
*/ |
||||
private void initInterface() { |
||||
setTitle(FrameConstants.TITLE); |
||||
setSize(FrameConstants.WIDTH, FrameConstants.HEIGHT); |
||||
setLayout(new BorderLayout()); |
||||
setResizable(false); |
||||
// 默认是生成密钥的界面
|
||||
chooseGenerateKeys(); |
||||
} |
||||
|
||||
private void chooseGenerateKeys(){ |
||||
add(TopPanel.INSTANCE, BorderLayout.NORTH); |
||||
add(TipPanel.INSTANCE,BorderLayout.CENTER); |
||||
add(TextPanel.INSTANCE,BorderLayout.SOUTH); |
||||
} |
||||
} |
@ -1,46 +0,0 @@
|
||||
package com.fr.password.tool.ui; |
||||
|
||||
import javax.swing.*; |
||||
import java.awt.*; |
||||
|
||||
public class TextPanel extends JPanel { |
||||
public final static TextPanel INSTANCE = new TextPanel(); |
||||
|
||||
private JTextArea leftTextArea = new JTextArea(); |
||||
private JTextArea rightTextArea = new JTextArea(); |
||||
|
||||
public TextPanel() { |
||||
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT); |
||||
flowLayout.setVgap(FrameConstants.CHOICE_FLOW_WIDTH); |
||||
setLayout(flowLayout); |
||||
|
||||
JScrollPane leftJScrollPane = new JScrollPane(leftTextArea); |
||||
JScrollPane rightJScrollPane = new JScrollPane(rightTextArea); |
||||
leftTextArea.setColumns(FrameConstants.TEXT_AREA_WIDTH); |
||||
leftTextArea.setRows(FrameConstants.TEXT_AREA_HEIGHT); |
||||
rightTextArea.setColumns(FrameConstants.TEXT_AREA_WIDTH); |
||||
rightTextArea.setRows(FrameConstants.TEXT_AREA_HEIGHT); |
||||
|
||||
leftJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
||||
rightJScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
||||
|
||||
add(leftJScrollPane); |
||||
add(rightJScrollPane); |
||||
} |
||||
|
||||
public void setLeftText(String text) { |
||||
this.leftTextArea.setText(text); |
||||
} |
||||
|
||||
public void setRightText(String text) { |
||||
this.rightTextArea.setText(text); |
||||
} |
||||
|
||||
public String getLeftText() { |
||||
return leftTextArea.getText(); |
||||
} |
||||
|
||||
public String getRightText() { |
||||
return rightTextArea.getText(); |
||||
} |
||||
} |
@ -1,32 +0,0 @@
|
||||
package com.fr.password.tool.ui; |
||||
|
||||
import javax.swing.*; |
||||
import java.awt.*; |
||||
|
||||
public class TipPanel extends JPanel { |
||||
public final static TipPanel INSTANCE = new TipPanel(); |
||||
|
||||
private JLabel leftLabel = new JLabel(); |
||||
private JLabel rightLabel = new JLabel(); |
||||
|
||||
|
||||
private TipPanel() { |
||||
FlowLayout flowLayout = new FlowLayout(); |
||||
flowLayout.setHgap(FrameConstants.FLOW_WIDTH); |
||||
setLayout(flowLayout); |
||||
add(FilePanel.INSTANCE); |
||||
|
||||
this.leftLabel.setText(FrameConstants.PUBLIC_KEY); |
||||
this.rightLabel.setText(FrameConstants.PRIVATE_KEY); |
||||
add(leftLabel); |
||||
add(rightLabel); |
||||
} |
||||
|
||||
public void setLeftLabel(String text) { |
||||
this.leftLabel.setText(text); |
||||
} |
||||
|
||||
public void setRightLabel(String text) { |
||||
this.rightLabel.setText(text); |
||||
} |
||||
} |
@ -1,183 +0,0 @@
|
||||
package com.fr.password.tool.ui; |
||||
|
||||
import cn.hutool.core.util.StrUtil; |
||||
import com.fr.password.tool.factory.SecretHelper; |
||||
import com.fr.password.tool.keys.RSAKeysHandler; |
||||
import com.fr.password.tool.keys.SM2KeysHandler; |
||||
import com.fr.password.tool.util.EncodeUtil; |
||||
import com.fr.password.tool.util.rsa.RSAUtil; |
||||
import com.fr.password.tool.util.smx.SM2KeyPair; |
||||
import com.fr.password.tool.util.smx.SM2Util; |
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters; |
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters; |
||||
|
||||
import javax.swing.*; |
||||
import java.awt.*; |
||||
import java.awt.event.ActionEvent; |
||||
import java.awt.event.ActionListener; |
||||
import java.io.IOException; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.KeyPair; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.security.PrivateKey; |
||||
import java.security.PublicKey; |
||||
import java.util.Map; |
||||
|
||||
public class TopPanel extends JPanel { |
||||
public final static TopPanel INSTANCE = new TopPanel(); |
||||
|
||||
private JButton runButton = new JButton(); |
||||
private JComboBox chooseComboBox = new JComboBox(); |
||||
private JComboBox interfaceComboBox = new JComboBox(); |
||||
|
||||
private EventHandler eventHandler = new EventHandler(); |
||||
|
||||
private TopPanel() { |
||||
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT); |
||||
flowLayout.setVgap(FrameConstants.CHOICE_FLOW_WIDTH); |
||||
flowLayout.setHgap(FrameConstants.CHOICE_FLOW_WIDTH); |
||||
setLayout(flowLayout); |
||||
|
||||
interfaceComboBox.addItem(FrameConstants.GENERATE); |
||||
interfaceComboBox.addItem(FrameConstants.ENCRYPT); |
||||
interfaceComboBox.addItem(FrameConstants.DECRYPT); |
||||
interfaceComboBox.addActionListener(eventHandler); |
||||
add(interfaceComboBox); |
||||
|
||||
JLabel chooseLabel = new JLabel(FrameConstants.CHOOSE_ENCRYPTION); |
||||
chooseComboBox.addItem(FrameConstants.WAIT_CHOOSE); |
||||
chooseComboBox.addItem(FrameConstants.RSA); |
||||
chooseComboBox.addItem(FrameConstants.SM2); |
||||
add(chooseLabel); |
||||
add(chooseComboBox); |
||||
|
||||
runButton.setText(FrameConstants.GENERATE_KEY); |
||||
runButton.addActionListener(eventHandler); |
||||
add(runButton); |
||||
|
||||
setBackground(Color.lightGray); |
||||
} |
||||
|
||||
public void setRunButtonText(String text) { |
||||
this.runButton.setText(text); |
||||
} |
||||
|
||||
public boolean isChooseEncryption() { |
||||
return chooseComboBox.getSelectedIndex() != 0; |
||||
} |
||||
|
||||
public String getChooseEncryption() { |
||||
return (String) chooseComboBox.getSelectedItem(); |
||||
} |
||||
|
||||
class EventHandler implements ActionListener { |
||||
|
||||
@Override |
||||
public void actionPerformed(ActionEvent e) { |
||||
// 生成密钥界面 (使用默认的页面)
|
||||
if (interfaceComboBox.getSelectedIndex() == 0) { |
||||
runButton.setText(FrameConstants.GENERATE_KEY); |
||||
FilePanel.INSTANCE.setLeftLabelText(FrameConstants.SAVE_KEY); |
||||
FilePanel.INSTANCE.setButtonText(FrameConstants.CHOOSE); |
||||
TipPanel.INSTANCE.setLeftLabel(FrameConstants.PUBLIC_KEY); |
||||
TipPanel.INSTANCE.setRightLabel(FrameConstants.PRIVATE_KEY); |
||||
if (e.getSource() != runButton || !isChooseEncryption()) { |
||||
return; |
||||
} |
||||
// 获取加密方式
|
||||
String encryptionType = (String) chooseComboBox.getSelectedItem(); |
||||
String publicKeyText = StrUtil.EMPTY; |
||||
String privateKeyText = StrUtil.EMPTY; |
||||
if (encryptionType.equals(FrameConstants.RSA)) { |
||||
try { |
||||
KeyPair keyPair = RSAKeysHandler.getInstance().generate(); |
||||
publicKeyText = RSAKeysHandler.getInstance().publicKey2String(keyPair.getPublic()); |
||||
privateKeyText = RSAKeysHandler.getInstance().privateKey2String(keyPair.getPrivate()); |
||||
} catch (NoSuchAlgorithmException noSuchAlgorithmException) { |
||||
noSuchAlgorithmException.printStackTrace(); |
||||
} |
||||
} |
||||
if (encryptionType.equals(FrameConstants.SM2)) { |
||||
try { |
||||
SM2KeyPair sm2KeyPair = SM2KeysHandler.getInstance().generate(); |
||||
publicKeyText = SM2KeysHandler.getInstance().publicKey2String(sm2KeyPair.getPublicKeyParameters(), sm2KeyPair.getPrivateKeyParameters()); |
||||
privateKeyText = SM2KeysHandler.getInstance().privateKey2String(sm2KeyPair.getPrivateKeyParameters()); |
||||
} catch (Exception exception) { |
||||
exception.printStackTrace(); |
||||
} |
||||
} |
||||
TextPanel.INSTANCE.setLeftText(publicKeyText); |
||||
TextPanel.INSTANCE.setRightText(privateKeyText); |
||||
} else { |
||||
FilePanel.INSTANCE.setLeftLabelText(FrameConstants.CHOOSE_FILE); |
||||
FilePanel.INSTANCE.setButtonText(FrameConstants.CHOOSE); |
||||
if (interfaceComboBox.getSelectedIndex() == 1) { |
||||
TipPanel.INSTANCE.setLeftLabel(FrameConstants.PLAIN_TEXT); |
||||
TipPanel.INSTANCE.setRightLabel(FrameConstants.CIPHER_TEXT); |
||||
runButton.setText(FrameConstants.RUN_ENCRYPT); |
||||
} else { |
||||
TipPanel.INSTANCE.setLeftLabel(FrameConstants.CIPHER_TEXT); |
||||
TipPanel.INSTANCE.setRightLabel(FrameConstants.PLAIN_TEXT); |
||||
runButton.setText(FrameConstants.RUN_DECRYPT); |
||||
} |
||||
if (e.getSource() == interfaceComboBox) { |
||||
TextPanel.INSTANCE.setLeftText(StrUtil.EMPTY); |
||||
TextPanel.INSTANCE.setRightText(StrUtil.EMPTY); |
||||
} |
||||
if (e.getSource() != runButton || !isChooseEncryption() || StrUtil.isEmpty(FilePanel.INSTANCE.getFilePath())) { |
||||
return; |
||||
} |
||||
String encryptionType = (String) chooseComboBox.getSelectedItem(); |
||||
String publicKeyText = StrUtil.EMPTY; |
||||
String privateKeyText = StrUtil.EMPTY; |
||||
Map<String, String> keyMap = SecretHelper.getKeyPair(FilePanel.INSTANCE.getFilePath()); |
||||
publicKeyText = keyMap.get(SecretHelper.PUBLIC_KEY); |
||||
privateKeyText = keyMap.get(SecretHelper.PRIVATE_KEY); |
||||
if (StrUtil.isEmpty(publicKeyText) || StrUtil.isEmpty(privateKeyText)) { |
||||
return; |
||||
} |
||||
// 加密界面
|
||||
if (interfaceComboBox.getSelectedIndex() == 1) { |
||||
String plainText = TextPanel.INSTANCE.getLeftText(); |
||||
if (StrUtil.isEmpty(plainText)) { |
||||
return; |
||||
} |
||||
if (encryptionType.equals(FrameConstants.RSA)) { |
||||
PublicKey publicKey = RSAKeysHandler.getInstance().string2PublicKey(publicKeyText); |
||||
TextPanel.INSTANCE.setRightText(EncodeUtil.byte2Base64(RSAUtil.encrypt(plainText.getBytes(), publicKey))); |
||||
} |
||||
if (encryptionType.equals(FrameConstants.SM2)) { |
||||
try { |
||||
ECPublicKeyParameters publicKeyParameters = SM2KeysHandler.getInstance().string2PublicKey(publicKeyText, privateKeyText); |
||||
TextPanel.INSTANCE.setRightText(EncodeUtil.byte2Base64(SM2Util.encrypt(publicKeyParameters, plainText.getBytes()))); |
||||
} catch (Exception exception) { |
||||
return; |
||||
} |
||||
} |
||||
} else { // 解密界面
|
||||
String cipherText = TextPanel.INSTANCE.getLeftText(); |
||||
if (StrUtil.isEmpty(cipherText)) { |
||||
return; |
||||
} |
||||
if (encryptionType.equals(FrameConstants.RSA)) { // RSA 解密
|
||||
try { |
||||
PrivateKey privateKey = RSAKeysHandler.getInstance().string2PrivateKey(privateKeyText); |
||||
TextPanel.INSTANCE.setLeftText(new String(RSAUtil.decrypt(EncodeUtil.base642Byte(cipherText), privateKey), StandardCharsets.UTF_8)); |
||||
} catch (IOException ioException) { |
||||
return; |
||||
} |
||||
|
||||
} else { // SM2 解密
|
||||
try { |
||||
ECPrivateKeyParameters privateKeyParameters = SM2KeysHandler.getInstance().string2PrivateKey(privateKeyText); |
||||
TextPanel.INSTANCE.setRightText(new String(SM2Util.decrypt(privateKeyParameters, EncodeUtil.base642Byte(cipherText)), StandardCharsets.UTF_8)); |
||||
} catch (Exception exception) { |
||||
return; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue