1、RSA 公钥算法Java代码带界面RSA公钥加密算法代码package .rsa.service;public class UnPrimeException extends Exception public UnPrimeException() public UnPrimeException(String message) super(message); public UnPrimeException(Throwable cause) super(cause); public UnPrimeException(String message, Throwable cause) super(mes
2、sage, cause); -package .rsa.service;public interface DataInfo public MyData sendData(long p, long q, long public_key, long deprime)throws UnPrimeException;-package .rsa.service;public class DataDeal implements DataInfo Override public MyData sendData(long p, long q, long public_key, long deprime) th
3、rows UnPrimeException MyData data ; if (!this.primeNumber(q)|!this.primeNumber(q) throw new UnPrimeException(非素数,请重新输入!) ; else if(deprime = this.getF(p,q) | (this.gcd(this.getF(p, q),deprime)!=1) throw new UnPrimeException(输入的公钥不符合条件!) ; else data = new MyData(p, q,public_key, deprime) ; return dat
4、a ; /判断是否为素数 public boolean primeNumber(long t) long k=0; k=(long)Math.sqrt(double)t); boolean flag=true; outer:for(int i=2;i=k;i+) if(t%i)=0) flag = false; break outer; return flag; private long gcd(long a, long b)/最大公约数 long gcd; if(b=0)gcd=a; else gcd=gcd(b,a%b); return gcd; private long getF(lon
5、g p, long q) return (p - 1)*(q -1) ; -package .rsa.service;public class MyData private long p; private long q; private long public_key ; private long deprime ; public MyData() super(); public MyData(long p, long q, long public_key, long deprime) super(); this.p = p; this.q = q; this.public_key = pub
6、lic_key; this.deprime = deprime; public long getP() return p; public void setP(long p) this.p = p; public long getQ() return q; public void setQ(long q) this.q = q; public long getDeprime() return deprime; public void setDeprime(long deprime) this.deprime = deprime; public long getPublic_key() retur
7、n public_key; public void setPublic_key(long public_key) this.public_key = public_key; public boolean toPrime()/判断最大公约数是否为1 if(this.gcd(p, q) != 1) return false ; return true ; /求最大公约数 public long gcd(long a, long b) long gcd; if(b=0)gcd=a; else gcd=gcd(b,a%b); return gcd; public long getN() return
8、p*q ; public long getF() return (p - 1)*(q -1) ; public long colum(long y,long n,long key) /加密/解密 long mul; if(key=1)mul=y%n; else mul=y*this.colum(y,n,key-1)%n; return mul; /计算得到密匙 public long getPrivate_key() long f = this.getF() ; long private_key = 1; long value=1; for(long i=1;i+) value=i*f+1;
9、if(value%this.public_key=0)& (value/this.public_key f) private_key=value/this.public_key; break ; return private_key ; /得到密文 public long getMyPrime() return this.colum(this.deprime, this.getN(), this.public_key) ; public String getStrPrime() return Long.toString(this.getMyPrime() ; /得到解密的文件 public l
10、ong getMyDeprime() return this.colum(this.getMyPrime(), this.getN(), this.getPrivate_key() ; public String getStrDeprime() return Long.toString(this.getMyDeprime() ; -package .rsa.frame;import java.awt.BorderLayout;import java.awt.Component;import java.awt.FlowLayout;import java.awt.GridLayout;impor
11、t java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPassword
12、Field;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;public class SenderFrame extends JFrame /* * */ private static final long serialVersionUID = 1L; public SenderFrame() init() ; public void init() setTitle(Sender); setSize(300, 220); setContentPane(createContentPane(); addWind
13、owListener(new WindowAdapter() public void windowClosing(WindowEvent e) client.exit(SenderFrame.this) ; ); private JPanel createContentPane() JPanel p = new JPanel(new BorderLayout(); p.add(BorderLayout.NORTH, new JLabel(发送数据, JLabel.CENTER); p.add(BorderLayout.CENTER, createCenterPane(); p.add(Bord
14、erLayout.SOUTH, createBtnPane(); p.setBorder(new EmptyBorder(6,6,6,6); return p; private JPanel createCenterPane() JPanel p= new JPanel(new BorderLayout(); p.add(BorderLayout.NORTH, createIdPwdPane(); p.setBorder(new EmptyBorder(6,6,6,6); return p; private JPanel createIdPwdPane() JPanel p= new JPan
15、el(new GridLayout(2, 2, 0, 6); p.add(createP(); p.add(createQ() ; p.add(createPublic_key() ; p.add(createPrime(); return p; private JPanel createP() JPanel p = new JPanel(new BorderLayout(5,0); p.add(BorderLayout.WEST, new JLabel(素数p:); idP = new JPasswordField(); idP.enableInputMethods(true); p.add
16、(BorderLayout.CENTER, idP); return p; private JPanel createQ() JPanel p = new JPanel(new BorderLayout(5,0); p.add(BorderLayout.WEST, new JLabel(素数q:); idQ = new JPasswordField(); idQ.enableInputMethods(true); p.add(BorderLayout.CENTER, idQ); return p; private JPanel createPublic_key() JPanel p = new
17、 JPanel(new BorderLayout(5,0); p.add(BorderLayout.WEST, new JLabel(公 钥:); public_key = new JTextField(); p.add(BorderLayout.CENTER, public_key); return p; private JPanel createPrime() JPanel p = new JPanel(new BorderLayout(5,0); p.add(BorderLayout.WEST, new JLabel(明 文:); prime = new JTextField(); p.
18、add(BorderLayout.CENTER, prime); return p; private JPanel createBtnPane() JPanel p= new JPanel(new FlowLayout(); JButton login = new JButton(发送); JButton cancel = new JButton(取消); p.add(login); p.add(cancel); getRootPane().setDefaultButton(login); login.addActionListener(new ActionListener() public
19、void actionPerformed(ActionEvent e) client.send() ; ); cancel.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) client.exit(SenderFrame.this) ; ); return p; private Client client ; public void setClient(Client client) this.client = client ; private JTextField public_k
20、ey ; private JTextField prime; private JPasswordField idP; private JPasswordField idQ; public long getP() char str = idP.getPassword(); String s = new String(str); return Integer.parseInt(s); public long getQ() char q = idQ.getPassword(); String s = new String(q); return Integer.parseInt(s) ; public
21、 long getPrime() String str = prime.getText(); return Integer.parseInt(str); public long getPublic_key() String str = public_key.getText(); return Integer.parseInt(str); -package .rsa.frame;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.Actio
22、nEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;public class ReceiverFrame extends JFrame /
23、* * */ private static final long serialVersionUID = 1L; public ReceiverFrame() this.shows() ; public void shows() setTitle(Receiver); setSize(300, 180); setContentPane(createContentPane(); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0) ; ); private JPan
24、el createContentPane() JPanel p = new JPanel(new BorderLayout(); p.add(BorderLayout.NORTH, new JLabel(接收数据, JLabel.CENTER); p.add(BorderLayout.CENTER, createCenterPane(); p.add(BorderLayout.SOUTH, createBtnPane(); p.setBorder(new EmptyBorder(6,6,6,6); return p; private JPanel createCenterPane() JPan
25、el p= new JPanel(new BorderLayout(); p.add(BorderLayout.NORTH, createCipPane(); p.setBorder(new EmptyBorder(6,6,6,6); return p; private JPanel createCipPane() JPanel p= new JPanel(new GridLayout(2, 1, 0, 6); p.add(createIdPane(); p.add(createPwdPane(); return p; private JPanel createIdPane() JPanel
26、p = new JPanel(new BorderLayout(5,0); p.add(BorderLayout.WEST, new JLabel(密文:); cipField= new JLabel(); p.add(BorderLayout.CENTER, cipField); return p; private JPanel createPwdPane() JPanel p = new JPanel(new BorderLayout(5,0); p.add(BorderLayout.WEST, new JLabel(解密:); decipField = new JLabel(); p.add(BorderLayout.CENTER, decipField); return p; private JPanel createBtnPane() JPanel p= new JPanel(new FlowLayout(); JButton button1 = new JButton(解密); JButton button = new JButton(确认); p.add(button1) ; p.add(button); getRootPane().setDefaultButton(button);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1