caofeichao
发贴: 1
积分: 2
|
于 2007-06-23 14:14
一、密钥生成器 package key; import javax.swing.UIManager; import javax.swing.*; import java.awt.*; /** * <p>Title: </p> * <p>Description: 运行环境: JDK 1.5 </p> * <p>Copyright: Copyright 2007</p> * <p>Company: </p> * @author not attributable * @version 1.0 */ public class creatkey { boolean packFrame = false;
//Construct the application public creatkey() { Frame1 frame = new Frame1(); //Validate frames that have preset sizes //Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) { frame.pack(); } else { frame.validate(); } //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); }
//Main method public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } new creatkey(); } } --------- package key; import java.io.*; import java.security.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.io.*; import javax.crypto.Cipher; import java.security.*; import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.math.BigInteger; public class Frame1 extends JFrame { JPanel contentPane; JEditorPane jEditorPane1 = new JEditorPane(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); JButton jButton4 = new JButton(); TitledBorder titledBorder1; final int KEY_SIZE=1024; KeyPair keypair; RSAPublicKey pubKey; RSAPrivateKey priKey; //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } }
//Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); titledBorder1 = new TitledBorder(""); jEditorPane1.setFont(new java.awt.Font("Dialog", 0, 13)); jEditorPane1.setForeground(Color.blue); jEditorPane1.setBorder(BorderFactory.createRaisedBevelBorder()); jEditorPane1.setToolTipText(""); jEditorPane1.setEditable(false); jEditorPane1.setText(" 系统说明 本系统用于产生RSA加/解密所需的密钥对、公钥、私钥文件,生成的以上文件位于当前" + "目录下,名称依次为keypair.dat,publickey.dat,privatekey.dat,操作时请先生成密钥对。 " + " 作者:曹飞超"); jEditorPane1.setBounds(new Rectangle(18, 37, 137, 229)); contentPane.setLayout(null); this.setSize(new Dimension(303, 320)); this.setResizable(false); this.setTitle("RSA加/解密器密钥生成器[英文版]"); jButton1.setBounds(new Rectangle(175, 38, 106, 34)); jButton1.setFont(new java.awt.Font("Dialog", 0, 13)); jButton1.setForeground(Color.black); // jButton1.setBorder(null); jButton1.setToolTipText("用于产生公钥、私钥的数据模型,如无此文件生成公、私钥功能不可用,请先产生密钥对"); //jButton1.setActionCommand("生成密钥对"); //jButton1.setHorizontalAlignment(SwingConstants.CENTER); jButton1.setText("生成密钥对"); jButton1.addActionListener(new CreatFrame1_jButton1_actionAdapter(this)); jButton2.setBounds(new Rectangle(176, 103, 104, 33)); jButton2.setFont(new java.awt.Font("Dialog", 0, 13)); jButton2.setToolTipText("用于加密"); jButton2.setText("生成公钥"); jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this)); jButton3.setBounds(new Rectangle(179, 170, 103, 33)); jButton3.setFont(new java.awt.Font("Dialog", 0, 13)); jButton3.setToolTipText("用于解密"); jButton3.setText("生成私钥"); jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this)); jButton4.setBounds(new Rectangle(178, 232, 103, 33)); jButton4.setFont(new java.awt.Font("Dialog", 0, 13)); jButton4.setForeground(Color.red); jButton4.setText("退 出"); jButton4.addActionListener(new CreatFrame1_jButton4_actionAdapter(this)); contentPane.add(jEditorPane1, null); contentPane.add(jButton1, null); contentPane.add(jButton4, null); contentPane.add(jButton2, null); contentPane.add(jButton3, null); //检查密钥对是否存在,如不存在禁止生成公、私钥 checkkeypair(); }
//Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent; if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } private void checkkeypair(){ String patht=".\\keypair"+"."+"dat"; File filecheck=new File(patht); try{ if(filecheck.exists()){ jButton2.setEnabled(true); jButton3.setEnabled(true); } else{ jButton2.setEnabled(false); jButton3.setEnabled(false); } } catch(Exception e){ e.printStackTrace(); } } //退出 void jButton4_actionPerformed(ActionEvent e) { dispose(); } //生成密钥对 void jButton1_actionPerformed(ActionEvent e) { createkeypair(); } private void createkeypair(){ try{ KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance("RSA"); // keyPairGen.initialize(KEY_SIZE); //得到密钥对-* keypair=keyPairGen.generateKeyPair(); String mishidui=".\\keypair"+"."+"dat"; File filecreat=new File(mishidui); if(filecreat.exists()){ JOptionPane.showMessageDialog(null,"密钥对文件keypair.dat已经存在,在当前目录下,如果要生成新的密约对请先移走原keypair.dat文件再生成新文件","系统提示",JOptionPane.INFORMATION_MESSAGE); return; } else{ FileOutputStream fileout = new FileOutputStream(mishidui); ObjectOutputStream fileobject = new ObjectOutputStream(fileout); fileobject.writeObject(keypair); JOptionPane.showMessageDialog(null, "密钥对文件keypair.dat已经生成,在当前目录下!", "系统提示", JOptionPane.INFORMATION_MESSAGE); jButton2.setEnabled(true); jButton3.setEnabled(true); } } catch(Exception e){ e.printStackTrace(); } } //生成公钥 void jButton2_actionPerformed(ActionEvent e) { pubKey=(RSAPublicKey)keypair.getPublic(); try{ String mishidui=".\\publickey"+"."+"dat"; File filecreat=new File(mishidui); if(filecreat.exists()){ JOptionPane.showMessageDialog(null,"公钥文件publickey.dat已经存在,在当前目录下,如果要生成新的公钥请先移走原publickey.dat文件再生成新文件","系统提示",JOptionPane.INFORMATION_MESSAGE); return; } else { FileOutputStream fileout = new FileOutputStream(mishidui); ObjectOutputStream fileobject = new ObjectOutputStream(fileout); fileobject.writeObject(pubKey); JOptionPane.showMessageDialog(null, "公钥文件publickey.dat已经生成,在当前目录下!", "系统提示", JOptionPane.INFORMATION_MESSAGE); } } catch(Exception es){ es.printStackTrace(); } } //生成私钥 void jButton3_actionPerformed(ActionEvent e) { priKey=(RSAPrivateKey)keypair.getPrivate(); try{ String siyuepath=".\\privatekey"+"."+"dat"; File filecreat=new File(siyuepath); if(filecreat.exists()){ JOptionPane.showMessageDialog(null,"私钥文件privatekey.dat已经存在,在当前目录下,如果要生成新的私钥请先移走原privatekey.dat文件再生成新文件","系统提示",JOptionPane.INFORMATION_MESSAGE); return; } else { FileOutputStream fileout = new FileOutputStream(siyuepath); ObjectOutputStream fileobject = new ObjectOutputStream(fileout); fileobject.writeObject(priKey); JOptionPane.showMessageDialog(null, "私钥文件privatekey.dat已经生成,在当前目录下!", "系统提示", JOptionPane.INFORMATION_MESSAGE); } } catch(Exception ek){ ek.printStackTrace(); }
}
} class CreatFrame1_jButton4_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
CreatFrame1_jButton4_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton4_actionPerformed; } }
class CreatFrame1_jButton1_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
CreatFrame1_jButton1_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed; } }
class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
Frame1_jButton2_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed; } }
class Frame1_jButton3_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
Frame1_jButton3_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed; } } 二、RSA加密器之加密端 package rsa; import javax.swing.UIManager; import java.awt.*;
/** * <p>Title: 物业收费</p> * <p>Description: 试用</p> * <p>Copyright: Copyright 2007</p> * <p>Company: 西安航力科技有限责任公司</p> * @author 曹飞超 * @version 1.0 */
public class RSAEncty { boolean packFrame = false;
//Construct the application public RSAEncty() { Frame1 frame = new Frame1(); //Validate frames that have preset sizes //Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) { frame.pack(); } else { frame.validate(); } //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); }
//Main method public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { e.printStackTrace(); } new RSAEncty(); } } ------------- package rsa; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import javax.crypto.Cipher; import java.security.*; import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.math.BigInteger; /** * <p>Title: RSA加密</p> * <p>Description: 试用</p> * <p>Copyright: Copyright 2007</p> * @author 曹飞超 * @version 1.0 */ public class Frame1 extends JFrame { JPanel contentPane; JFileChooser chooser=new JFileChooser(); JEditorPane jEditorPane1 = new JEditorPane(); JLabel jLabel1=new JLabel(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); File file1; //存放明文 String pre; byte[] pre_tmp; byte[] data; //存放密文 byte [] miwen; //密钥的长度 String path=".\\publickey"+"."+"dat"; RSAPublicKey enkey; final int KEY_SIZE=1024; //选种的文件名称 String filename; //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } }
//Component initialization public void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); jEditorPane1.setFont(new java.awt.Font("Dialog", 0, 16)); jEditorPane1.setBorder(BorderFactory.createRaisedBevelBorder()); jEditorPane1.setMaximumSize(new Dimension(2147483647, 2147483647)); jEditorPane1.setToolTipText(""); jEditorPane1.setCaretColor(new Color(40, 0, 0)); jEditorPane1.setEditable(false); jEditorPane1.setSelectedTextColor(UIManager.getColor("ProgressBar.foreground")); jEditorPane1.setSelectionStart(199); jEditorPane1.setText(" " + " 系统简介 RSA加密算法是专业的加密解决方案,由于非对称算法运行耗费资源,运行缓慢,本系统目前只支持对英文加解密,本系统适合小容量密级要求高的资" + "料,加密端的密钥是公钥,解密请用RSA加密器之解密端 " + " " + " ---作者:曹飞超"); jEditorPane1.setContentType("text/plain"); jEditorPane1.setBounds(new Rectangle(30, 42, 213, 279)); jLabel1.setText("RSA加密是专业的加密问题解决方案,这是系统的加密端 -作者:曹飞超"); contentPane.setLayout(null); this.setSize(new Dimension(400, 380)); this.setResizable(false); this.setTitle("RSA加密器之加密端[英文版]"); jButton1.setBounds(new Rectangle(261, 46, 104, 37)); jButton1.setFont(new java.awt.Font("Dialog", 0, 15)); jButton1.setForeground(new Color(0, 55, 0)); jButton1.setToolTipText("选择要加密的文件"); jButton1.setText("选择文件"); jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this)); jButton2.setBounds(new Rectangle(264, 157, 99, 36)); jButton2.setFont(new java.awt.Font("Dialog", 0, 15)); jButton2.setForeground(Color.green); jButton2.setToolTipText("本系统加密的公钥是publickey.dat,由工具[RSA加解密器密钥生成器]产生"); jButton2.setText("RSA加密"); jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this)); jButton3.setBounds(new Rectangle(264, 284, 99, 36)); jButton3.setFont(new java.awt.Font("Dialog", 0, 15)); jButton3.setForeground(Color.red); jButton3.setSelected(false); jButton3.setText("关闭窗口"); jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this)); contentPane.add(jEditorPane1, null); contentPane.add(jButton1, null); contentPane.add(jButton2, null); contentPane.add(jButton3, null); //判断密钥文件是否存在 jianchakey(); }
//Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent; if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } //关闭 void jButton3_actionPerformed(ActionEvent e) { dispose(); } private void jianchakey(){ String patht=".\\publickey"+"."+"dat"; File filecheck=new File(patht); try{ if(filecheck.exists()){ jButton1.setEnabled(true); jButton2.setEnabled(true); } else{ JOptionPane.showMessageDialog(null,"加密用的公钥文件不存在,请用[RSA加解密器密钥生成器]生成公钥再使用!","系统提示",JOptionPane.INFORMATION_MESSAGE); jButton1.setEnabled(false); jButton1.setToolTipText("无公钥文件,选择功能不可用"); jButton2.setEnabled(false); jButton2.setToolTipText("无公钥文件,加密功能不可用"); } } catch(Exception e){ e.printStackTrace(); }
} //选择文件 void jButton1_actionPerformed(ActionEvent e) { chooser.setDialogTitle("请选择要进行加密处理的文件"); chooser.setApproveButtonText("确定"); int m=chooser.showOpenDialog(this); try{ String url=".\\publickey"+"."+"dat"; File keyfile=new File(url); file1 = chooser.getSelectedFile(); //获取选中的文件名称 filename = file1.getAbsolutePath(); File file2 = new File(filename); //获取选中文件长度 long chang = file2.length(); if(chang==0){ JOptionPane.showMessageDialog(null,"您没有选择任何文件,无法进行加密操作!","系统提示",JOptionPane.INFORMATION_MESSAGE); return; } else{ String tmp = String.valueOf(chang); int len = Integer.parseInt(tmp); data = new byte[len]; FileInputStream filein = new FileInputStream(filename); int count=filein.read(data,0,len); } } catch(IOException ef){ System.out.println(ef); } } //加密 void jButton2_actionPerformed(ActionEvent e) { String pathe=".\\publickey"+"."+"dat"; try{ FileInputStream filein=new FileInputStream(pathe); ObjectInputStream objectin=new ObjectInputStream(filein); //读出公钥 enkey=(RSAPublicKey)objectin.readObject(); Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding"); //以加密模式初始化密钥 cipher.init(Cipher.ENCRYPT_MODE,enkey); miwen=cipher.doFinal(data); FileOutputStream fileout=new FileOutputStream(filename); int mj=miwen.length; fileout.write(miwen,0,mj); JOptionPane.showMessageDialog(null,"RSA专业加密完成,请用私钥解密!","系统提示",JOptionPane.INFORMATION_MESSAGE);
} catch(NoSuchAlgorithmException ea){ System.out.println(ea); } catch(ClassNotFoundException eg){ System.out.println(eg); } catch(FileNotFoundException el){ System.out.println(el); } catch(IOException eb){ System.out.println(eb); } catch(Exception ed){ ed.printStackTrace(); } } class Frame1_jButton3_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
Frame1_jButton3_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed; } }
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed; } } }
class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee; Frame1_jButton2_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed; } }
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed; } } 三、RSA加密器之解密端 package decryrsa; import javax.swing.UIManager; import java.awt.*;
/** * <p>Title: 物业收费</p> * <p>Description: 试用</p> * <p>Copyright: Copyright 2007</p> * <p>Company: 西安航力科技有限责任公司</p> * @author 曹飞超 * @version 1.0 */
public class Decrykey { boolean packFrame = false;
//Construct the application public Decrykey() { try{ Frame1 frame = new Frame1(); //Validate frames that have preset sizes //Pack frames that have useful preferred size info, e.g. from their layout if (packFrame) { frame.pack(); } else { frame.validate(); } //Center the window Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); } catch(Exception e){ e.printStackTrace(); } }
//Main method public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception eu) { eu.printStackTrace(); } new Decrykey(); } } -------------------------------------------------- package decryrsa; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import javax.crypto.Cipher; import java.security.*; import java.security.spec.RSAPublicKeySpec; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.math.BigInteger; public class Frame1 extends JFrame { JPanel contentPane; JFileChooser chooser=new JFileChooser(); JEditorPane jEditorPane1 = new JEditorPane(); JLabel jLabel1=new JLabel(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); File file1; //存放密文 byte [] miwen; byte[] data; String pre; byte[] pre_tmp; String filename; //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try{ jbInit1(); } catch(Exception er){ er.printStackTrace(); } }
//Component initialization public void jbInit1() throws Exception { contentPane = (JPanel) this.getContentPane(); jEditorPane1.setFont(new java.awt.Font("Dialog", 0, 16)); jEditorPane1.setBorder(BorderFactory.createRaisedBevelBorder()); jEditorPane1.setMaximumSize(new Dimension(2147483647, 2147483647)); jEditorPane1.setToolTipText(""); jEditorPane1.setCaretColor(new Color(40, 0, 0)); jEditorPane1.setEditable(false); jEditorPane1.setSelectedTextColor(UIManager.getColor("ProgressBar.foreground")); jEditorPane1.setSelectionStart(199); jEditorPane1.setText(" " + " 系统简介 RSA加密算法是专业的加密解决方案,由于非对称算法运行耗费资源,运行缓慢,本系统目前只支持对英文加解密,适合小容量密级要求高的资" + "料,解密端的密钥是私钥,解密请用RSA加密器之解密端 " + " " + " ---作者:曹飞超"); jEditorPane1.setContentType("text/plain"); jEditorPane1.setBounds(new Rectangle(30, 42, 213, 279)); jLabel1.setText("RSA加密是专业的加密问题解决方案,这是系统的解密端 -作者:曹飞超"); contentPane.setLayout(null); this.setSize(new Dimension(400, 380)); this.setResizable(false); this.setTitle("RSA加密器之解密端[英文版]"); jButton1.setBounds(new Rectangle(261, 46, 104, 37)); jButton1.setFont(new java.awt.Font("Dialog", 0, 15)); jButton1.setForeground(new Color(0, 55, 0)); jButton1.setToolTipText("选择要解密的文件"); jButton1.setText("选择文件"); jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this)); jButton2.setBounds(new Rectangle(264, 157, 99, 36)); jButton2.setFont(new java.awt.Font("Dialog", 0, 15)); jButton2.setForeground(Color.green); jButton2.setToolTipText("本系统解密的私钥是privatekey.dat,由工具[RSA加解密器密钥生成器]产生"); jButton2.setText("RSA解密"); jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this)); jButton3.setBounds(new Rectangle(264, 284, 99, 36)); jButton3.setFont(new java.awt.Font("Dialog", 0, 15)); jButton3.setForeground(Color.red); jButton3.setSelected(false); jButton3.setText("关闭窗口"); jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this)); contentPane.add(jEditorPane1, null); contentPane.add(jButton1, null); contentPane.add(jButton2, null); contentPane.add(jButton3, null); //判断密钥文件是否存在 jianchakey(); }
//Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent; if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } //关闭 void jButton3_actionPerformed(ActionEvent e) { dispose(); } private void jianchakey(){ String patht=".\\privatekey"+"."+"dat"; File filecheck=new File(patht); try{ if(filecheck.exists()){ jButton1.setEnabled(true); jButton2.setEnabled(true); } else{ JOptionPane.showMessageDialog(null,"解密用的私钥文件不存在,请用[RSA加解密器密钥生成器]生成私钥再使用!","系统提示",JOptionPane.INFORMATION_MESSAGE); jButton1.setEnabled(false); jButton1.setToolTipText("无私钥文件,选择功能不可用"); jButton2.setEnabled(false); jButton2.setToolTipText("无私钥文件,解密功能不可用"); } } catch(Exception e){ e.printStackTrace(); }
} //选择文件 void jButton1_actionPerformed(ActionEvent e) { chooser.setDialogTitle("请选择要进行解密处理的文件"); chooser.setApproveButtonText("确定"); int m=chooser.showOpenDialog(this); try{ file1 = chooser.getSelectedFile(); //获取选中的文件名称 filename = file1.getAbsolutePath(); File file2 = new File(filename); //获取选中文件长度 long chang = file2.length(); if(chang==0){ JOptionPane.showMessageDialog(null,"您没有选择任何文件,无法进行解密操作!","系统提示",JOptionPane.INFORMATION_MESSAGE); return; } else{ String tmp = String.valueOf(chang); int len = Integer.parseInt(tmp); data = new byte[len]; FileInputStream filein = new FileInputStream(filename); int count=filein.read(data,0,len); } } catch(IOException ef){ System.out.println(ef); } } //解密 void jButton2_actionPerformed(ActionEvent e1) { String pathe=".\\privatekey"+"."+"dat"; try{ FileInputStream filein=new FileInputStream(pathe); ObjectInputStream objectin=new ObjectInputStream(filein); //读出公钥 RSAPrivateKey enkey=(RSAPrivateKey)objectin.readObject(); Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding"); //以解密模式初始化密钥 cipher.init(Cipher.DECRYPT_MODE,enkey); miwen=cipher.doFinal(data); FileOutputStream fileout=new FileOutputStream(filename); int mj=miwen.length; fileout.write(miwen,0,mj); JOptionPane.showMessageDialog(null,"RSA专业解密完成,请用公钥加密!","系统提示",JOptionPane.INFORMATION_MESSAGE);
} catch(NoSuchAlgorithmException ea){ System.out.println(ea); } catch(ClassNotFoundException eg){ System.out.println(eg); } catch(FileNotFoundException el){ System.out.println(el); } catch(IOException eb){ System.out.println(eb); } catch(Exception ed){ ed.printStackTrace(); } } class Frame1_jButton3_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
Frame1_jButton3_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed; } }
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed; } } }
class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener { Frame1 adaptee; Frame1_jButton2_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e1) { adaptee.jButton2_actionPerformed(e1); } } 以上三个工具在key, rsa ,decryrsa包中 代码为本人原创,可供各位借鉴。 联系方式E-mail:caofeichao@mail.china.com
menzy edited on 2007-06-27 11:19
|