rsa加密 java
2018-04-10 08:35
134 查看
rsa加密数据(底层实现,不需要打入库或jar包)带UI
UI部分
//RSA加密算法部分 package rsa_test; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; 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 java.util.Scanner; public class RSA { static BigInteger m,c; static PublicKey pbkey; static PrivateKey prkey; public String enctype(String test) { String ans=null; KeyPairGenerator kpg; try { kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); KeyPair kp=kpg.genKeyPair(); PublicKey pbkey=kp.getPublic(); PrivateKey prkey=kp.getPrivate(); RSAPublicKey pbk=(RSAPublicKey)pbkey; BigInteger e=pbk.getPublicExponent(); BigInteger n=pbk.getModulus(); byte[] text; try { text = test.getBytes("UTF8"); m=new BigInteger(text); c=m.modPow(e,n); ans=c.toString(); //System.out.println(ans); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } catch (NoSuchAlgorithmException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } return ans; } public String deencypt() { String ans = null; RSAPrivateKey prk=(RSAPrivateKey)prkey; BigInteger d=prk.getPrivateExponent(); BigInteger n=prk.getModulus(); m=c.modPow(d,n); byte []mt=m.toByteArray(); try { ans=new String(mt,"UTF8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ans; //ans=new String(ans.getBytes("UTF8")); //System.out.println(ans); } public static void main(String args[]) throws Exception{ KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); KeyPair kp=kpg.genKeyPair(); pbkey=kp.getPublic(); prkey=kp.getPrivate(); RSAPublicKey pbk=(RSAPublicKey)pbkey; BigInteger e=pbk.getPublicExponent(); BigInteger n=pbk.getModulus(); Scanner reader=new Scanner(System.in); String test; test=reader.nextLine(); byte[] text=test.getBytes("UTF8"); m=new BigInteger(text); c=m.modPow(e,n); String ans=c.toString(); System.out.println(ans); RSAPrivateKey prk=(RSAPrivateKey)prkey; BigInteger d=prk.getPrivateExponent(); n=prk.getModulus(); m=c.modPow(d,n); byte []mt=m.toByteArray(); ans=new String(mt,"UTF8"); //ans=new String(ans.getBytes("UTF8")); System.out.println(ans); } }
UI部分
package rsa_test; import java.io.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class UI extends JFrame implements ActionListener{ private JButton confirm1,confirm2,reset; private Label []label; private TextField []text; private TextArea print,input; public UI() { setTitle("lock"); setBounds(100, 100, 500, 350); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); setVisible(true); label=new Label[5]; for(int i=0;i<5;i++) { label[i]=new Label(); } text=new TextField[4]; for(int i=0;i<4;i++) text[i]=new TextField(); label[3].setText("输入明文:"); label[1].setText("输入文件地址:"); label[2].setText("输出文件地址:"); label[0].setText("输入移位数:"); label[4].setText("加密后:"); confirm1=new JButton("确认"); confirm2=new JButton("确认"); reset=new JButton("重置"); /*label[0].setBounds(10,10,70,20); add(label[0]); text[0].setBounds(100, 10, 80, 20); add(text[0]);*/ label[3].setBounds(10,50,80,20); add(label[3]); input=new TextArea(); int h=40; input.setBounds(100, 40, 300, 30+h); add(input); confirm1.setBounds(400,45,70,20); confirm1.addActionListener(this); add(confirm1); label[1].setBounds(10,80+h,100,20); add(label[1]); text[1].setBounds(110, 80+h, 290, 20); add(text[1]); label[2].setBounds(10,110+h,100,20); add(label[2]); text[2].setBounds(110, 110+h, 290, 20); add(text[2]); confirm2.setBounds(40 fd1c 0,110+h,70,20); confirm2.addActionListener(this); add(confirm2); print=new TextArea(); label[4].setBounds(10,140+h,80,20); add(label[4]); print.setBounds(100,140+40,300,70); add(print); reset=new JButton("重置"); reset.setBounds(200,140+85+35,70,20); reset.addActionListener(this); add(reset); } public void actionPerformed(ActionEvent e) { if(e.getSource()==confirm1) { String s=input.getText(); if(s.length()<1) { JOptionPane.showMessageDialog( null , "无明文输入" ,"警告" , JOptionPane.ERROR_MESSAGE); return; } RSA t=new RSA(); print.setText(t.enctype(s)); } else if(e.getSource()==confirm2) { String in=text[1].getText(); String out=text[2].getText(); if(in.length()<1||out.length()<1) { JOptionPane.showMessageDialog( null , "输入或输出文件地址未输入" ,"警告" , JOptionPane.ERROR_MESSAGE); return; } File outf=new File(out); File inf=new File(in); if(!outf.exists()) try{ outf.createNewFile(); }catch(IOException e1) { } FileWriter outs=null; FileReader ins=null; BufferedReader bufferr=null; BufferedWriter bufferw=null; try { outs=new FileWriter(outf,true); ins=new FileReader(inf); bufferr=new BufferedReader(ins); bufferw=new BufferedWriter(outs); String line=null; while((line=bufferr.readLine())!=null) { RSA t=new RSA(); bufferw.write(t.enctype(line)); print.append(t.enctype(line)+"\n"); bufferw.newLine(); } bufferw.newLine(); bufferw.flush(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else if(e.getSource()==reset) { input.setText(""); print.setText(""); for(int i=0;i<3;i++) text[i].setText(""); } } public static void main(String args[]) { UI temp=new UI(); } }
相关文章推荐
- (转)RSA加密解密及数字签名Java实现
- C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
- Java-web中利用RSA进行加密解密操作的方法示例
- RSA加密解密java实现
- JAVA中基于RSA的签名验签/加密解密的实现
- Java使用RSA加密解密签名及校验
- java前后端RSA加密传输数据
- Java生成RSA非对称型加密的公钥和私钥(利用java API)
- 我的java web登录RSA加密
- iOS and JAVA 的 RSA 加密解密详解
- Java生成RSA非对称型加密的公钥和私钥
- JAVA中使用RSA通过秘钥文件对字符串进行加密解密
- java实现RSA的简单加密解密
- 用JAVA实现RSA加密解密
- java和php实现RSA加密互通-b
- IOS and JAVA 的 RSA 加密解密
- java RSA加密 RSA签名
- Java安全系列-RSA登录表单加密
- php 和 java RSA 对称加密互通的问题
- RSA使用javascript加密JAVA解密