本篇,我们尝试使用java开发一个图形化小工具(rsaTool)。
最终运行效果如下: 界面最上方有4个tab标签, 这4个标签页中分别实现了rsa加密、解密、签名、验签的功能。
下面我们来具体介绍下项目代码开发过程。
此项目使用的IDE是spring STS ,由于开发的是GUI程序,我们可以先安装一款可视化编辑器工具windowbuilder ,安装步骤如图:
在 help--》eclipse Marketplace --》search --》windowbuilder--》 安装 即可。
new一个java工程,起名为rsaTool。
java开发人员,无论开发什么都必须面向对象,对整个项目进行MVC划分。
img :图标资源
model: 模型层 ,java实体bean,业务bean等。
view :视图层,主要是JFrame 窗体等
由于RSA加解密需要依赖包 bcprov-jdk16-1.46.jar ,故需要在项目根目录中新建一个lib目录,将bcprov-jdk16-1.46.jar放入lib目录中并加入classpath 中。
ok,我们先来看看model 模型层 : 有两个工具类,一个业务类。
Base64.java
package com.tingcream.rsaTool.model; public final class Base64 { static private final int BASELENGTH = 128; static private final int LOOKUPLENGTH = 64; static private final int TWENTYFOURBITGROUP = 24; static private final int EIGHTBIT = 8; static private final int SIXTEENBIT = 16; static private final int FOURBYTE = 4; static private final int SIGN = -128; static private final char PAD = '='; static private final boolean fDebug = false; static final private byte[] base64Alphabet = new byte[BASELENGTH]; static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; static { for (int i = 0; i < BASELENGTH; ++i) { base64Alphabet[i] = -1; } for (int i = 'Z'; i >= 'A'; i--) { base64Alphabet[i] = (byte) (i - 'A'); } for (int i = 'z'; i >= 'a'; i--) { base64Alphabet[i] = (byte) (i - 'a' + 26); } for (int i = '9'; i >= '0'; i--) { base64Alphabet[i] = (byte) (i - '0' + 52); } base64Alphabet['+'] = 62; base64Alphabet['/'] = 63; for (int i = 0; i <= 25; i++) { lookUpBase64Alphabet[i] = (char) ('A' + i); } for (int i = 26, j = 0; i <= 51; i++, j++) { lookUpBase64Alphabet[i] = (char) ('a' + j); } for (int i = 52, j = 0; i <= 61; i++, j++) { lookUpBase64Alphabet[i] = (char) ('0' + j); } lookUpBase64Alphabet[62] = (char) '+'; lookUpBase64Alphabet[63] = (char) '/'; } private static boolean isWhiteSpace(char octect) { return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); } private static boolean isPad(char octect) { return (octect == PAD); } private static boolean isData(char octect) { return (octect < BASELENGTH && base64Alphabet[octect] != -1); } public static String encodeFromStr(String str){ return encode(str.getBytes()); } /** * Encodes hex octects into Base64 * @param binaryData Array containing binaryData * @return Encoded Base64 array */ public static String encode(byte[] binaryData) { if (binaryData == null) { return null; } int lengthDataBits = binaryData.length * EIGHTBIT; if (lengthDataBits == 0) { return ""; } int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP; int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP; int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets; char encodedData[] = null; encodedData = new char[numberQuartet * 4]; byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; int encodedIndex = 0; int dataIndex = 0; if (fDebug) { System.out.println("number of triplets = " + numberTriplets); } for (int i = 0; i < numberTriplets; i++) { b1 = binaryData[dataIndex++]; b2 = binaryData[dataIndex++]; b3 = binaryData[dataIndex++]; if (fDebug) { System.out.println("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3); } l = (byte) (b2 & 0x0f); k = (byte) (b1 & 0x03); byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc); if (fDebug) { System.out.println("val2 = " + val2); System.out.println("k4 = " + (k << 4)); System.out.println("vak = " + (val2 | (k << 4))); } encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3]; encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f]; } // form integral number of 6-bit groups if (fewerThan24bits == EIGHTBIT) { b1 = binaryData[dataIndex]; k = (byte) (b1 & 0x03); if (fDebug) { System.out.println("b1=" + b1); System.out.println("b1<<2 = " + (b1 >> 2)); } byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4]; encodedData[encodedIndex++] = PAD; encodedData[encodedIndex++] = PAD; } else if (fewerThan24bits == SIXTEENBIT) { b1 = binaryData[dataIndex]; b2 = binaryData[dataIndex + 1]; l = (byte) (b2 & 0x0f); k = (byte) (b1 & 0x03); byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2]; encodedData[encodedIndex++] = PAD; } return new String(encodedData); } public static String decodeToStr(String encoded){ return new String(decode(encoded)); } /** * Decodes Base64 data into octects * * @param encoded string containing Base64 data * @return Array containind decoded data. */ public static byte[] decode(String encoded) { if (encoded == null) { return null; } char[] base64Data = encoded.toCharArray(); // remove white spaces int len = removeWhiteSpace(base64Data); if (len % FOURBYTE != 0) { return null;//should be divisible by four } int numberQuadruple = (len / FOURBYTE); if (numberQuadruple == 0) { return new byte[0]; } byte decodedData[] = null; byte b1 = 0, b2 = 0, b3 = 0, b4 = 0; char d1 = 0, d2 = 0, d3 = 0, d4 = 0; int i = 0; int encodedIndex = 0; int dataIndex = 0; decodedData = new byte[(numberQuadruple) * 3]; for (; i < numberQuadruple - 1; i++) { if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++])) || !isData((d3 = base64Data[dataIndex++])) || !isData((d4 = base64Data[dataIndex++]))) { return null; }//if found "no data" just return null b1 = base64Alphabet[d1]; b2 = base64Alphabet[d2]; b3 = base64Alphabet[d3]; b4 = base64Alphabet[d4]; decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); } if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) { return null;//if found "no data" just return null } b1 = base64Alphabet[d1]; b2 = base64Alphabet[d2]; d3 = base64Data[dataIndex++]; d4 = base64Data[dataIndex++]; if (!isData((d3)) || !isData((d4))) {//Check if they are PAD characters if (isPad(d3) && isPad(d4)) { if ((b2 & 0xf) != 0)//last 4 bits should be zero { return null; } byte[] tmp = new byte[i * 3 + 1]; System.arraycopy(decodedData, 0, tmp, 0, i * 3); tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); return tmp; } else if (!isPad(d3) && isPad(d4)) { b3 = base64Alphabet[d3]; if ((b3 & 0x3) != 0)//last 2 bits should be zero { return null; } byte[] tmp = new byte[i * 3 + 2]; System.arraycopy(decodedData, 0, tmp, 0, i * 3); tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); return tmp; } else { return null; } } else { //No PAD e.g 3cQl b3 = base64Alphabet[d3]; b4 = base64Alphabet[d4]; decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); } return decodedData; } /** * remove WhiteSpace from MIME containing encoded Base64 data. * * @param data the byte array of base64 data (with WS) * @return the new length */ private static int removeWhiteSpace(char[] data) { if (data == null) { return 0; } // count characters that's not whitespace int newSize = 0; int len = data.length; for (int i = 0; i < len; i++) { if (!isWhiteSpace(data[i])) { data[newSize++] = data[i]; } } return newSize; } }
RSA.java
package com.tingcream.rsaTool.model; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import org.bouncycastle.jce.provider.BouncyCastleProvider; public class RSA { private static String RSA_JAVA = "RSA/None/PKCS1Padding"; public static final String SIGN_ALGORITHMS = "SHA1WithRSA"; /** * 通过rsa私钥字符串 创建RSA私钥对象 */ public static RSAPrivateKey getPrivateKeyFromStr(String privatekeyStr) { try { byte[] buffer = Base64.decode(privatekeyStr); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPrivateKey privatekey = (RSAPrivateKey)keyFactory.generatePrivate(keySpec); return privatekey; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("RSA私钥文件已损坏"); } } /** * 通过rsa公钥字符串 创建RSA公钥对象 */ public static RSAPublicKey getPublickeyFromStr(String publickeyStr) { try { byte[] buffer = Base64.decode(publickeyStr); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKey publickey = (RSAPublicKey)keyFactory.generatePublic(keySpec); return publickey; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("RSA公钥文件已损坏"); } } /** * rsa 加密方法 * 入参 rsa公钥 、待加密内容 */ public static String encrypt(RSAPublicKey publicKey, String content) { if(content==null){ return ""; } if (publicKey == null) { throw new RuntimeException("publickey 值不能为空"); } Cipher cipher = null; try { cipher = Cipher.getInstance(RSA_JAVA, new BouncyCastleProvider()); cipher.init(1, publicKey); byte[] binaryData = cipher.doFinal(content.getBytes("utf-8")); String str = Base64.encode(binaryData); return str; } catch (Exception e) { throw new RuntimeException(e); } } /** * rsa 解密方法 * rsa私钥、密文字符串 (密文编码默认utf8) */ public static String decrypt(RSAPrivateKey privateKey, String cipherContent) { if(cipherContent==null){ return ""; } byte[] data = Base64.decode(cipherContent); if (privateKey == null) { throw new RuntimeException("privateKey 不能为空"); } Cipher cipher = null; try { cipher = Cipher.getInstance(RSA_JAVA, new BouncyCastleProvider()); cipher.init(2, privateKey); byte[] output = cipher.doFinal(data); String str = Base64.encode(output); String text = new String(Base64.decode(str), "utf-8"); return text; } catch (NoSuchAlgorithmException e) { throw new RuntimeException("无此解密算法"); } catch (Exception e) { throw new RuntimeException("密文数据已损坏"); } } /** * RSA验签名检查 * @param content 待签名数据 * @param sign 签名值 * @param pubKey rsa公钥 * @return 布尔值 */ public static boolean verify(String content, String sign, PublicKey pubKey) { try { java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update( content.getBytes("utf-8") ); boolean bverify = signature.verify( Base64.decode(sign) ); return bverify; } catch (Exception e) { e.printStackTrace(); } return false; } /** * RSA签名 * @param content 待签名数据 * @param privateKey 商户私钥 * @param input_charset 编码格式 * @return 签名值 */ public static String sign(String content, PrivateKey priKey ) { try { java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initSign(priKey); signature.update( content.getBytes("utf-8") ); byte[] signed = signature.sign(); return Base64.encode(signed); } catch (Exception e) { e.printStackTrace(); } return ""; } }
RSAHelper.java
package com.tingcream.rsaTool.model; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; /** * RSA 辅助类 * @author jelly * */ public class RSAHelper { /** * rsa 加密 (公钥加密) * @param content 明文内容 * @param rsaPublickeyStr 公钥字符串 * @return 加密结果 */ public String rsaEncrypt(String content ,String rsaPublickeyStr) { RSAPublicKey rsaPublicKey= RSA.getPublickeyFromStr(rsaPublickeyStr); return RSA.encrypt(rsaPublicKey, content); } /** * rsa 解密 (私钥解密) * @param cipherContent 密文内容 * @param rsaPrivatekeyStr 私钥字符串 * @return 解密结果 */ public String rsaDecrypt(String cipherContent ,String rsaPrivatekeyStr) { RSAPrivateKey rsaPrivateKey = RSA.getPrivateKeyFromStr(rsaPrivatekeyStr); return RSA.decrypt(rsaPrivateKey, cipherContent); } /** * rsa 签名 (私钥签名) * @param content 明文内容 * @param rsaPrivatekeyStr 私钥字符串 * @return 签名结果 */ public String rsaSign (String content ,String rsaPrivatekeyStr ) { RSAPrivateKey rsaPrivateKey = RSA.getPrivateKeyFromStr(rsaPrivatekeyStr); return RSA.sign(content, rsaPrivateKey); } /** * rsa 验证签名 (公钥验签) * @param content * @param sign * @param rsaPublicKeyStr * @return */ public boolean rsaSignVerify(String content ,String sign,String rsaPublicKeyStr ) { RSAPublicKey rsaPublicKey= RSA.getPublickeyFromStr(rsaPublicKeyStr); return RSA.verify(content, sign, rsaPublicKey); } }
view 视图层 (JFrame):
MainFrame.java 主窗体
package com.tingcream.rsaTool.view; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import java.awt.Color; import javax.swing.ImageIcon; import java.awt.Font; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import java.awt.Toolkit; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.LayoutStyle.ComponentPlacement; import com.tingcream.rsaTool.model.RSAHelper; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.JTextArea; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JScrollPane; /** * rsaTool 主窗体 * @author jelly * */ public class MainFrame extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private RSAHelper rsaHelper=new RSAHelper(); private JTextField panel_1_textField_1; private JLabel panel_1_label_1,panel_1_label_2,panel_1_label_3; private JButton panel_1_button_1,panel_1_button_2; private JScrollPane panel_1_scrollPane_1; private JTextArea panel_1_textArea_1; private JScrollPane panel_1_scrollPane_2; private JTextArea panel_1_textArea_2; private JLabel panel_2_label_1; private JTextField panel_2_textField_1; private JLabel panel_2_label_2; private JScrollPane panel_2_scrollPane_1; private JTextArea panel_2_textArea_1; private JButton panel_2_button_1; private JButton panel_2_button_2; private JLabel panel_2_label_3; private JScrollPane panel_2_scrollPane_2; private JTextArea panel_2_textArea_2; private JLabel panel_3_label_1; private JLabel panel_3_label_2; private JLabel panel_3_label_3; private JTextField panel_3_textField_1; private JScrollPane panel_3_scrollPane_1; private JTextArea panel_3_textArea_1; private JScrollPane panel_3_scrollPane_2; private JTextArea panel_3_textArea_2; private JButton panel_3_button_1; private JButton panel_3_button_2; private JLabel panel_4_label_1; private JTextField panel_4_textField_1; private JLabel panel_4_label_2; private JScrollPane panel_4_scrollPane_1; private JTextArea panel_4_textArea_1; private JLabel panel_4_label_3; private JScrollPane panel_4_scrollPane_2; private JTextArea panel_4_textArea_2; private JButton panel_4_button_1; private JButton panel_4_button_2; private JLabel panel_4_label_4; private JLabel panel_4_label_5; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainFrame frame = new MainFrame(); frame.setVisible(true); frame.setLocationRelativeTo(null);//主窗体居中 } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MainFrame() { setResizable(false); setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/com/tingcream/rsaTool/img/tools_24px.png"))); setFont(new Font("微软雅黑", Font.PLAIN, 18)); setBackground(Color.LIGHT_GRAY); setTitle("rsaTool加解密工具"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 635, 514); contentPane = new JPanel(); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 18)); contentPane.add(tabbedPane, BorderLayout.CENTER); JPanel panel_1 = new JPanel(); tabbedPane.addTab("rsa加密", new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/encrypt_3_24px.png")), panel_1, null); tabbedPane.setForegroundAt(0, Color.BLACK); tabbedPane.setBackgroundAt(0, Color.WHITE); panel_1_label_1 = new JLabel("RSA公钥:"); panel_1_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_textField_1 = new JTextField(); panel_1_textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_textField_1.setColumns(10); panel_1_label_2 = new JLabel("明 文:"); panel_1_label_2.setHorizontalAlignment(SwingConstants.TRAILING); panel_1_label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_button_1 = new JButton("加 密"); panel_1_button_1.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/encrypt_3_24px.png"))); panel_1_button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_1_button_1_click(e); } }); panel_1_button_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_label_3 = new JLabel("密 文:"); panel_1_label_3.setHorizontalAlignment(SwingConstants.TRAILING); panel_1_label_3.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_button_2 = new JButton("清除明/密文"); panel_1_button_2.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/editclear_24px.png"))); panel_1_button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_1_textArea_1.setText(""); panel_1_textArea_2.setText(""); } }); panel_1_button_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_scrollPane_1 = new JScrollPane(); panel_1_scrollPane_2 = new JScrollPane(); GroupLayout gl_panel_1 = new GroupLayout(panel_1); gl_panel_1.setHorizontalGroup( gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addContainerGap() .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(panel_1_label_1) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(panel_1_textField_1)) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(panel_1_label_2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(16) .addComponent(panel_1_scrollPane_1, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE)))) .addGroup(gl_panel_1.createSequentialGroup() .addGap(20) .addComponent(panel_1_label_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(13) .addComponent(panel_1_scrollPane_2, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_1.createSequentialGroup() .addGap(105) .addComponent(panel_1_button_1) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panel_1_button_2))) .addContainerGap(51, Short.MAX_VALUE)) ); gl_panel_1.setVerticalGroup( gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addGap(24) .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE) .addComponent(panel_1_label_1) .addComponent(panel_1_textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(28) .addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(panel_1_label_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE) .addGap(102)) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(panel_1_scrollPane_1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE) .addGap(16))) .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE) .addComponent(panel_1_button_1) .addComponent(panel_1_button_2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addGap(22) .addComponent(panel_1_label_3, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_1.createSequentialGroup() .addGap(24) .addComponent(panel_1_scrollPane_2, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))) .addContainerGap(28, Short.MAX_VALUE)) ); panel_1_textArea_2 = new JTextArea(); panel_1_textArea_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_scrollPane_2.setViewportView(panel_1_textArea_2); panel_1_textArea_1 = new JTextArea(); panel_1_textArea_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_1_scrollPane_1.setViewportView(panel_1_textArea_1); panel_1.setLayout(gl_panel_1); JPanel panel_2 = new JPanel(); panel_2.setForeground(Color.WHITE); tabbedPane.addTab("rsa解密", new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/decrypted_24px.png")), panel_2, null); panel_2_label_1 = new JLabel("RSA私钥:"); panel_2_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_textField_1 = new JTextField(); panel_2_textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_textField_1.setColumns(10); panel_2_label_2 = new JLabel(" 密 文:"); panel_2_label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_scrollPane_1 = new JScrollPane(); panel_2_button_1 = new JButton("解 密"); panel_2_button_1.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/decrypted_24px.png"))); panel_2_button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_2_button_1_click(e); } }); panel_2_button_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_button_2 = new JButton("清除明/密文"); panel_2_button_2.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/editclear_24px.png"))); panel_2_button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_2_textArea_1.setText(""); panel_2_textArea_2.setText(""); } }); panel_2_button_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_label_3 = new JLabel(" 明 文:"); panel_2_label_3.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_scrollPane_2 = new JScrollPane(); GroupLayout gl_panel_2 = new GroupLayout(panel_2); gl_panel_2.setHorizontalGroup( gl_panel_2.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_2.createSequentialGroup() .addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_2.createSequentialGroup() .addContainerGap() .addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING, false) .addGroup(gl_panel_2.createSequentialGroup() .addComponent(panel_2_label_1) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(panel_2_textField_1)) .addGroup(gl_panel_2.createSequentialGroup() .addComponent(panel_2_label_2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(16) .addComponent(panel_2_scrollPane_1, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE)))) .addGroup(gl_panel_2.createSequentialGroup() .addGap(20) .addComponent(panel_2_label_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(13) .addComponent(panel_2_scrollPane_2, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_2.createSequentialGroup() .addGap(105) .addComponent(panel_2_button_1) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panel_2_button_2))) .addContainerGap(51, Short.MAX_VALUE)) ); gl_panel_2.setVerticalGroup( gl_panel_2.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_2.createSequentialGroup() .addGap(24) .addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE) .addComponent(panel_2_label_1) .addComponent(panel_2_textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(28) .addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panel_2.createSequentialGroup() .addComponent(panel_2_label_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE) .addGap(102)) .addGroup(gl_panel_2.createSequentialGroup() .addComponent(panel_2_scrollPane_1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE) .addGap(16))) .addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE) .addComponent(panel_2_button_1) .addComponent(panel_2_button_2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_2.createSequentialGroup() .addGap(22) .addComponent(panel_2_label_3, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_2.createSequentialGroup() .addGap(24) .addComponent(panel_2_scrollPane_2, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))) .addContainerGap(28, Short.MAX_VALUE)) ); panel_2_textArea_2 = new JTextArea(); panel_2_textArea_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_scrollPane_2.setViewportView(panel_2_textArea_2); panel_2_textArea_1 = new JTextArea(); panel_2_textArea_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_2_scrollPane_1.setViewportView(panel_2_textArea_1); panel_2.setLayout(gl_panel_2); tabbedPane.setForegroundAt(1, Color.BLACK); JPanel panel_3 = new JPanel(); tabbedPane.addTab("rsa签名", new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/ok_signature_24px.png")), panel_3, null); panel_3_label_1 = new JLabel("RSA私钥:"); panel_3_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_3_label_2 = new JLabel(" 明 文:"); panel_3_label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_3_label_3 = new JLabel(" 签 名 值:"); panel_3_label_3.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_3_textField_1 = new JTextField(); panel_3_textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_3_textField_1.setColumns(10); panel_3_scrollPane_1 = new JScrollPane(); panel_3_scrollPane_2 = new JScrollPane(); panel_3_button_1 = new JButton("签 名"); panel_3_button_1.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/ok_signature_24px.png"))); panel_3_button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_3_button_1_click(e); } }); panel_3_button_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_3_button_2 = new JButton("清除明文/签名值"); panel_3_button_2.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/editclear_24px.png"))); panel_3_button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_3_textArea_1.setText(""); panel_3_textArea_2.setText(""); } }); panel_3_button_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); GroupLayout gl_panel_3 = new GroupLayout(panel_3); gl_panel_3.setHorizontalGroup( gl_panel_3.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_3.createSequentialGroup() .addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_3.createSequentialGroup() .addContainerGap() .addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING, false) .addGroup(gl_panel_3.createSequentialGroup() .addComponent(panel_3_label_1) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(panel_3_textField_1)) .addGroup(gl_panel_3.createSequentialGroup() .addComponent(panel_3_label_2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(16) .addComponent(panel_3_scrollPane_1, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE)))) .addGroup(gl_panel_3.createSequentialGroup() .addGap(20) .addComponent(panel_3_label_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(13) .addComponent(panel_3_scrollPane_2, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_3.createSequentialGroup() .addGap(105) .addComponent(panel_3_button_1) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panel_3_button_2))) .addContainerGap(51, Short.MAX_VALUE)) ); gl_panel_3.setVerticalGroup( gl_panel_3.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_3.createSequentialGroup() .addGap(24) .addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE) .addComponent(panel_3_label_1) .addComponent(panel_3_textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(28) .addGroup(gl_panel_3.createParallelGroup(Alignment.TRAILING) .addGroup(gl_panel_3.createSequentialGroup() .addComponent(panel_3_label_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE) .addGap(102)) .addGroup(gl_panel_3.createSequentialGroup() .addComponent(panel_3_scrollPane_1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE) .addGap(16))) .addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE) .addComponent(panel_3_button_1) .addComponent(panel_3_button_2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_3.createSequentialGroup() .addGap(22) .addComponent(panel_3_label_3, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_3.createSequentialGroup() .addGap(24) .addComponent(panel_3_scrollPane_2, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))) .addContainerGap(28, Short.MAX_VALUE)) ); panel_3_textArea_2 = new JTextArea(); panel_3_textArea_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_3_scrollPane_2.setViewportView(panel_3_textArea_2); panel_3_textArea_1 = new JTextArea(); panel_3_textArea_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_3_scrollPane_1.setViewportView(panel_3_textArea_1); panel_3.setLayout(gl_panel_3); tabbedPane.setForegroundAt(2, Color.BLACK); JPanel panel_4 = new JPanel(); tabbedPane.addTab("rsa验签", new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/verified_user_24px.png")), panel_4, null); panel_4_label_1 = new JLabel("RSA公钥:"); panel_4_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_textField_1 = new JTextField(); panel_4_textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_textField_1.setColumns(10); panel_4_label_2 = new JLabel(" 明 文:"); panel_4_label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_scrollPane_1 = new JScrollPane(); panel_4_label_3 = new JLabel(" 签 名 值:"); panel_4_label_3.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_scrollPane_2 = new JScrollPane(); panel_4_button_1 = new JButton("验 签"); panel_4_button_1.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/verified_user_24px.png"))); panel_4_button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_4_button_1_click(e); } }); panel_4_button_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_button_2 = new JButton("清除明文/签名值"); panel_4_button_2.setIcon(new ImageIcon(MainFrame.class.getResource("/com/tingcream/rsaTool/img/editclear_24px.png"))); panel_4_button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel_4_textArea_1.setText(""); panel_4_textArea_2.setText(""); } }); panel_4_button_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_label_4 = new JLabel(" 验签结果:"); panel_4_label_4.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_label_5 = new JLabel(""); panel_4_label_5.setFont(new Font("微软雅黑", Font.PLAIN, 18)); GroupLayout gl_panel_4 = new GroupLayout(panel_4); gl_panel_4.setHorizontalGroup( gl_panel_4.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_4.createSequentialGroup() .addContainerGap() .addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_4.createSequentialGroup() .addComponent(panel_4_label_1) .addGap(18) .addComponent(panel_4_textField_1, GroupLayout.PREFERRED_SIZE, 446, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_4.createSequentialGroup() .addComponent(panel_4_label_2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(panel_4_scrollPane_1, GroupLayout.PREFERRED_SIZE, 446, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_4.createSequentialGroup() .addComponent(panel_4_label_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE) .addGap(18) .addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_4.createSequentialGroup() .addComponent(panel_4_button_1) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panel_4_button_2, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)) .addComponent(panel_4_scrollPane_2, GroupLayout.PREFERRED_SIZE, 446, GroupLayout.PREFERRED_SIZE))) .addGroup(gl_panel_4.createSequentialGroup() .addComponent(panel_4_label_4, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(panel_4_label_5))) .addContainerGap(70, Short.MAX_VALUE)) ); gl_panel_4.setVerticalGroup( gl_panel_4.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_4.createSequentialGroup() .addGap(27) .addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE) .addComponent(panel_4_label_1) .addComponent(panel_4_textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING) .addComponent(panel_4_label_2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE) .addComponent(panel_4_scrollPane_1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING) .addComponent(panel_4_scrollPane_2, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE) .addComponent(panel_4_label_3, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_4.createSequentialGroup() .addComponent(panel_4_button_1) .addGap(25) .addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE) .addComponent(panel_4_label_4, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE) .addComponent(panel_4_label_5))) .addComponent(panel_4_button_2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)) .addContainerGap(26, Short.MAX_VALUE)) ); panel_4_textArea_2 = new JTextArea(); panel_4_textArea_2.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_scrollPane_2.setViewportView(panel_4_textArea_2); panel_4_textArea_1 = new JTextArea(); panel_4_textArea_1.setFont(new Font("微软雅黑", Font.PLAIN, 18)); panel_4_scrollPane_1.setViewportView(panel_4_textArea_1); panel_4.setLayout(gl_panel_4); } //rsa 加密 private void panel_1_button_1_click(ActionEvent e) { try { String publicKeyStr =this.panel_1_textField_1.getText(); String content = this.panel_1_textArea_1.getText(); if(publicKeyStr==null ||publicKeyStr.trim().equals("")) { JOptionPane.showMessageDialog(null, "RSA公钥不能为空!", "错误", JOptionPane.ERROR_MESSAGE); return ; } if(content==null ||content.trim().equals("")) { //JOptionPane.showMessageDialog(null, "明文内容不能为空!", "信息", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "明文内容不能为空!");//默认的 提示框 title为“信息” type为 INFORMATION_MESSAGE return ; } String cipherContent = rsaHelper.rsaEncrypt(content, publicKeyStr); this.panel_1_textArea_2.setText(cipherContent); } catch (Exception e1) { e1.printStackTrace(); JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } //rsa 解密 private void panel_2_button_1_click(ActionEvent e) { try { String privateKeyStr =this.panel_2_textField_1.getText(); String cipherContent = this.panel_2_textArea_1.getText(); if(privateKeyStr==null ||privateKeyStr.trim().equals("")) { JOptionPane.showMessageDialog(null, "RSA私钥不能为空!", "错误", JOptionPane.ERROR_MESSAGE); return ; } if(cipherContent==null ||cipherContent.trim().equals("")) { JOptionPane.showMessageDialog(null, "密文内容不能为空!");//默认的 提示框 title为“信息” type为 INFORMATION_MESSAGE return ; } String content = rsaHelper.rsaDecrypt(cipherContent, privateKeyStr); this.panel_2_textArea_2.setText(content); } catch (Exception e1) { e1.printStackTrace(); JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } //rsa 签名 private void panel_3_button_1_click(ActionEvent e) { try { String privateKeyStr =this.panel_3_textField_1.getText(); String content = this.panel_3_textArea_1.getText(); if(privateKeyStr==null ||privateKeyStr.trim().equals("")) { JOptionPane.showMessageDialog(null, "RSA私钥不能为空!", "错误", JOptionPane.ERROR_MESSAGE); return ; } if(content==null ||content.trim().equals("")) { JOptionPane.showMessageDialog(null, "明文内容不能为空!");//默认的 提示框 title为“信息” type为 INFORMATION_MESSAGE return ; } String sign =rsaHelper.rsaSign(content, privateKeyStr); this.panel_3_textArea_2.setText(sign); } catch (Exception e1) { e1.printStackTrace(); JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } //rsa 验证签名 private void panel_4_button_1_click(ActionEvent e) { try { String publicKeyStr = this.panel_4_textField_1.getText(); String content = this.panel_4_textArea_1.getText(); String sign = this.panel_4_textArea_2.getText(); if(publicKeyStr==null ||publicKeyStr.trim().equals("")) { JOptionPane.showMessageDialog(null, "RSA公钥不能为空!", "错误", JOptionPane.ERROR_MESSAGE); return ; } if(content==null ||content.trim().equals("")) { JOptionPane.showMessageDialog(null, "明文内容不能为空!"); return ; } if(sign==null ||sign.trim().equals("")) { JOptionPane.showMessageDialog(null, "签名值内容不能为空!"); return ; } boolean f=rsaHelper.rsaSignVerify(content, sign, publicKeyStr); if(f) { this.panel_4_label_5.setForeground( new Color(0, 255, 0)); this.panel_4_label_5.setText("验签成功"); }else { this.panel_4_label_5.setForeground( new Color(255, 0, 0)); this.panel_4_label_5.setText("验签失败"); } } catch (Exception e1) { e1.printStackTrace(); JOptionPane.showMessageDialog(null, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE); } } }
img层: 存放的都是图标资源 ,都是从easyicon.net上下载的, 读者们也可自行到其他网站下载合适的图标。
项目源码 : https://github.com/jellyflu/rsaTool (github)
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1