📄 jiami.java
字号:
System.out.println(e); } } private void jmi8ActionPerformed(java.awt.event.ActionEvent evt) { try{ Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.WRAP_MODE, pubk); byte[] wrapedkey = cipher.wrap(sk); OutputStream fos = new FileOutputStream("RSA加密.dat"); ObjectOutputStream b2=new ObjectOutputStream(fos); b2.writeObject(wrapedkey); b2.close(); fos.close(); }catch(Exception e){System.out.println("error");} jTextArea2.append("\n DES密钥已加密!"); } private void jmi7ActionPerformed(java.awt.event.ActionEvent evt) { Frame f=new Frame(); f.setLocation(300,300); f.setSize(600,400); f.addWindowListener(new WindowAdapter(){ public void windowClosed(WindowEvent e){ System.exit(0); } }); FileDialog fd=new FileDialog(f,"输入要加密的密钥",FileDialog.LOAD); fd.setVisible(true); String strfile=fd.getDirectory()+fd.getFile(); try{ FileInputStream fis=new FileInputStream(strfile); ObjectInputStream ob=new ObjectInputStream(fis); sk=(SecretKey)ob.readObject(); }catch(Exception e){} jTextArea2.append("\n 要加密的密钥已输入......."); } private void jmi6ActionPerformed(java.awt.event.ActionEvent evt) { Frame f=new Frame(); f.setLocation(300,300); f.setSize(600,400); f.addWindowListener(new WindowAdapter(){ public void windowClosed(WindowEvent e){ System.exit(0); } }); FileDialog fd=new FileDialog(f,"输入对方公钥",FileDialog.LOAD); fd.setVisible(true); String strfile=fd.getDirectory()+fd.getFile(); try{ FileInputStream fis=new FileInputStream(strfile); ObjectInputStream ob=new ObjectInputStream(fis); pubk=(RSAPublicKey)ob.readObject(); }catch(Exception e){} jTextArea2.append("\n 对方公钥已输入......."); } private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) { jTextArea1.setText(""); } private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { Frame f=new Frame(); f.setLocation(300,300); f.setSize(600,400); f.addWindowListener(new WindowAdapter(){ public void windowClosed(WindowEvent e){ System.exit(0); } }); FileDialog fd=new FileDialog(f,"打开消息文件",FileDialog.LOAD); fd.setVisible(true); String strfile=fd.getDirectory()+fd.getFile(); if(strfile!=null){ try{ FileInputStream fis=new FileInputStream(strfile); int num=fis.available(); byte[] data=new byte[num]; fis.read(data); jTextArea1.append(new String(data,0,num)); }catch(Exception e){} } jTextArea2.append("\n 消息文件已打开......."); } private void jmi5ActionPerformed(java.awt.event.ActionEvent evt) { try{ FileInputStream fis1=new FileInputStream("对称加密密钥.dat"); ObjectInputStream ob=new ObjectInputStream(fis1); Key skey=(Key)ob.readObject(); Cipher cp=Cipher.getInstance("DESede"); cp.init(Cipher.ENCRYPT_MODE,skey); FileInputStream fis2=new FileInputStream("数字签名.dat"); FileOutputStream fos1=new FileOutputStream("Message.txt"); CipherOutputStream cos=new CipherOutputStream(fos1,cp); int z=0; while((z=fis2.read())!=-1){ cos.write(z); } fis2=new FileInputStream("text.dat"); while((z=fis2.read())!=-1){ cos.write(z); } cos.close(); fos1.close(); // fis3.close(); fis1.close(); fis2.close(); }catch(Exception e){System.out.println("error");} jTextArea2.append("\n 消息已加密并输出......."); } private void jmi32ActionPerformed(java.awt.event.ActionEvent evt) { Frame f=new Frame(); f.setLocation(300,300); f.setSize(600,400); f.addWindowListener(new WindowAdapter(){ public void windowClosed(WindowEvent e){ System.exit(0); } }); FileDialog fd=new FileDialog(f,"输入消息摘要",FileDialog.LOAD); fd.setVisible(true); String strfile=fd.getDirectory()+fd.getFile(); try{ FileInputStream fis=new FileInputStream(strfile); int num=fis.available(); b=new byte[num]; fis.read(b); }catch(Exception e){} jTextArea2.append("\n 消息摘要已输入......."); } private void jmi33ActionPerformed(java.awt.event.ActionEvent evt) { try{ Signature sig=Signature.getInstance("MD5WithRSA"); sig.initSign(k); sig.update(b); byte[] signedmsg=sig.sign(); FileOutputStream fos=new FileOutputStream("数字签名.dat"); fos.write(signedmsg); // for(int i=0;i<b.length;i++){ // System.out.println(signedmsg[i]+","); // } }catch(Exception e){} jTextArea2.append("\n 消息摘要已完成数字签名并输出......."); } private void jmi31ActionPerformed(java.awt.event.ActionEvent evt) { Frame f=new Frame(); f.setLocation(300,300); f.setSize(600,400); f.addWindowListener(new WindowAdapter(){ public void windowClosed(WindowEvent e){ System.exit(0); } }); FileDialog fd=new FileDialog(f,"输入私钥",FileDialog.LOAD); fd.setVisible(true); String strfile=fd.getDirectory()+fd.getFile(); try{ FileInputStream fis=new FileInputStream(strfile); ObjectInputStream ob=new ObjectInputStream(fis); k=(RSAPrivateKey)ob.readObject(); }catch(Exception e){} jTextArea2.append("\n 私钥已输入......."); } private void jmi2ActionPerformed(java.awt.event.ActionEvent evt) { try{ FileOutputStream fos=new FileOutputStream("text.dat"); String s=jTextArea1.getText(); byte[] msg=s.getBytes(); fos.write(msg); fos.close(); FileInputStream fis=new FileInputStream("text.dat"); int num=fis.available(); byte[] b=new byte[num]; fis.read(b); MessageDigest m=MessageDigest.getInstance("MD5"); m.update(b); byte ds[]=m.digest(); String result=""; for(int i=0;i<ds.length;i++){ result+=Integer.toHexString((0x000000ff & ds[i]) | 0xffffff00).substring(6); } //System.out.println(result); FileOutputStream fos1=new FileOutputStream("消息摘要.dat"); byte[] buf=result.getBytes(); fos1.write(buf); fos.close(); fis.close(); jTextField1.setText(result); }catch(Exception e){System.out.println("error");} jTextArea2.append("\n 消息摘要已计算并输出......."); } private void jmi4ActionPerformed(java.awt.event.ActionEvent evt) { try{ KeyGenerator kg=KeyGenerator.getInstance("DESede"); kg.init(168); SecretKey k=kg.generateKey(); FileOutputStream fs=new FileOutputStream("对称加密密钥.dat"); ObjectOutputStream b=new ObjectOutputStream(fs); b.writeObject(k); }catch(Exception e){} jTextArea2.append("\n 对称加密密钥已生成并输出.......") ; } private void jmi1ActionPerformed(java.awt.event.ActionEvent evt) { Frame f=new Frame(); f.setLocation(300,300); f.setSize(600,400); f.addWindowListener(new WindowAdapter(){ public void windowClosed(WindowEvent e){ System.exit(0); } }); FileDialog fd=new FileDialog(f,"保存文件",FileDialog.SAVE); fd.setVisible(true); filename=fd.getDirectory()+fd.getFile(); try{ FileOutputStream fos=new FileOutputStream(filename); String s=jTextArea1.getText(); byte[] msg=s.getBytes(); fos.write(msg); }catch(Exception e){System.out.println("IOException!!");} jTextArea2.append("\n 消息已保存......."); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new jiami().setVisible(true); } }); } private byte[] b; private RSAPublicKey pubk; private RSAPrivateKey k; private SecretKey sk; private String filename; // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenu jMenu4; private javax.swing.JMenu jMenu5; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextArea jTextArea2; private javax.swing.JTextField jTextField1; public static final javax.swing.JTextField jTextField3 = new javax.swing.JTextField(); private javax.swing.JMenuItem jmi1; private javax.swing.JMenuItem jmi2; private javax.swing.JMenuItem jmi31; private javax.swing.JMenuItem jmi32; private javax.swing.JMenuItem jmi33; private javax.swing.JMenuItem jmi4; private javax.swing.JMenuItem jmi5; private javax.swing.JMenuItem jmi6; private javax.swing.JMenuItem jmi7; private javax.swing.JMenuItem jmi8; // End of variables declaration }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -