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.
183 lines
8.7 KiB
183 lines
8.7 KiB
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; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
|