package com.jijunpeng.javafx.cipher.controller;
import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.GlobalBouncyCastleProvider;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.jijunpeng.javafx.cipher.common.DialogUtil;
import com.jijunpeng.javafx.cipher.common.enums.*;
import com.jijunpeng.javafx.cipher.controller.api.BaseController;
import com.jijunpeng.javafx.cipher.controller.api.ICipherController;
import com.jijunpeng.javafx.cipher.exception.KeyTypeException;
import com.jijunpeng.javafx.cipher.persistence.PersistenceJson;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.util.io.pem.PemObject;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
/**
* @author Ji Junpeng
* @date 2019-11-30 19-45
*/
@Getter
@Slf4j
@PersistenceJson(path = "$.main_inner_$rsa")
public class RsaController extends BaseController implements ICipherController {
@FXML
private Pane rsaRootPane;
@FXML
@PersistenceJson(path = "leftHexRb", field = "selected", getter = "isSelected")
private RadioButton leftHexRb;
@FXML
@PersistenceJson(path = "leftBase64Rb", field = "selected", getter = "isSelected")
private RadioButton leftBase64Rb;
@FXML
@PersistenceJson(path = "leftStringRb", field = "selected", getter = "isSelected")
private RadioButton leftStringRb;
@FXML
@PersistenceJson(path = "leftStringCharsetComboBox", field = "value")
private ComboBox<CharsetEnum> leftStringCharsetComboBox;
@FXML
@PersistenceJson(path = "leftTextArea", field = "text")
private TextArea leftTextArea;
@FXML
@PersistenceJson(path = "rightHexRb", field = "selected", getter = "isSelected")
private RadioButton rightHexRb;
@FXML
@PersistenceJson(path = "rightBase64Rb", field = "selected", getter = "isSelected")
private RadioButton rightBase64Rb;
@FXML
@PersistenceJson(path = "rightTextArea", field = "text")
private TextArea rightTextArea;
@FXML
@PersistenceJson(path = "paddingComboBox", field = "value")
private ComboBox<RsaPaddingEnum> paddingComboBox;
@FXML
@PersistenceJson(path = "cipherProviderComboBox", field = "value")
private ComboBox<CipherProviderEnum> cipherProviderComboBox;
@FXML
@PersistenceJson(path = "privateKeyTypeComboBox", field = "value")
private ComboBox<RsaPrivateKeyTypeEnum> privateKeyTypeComboBox;
@FXML
@PersistenceJson(path = "publicKeyTypeComboBox", field = "value")
private ComboBox<RsaPublicKeyTypeEnum> publicKeyTypeComboBox;
@FXML
@PersistenceJson(path = "privateKeyTextArea", field = "text")
private TextArea privateKeyTextArea;
@FXML
@PersistenceJson(path = "publicKeyTextArea", field = "text")
private TextArea publicKeyTextArea;
@FXML
private Pane paddingVg;
@FXML
private Button genKeyPairBtn;
@FXML
private Button genPublicKeyBtn;
@Override
protected Pane getRootPane() {
return rsaRootPane;
}
@Override
protected void initViewData() {
super.initViewData();
initCipherController();
paddingComboBox.setItems(FXCollections.observableArrayList(RsaPaddingEnum.values()));
paddingComboBox.setValue(RsaPaddingEnum.values()[0]);
// todo
privateKeyTypeComboBox.setItems(FXCollections.singletonObservableList(RsaPrivateKeyTypeEnum.values()[0]));
privateKeyTypeComboBox.setValue(RsaPrivateKeyTypeEnum.values()[0]);
publicKeyTypeComboBox.setItems(FXCollections.observableArrayList(RsaPublicKeyTypeEnum.values()));
publicKeyTypeComboBox.setValue(RsaPublicKeyTypeEnum.values()[0]);
privateKeyTextArea.setPromptText("-----BEGIN PRIVATE KEY----- 开头,或者 -----BEGIN RSA PRIVATE KEY----- 开头");
publicKeyTextArea.setPromptText("-----BEGIN PUBLIC KEY----- 开头");
// todo
paddingVg.setVisible(false);
publicKeyTypeComboBox.setVisible(false);
genKeyPairBtn.setVisible(false);
genPublicKeyBtn.setVisible(false);
}
/**
* left --private key encrypt--> right
*/
@FXML
private void encryptByPrivateKey(ActionEvent actionEvent) {
try {
PrivateKey privateKey = getPrivateKey();
byte[] inputBytes = getLeftBytes();
byte[] outputBytes = newRsa().setPrivateKey(privateKey).encrypt(inputBytes, KeyType.PrivateKey);
String rightStr = buildRightStr(outputBytes);
rightTextArea.setText(rightStr);
log.info("RSA encrypt by private key. inputStr:{}, outputStr:{}", leftTextArea.getText(), rightStr);
} catch (Exception e) {
log.error("RSA encrypt by private key throw exception. e:", e);
DialogUtil.showError(rsaRootPane, "RSA 私钥加密失败", "错误原因:" + e.getMessage());
}
}
/**
* left --public key encrypt--> right
*/
@FXML
private void encryptByPublicKey(ActionEvent actionEvent) {
try {
PublicKey publicKey = getPublicKey();
byte[] inputBytes = getLeftBytes();
byte[] outputBytes = newRsa().setPublicKey(publicKey).encrypt(inputBytes, KeyType.PublicKey);
String rightStr = buildRightStr(outputBytes);
rightTextArea.setText(rightStr);
log.info("RSA encrypt by public key. inputStr:{}, outputStr:{}", leftTextArea.getText(), rightStr);
} catch (Exception e) {
log.error("RSA encrypt by public key throw exception. e:", e);
DialogUtil.showError(rsaRootPane, "RSA 私钥加密失败", "错误原因:" + e.getMessage());
}
}
/**
* left <--private key decrypt-- right
*/
@FXML
private void decryptByPrivateKey(ActionEvent actionEvent) {
try {
PrivateKey privateKey = getPrivateKey();
byte[] inputBytes = getRightBytes();
byte[] outputBytes = newRsa().setPrivateKey(privateKey).decrypt(inputBytes, KeyType.PrivateKey);
String leftStr = buildLeftStr(outputBytes);
leftTextArea.setText(leftStr);
log.info("RSA decrypt by private key. inputStr:{}, outputStr:{}", rightTextArea.getText(), leftStr);
} catch (Exception e) {
log.error("RSA decrypt by private key throw exception. e:", e);
DialogUtil.showError(rsaRootPane, "RSA 私钥解密失败", "错误原因:" + e.getMessage());
}
}
/**
* left <--public key decrypt-- right
*/
@FXML
private void decryptByPublicKey(ActionEvent actionEvent) {
try {
PublicKey publicKey = getPublicKey();
byte[] inputBytes = getRightBytes();
byte[] outputBytes = newRsa().setPublicKey(publicKey).decrypt(inputBytes, KeyType.PublicKey);
String leftStr = buildLeftStr(outputBytes);
leftTextArea.setText(leftStr);
log.info("RSA decrypt by public key. inputStr:{}, outputStr:{}", rightTextArea.getText(), leftStr);
} catch (Exception e) {
log.error("RSA decrypt by public key throw exception. e:", e);
DialogUtil.showError(rsaRootPane, "RSA 公钥解密失败", "错误原因:" + e.getMessage());
}
}
private RSA newRsa() {
GlobalBouncyCastleProvider.setUseBouncyC
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
JavaFX是一种用于构建桌面、移动和嵌入式设备上的富客户端应用程序的开源框架,它提供了丰富的用户界面组件和图形效果。在本项目中,“使用JavaFx编写的加解密工具”是一个利用JavaFX库创建的实用程序,它可以帮助用户进行数据的加密和解密操作。该工具支持两种常见的加密算法:Base64和AES(高级加密标准)。 Base64是一种编码方式,通常用于将二进制数据转换为ASCII文本,以便在网络上传输。在加解密领域,Base64常用于将密文转换为可读的文本格式。而AES是一种块密码,以其高安全性、快速执行和易于实现而广泛应用于各种加密场景。AES的工作模式如ECB(电子密码本)、CBC(链式模式)、CFB(密文反馈模式)和OFB(输出反馈模式)等,可以根据需求选择。 开发这个工具的环境是IntelliJ IDEA 2019.2,这是一款流行的Java集成开发环境,提供了强大的代码编辑、调试和项目管理功能。IDEA对于JavaFX的支持非常完善,可以方便地创建、运行和调试JavaFX应用。 JDK 8是Java Development Kit的第8个主要版本,它引入了Lambda表达
资源推荐
资源详情
资源评论
收起资源包目录
javsdfg343443ool-master.zip (43个子文件)
pom.xml 3KB
doc
A30F5157-3265-4D6D-9AE8-FE9C45B999DA.png 221KB
41E3796D-24FA-4DB6-BB96-DC160B15DC30.png 105KB
3814E6EB-9F93-4E5D-85C3-20AB25C3EE03.png 73KB
600336E1-0B6C-4565-9F6A-4421A3C477FE.png 83KB
src
test
java
com
jijunpeng
javafx
cipher
controller
HexControllerTest.java 1KB
AesTest.java 945B
BaseTest.java 542B
main
resources
project.properties 34B
logback.xml 412B
fxml
aes.fxml 6KB
hex.fxml 3KB
main.fxml 1KB
rsa.fxml 7KB
hash.fxml 5KB
random.fxml 3KB
base64.fxml 4KB
img
Cipher Tool.icns 195KB
ic_launcher.png 165KB
java
com
jijunpeng
javafx
cipher
controller
HexController.java 4KB
RandomController.java 5KB
Base64Controller.java 6KB
HashController.java 4KB
api
ILeftStringCharsetController.java 814B
ICipherController.java 5KB
BaseController.java 8KB
AesController.java 9KB
MainController.java 3KB
RsaController.java 10KB
persistence
PersistenceJson.java 697B
common
LetterCaseEnum.java 414B
enums
RsaPublicKeyTypeEnum.java 539B
RsaPaddingEnum.java 460B
AesKeyTypeEnum.java 533B
CipherProviderEnum.java 403B
CharsetEnum.java 527B
RsaPrivateKeyTypeEnum.java 556B
ComboTxtConstants.java 1KB
DialogUtil.java 1KB
exception
OutputTypeException.java 698B
KeyTypeException.java 680B
InputTypeException.java 692B
CipherToolApplication.java 5KB
共 43 条
- 1
资源评论
程序猿小D
- 粉丝: 3909
- 资源: 386
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功