rsa加密 java

来源:互联网 发布:js for循环中调用函数 编辑:程序博客网 时间:2024/06/01 08:13

rsa加密数据(底层实现,不需要打入库或jar包)带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(400,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();}}
0 0
原创粉丝点击