⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsa.java

📁 rsa算法演示
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.awt.event.*;
import java.math.*;
import java.util.*;
import java.awt.*;
import java.io.*;

public class RSA 
{
	public static void main(String[] args)
	{
		MyFrame frame = new MyFrame();
		MyPanel_fbutton panel_fbutton = new MyPanel_fbutton(frame,frame.P,frame.Q,frame.d,frame.e);
		FlowLayout fl = new FlowLayout (FlowLayout.CENTER,0,0);
		frame.setLayout(fl);
		frame.add(panel_fbutton);
		frame.setBounds( 220, 100, 580, 620 );
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);	
	}
	
}

class MyFrame extends JFrame
{
	public MyFrame()
	{
		setTitle("网络课程设计 RSA算法");
		
		add(wel);
		MyPanel_p panel_p = new MyPanel_p(P);
		add(panel_p);
		MyPanel_q panel_q = new MyPanel_q(Q);
		add(panel_q);
		MyPanel_d panel_d = new MyPanel_d(d);
		add(panel_d);
		MyPanel_e panel_e = new MyPanel_e(e);
		add(panel_e);
		
		MyPanel_in panel_in = new MyPanel_in(input);
		add(panel_in);
		MyPanel_out panel_out = new MyPanel_out(output);
		add(panel_out);
		MyPanel_out1 panel_out1 = new MyPanel_out1(output1);
		add(panel_out1);		
		MyPanel_button panel_button = new MyPanel_button(P,Q,d,e,input,output,output1);
		add(panel_button);	
	}
	private JLabel wel = new JLabel("RSA算法演示 1.0版");
	protected JTextField P = new JTextField(42);
	protected JTextField Q = new JTextField(42);
	protected JTextField d = new JTextField(42);
	protected JTextField e = new JTextField(42);
	protected JTextArea input = new JTextArea(6,42);
	protected JTextArea output = new JTextArea(6,42);
	protected JTextArea output1 = new JTextArea(6,42);		
}


class MyPanel_fbutton extends JPanel
{
	public MyPanel_fbutton(Frame aframe,JTextField aP, JTextField aQ, JTextField ad, JTextField ae)
    {
    	frame =  aframe;
    	P = aP;
		Q = aQ;
		e = ae;
		d = ad;	
    	fileEncode.addActionListener(new FileEncodeListener( frame,P,Q,e,d ));
		add(fileEncode);		
    	fileDecode.addActionListener(new FileDecodeListener( frame,P,Q,e,d));
    	add(fileDecode);
    }
    private Frame frame; 
    private JTextField P;
	private JTextField Q;
	private JTextField d;
	private JTextField e;
	private JButton fileEncode = new JButton("文件加密");
	private JButton fileDecode = new JButton("文件解密");
    
}

class MyPanel_p extends JPanel
{
	public MyPanel_p(JTextField aP)
	{
		P=aP;
		add(new JLabel("质数 P:"));
		add(P);
	}
	private JTextField P;

}


class MyPanel_q extends JPanel
{
	public MyPanel_q(JTextField aQ)
	{
		Q=aQ;
		add(new JLabel("质数 Q:"));
		add(Q);
	}
	private JTextField Q;

}

class MyPanel_d extends JPanel
{
	public MyPanel_d(JTextField ad)
	{
		d=ad;
		add(new JLabel("公    钥:"));
		add(d);
	}
	private JTextField d;

}

class MyPanel_e extends JPanel
{
	public MyPanel_e(JTextField ae)
	{
		e=ae;
		add(new JLabel("私    钥:"));
		add(e);
	}
	private JTextField e;
}

class MyPanel_in extends JPanel
{
	public MyPanel_in(JTextArea ainput)
	{
		input =  ainput;
		add(new JLabel("明    文:"));
		JScrollPane jsp1 = new JScrollPane (input,v,h);
		add(jsp1);
	}
	private JTextArea input;
	int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
	int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
}

class MyPanel_out extends JPanel
{
	public MyPanel_out(JTextArea aoutput)
	{
		output = aoutput;
		add(new JLabel("暗    文:"));
		JScrollPane jsp = new JScrollPane (output,v,h);
		add(jsp);
	}
	private JTextArea output;
	int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
	int h=JScrollPane.HORIZONTAL_SCROLLBAR_NEVER;
}

class MyPanel_out1 extends JPanel
{
	public MyPanel_out1(JTextArea aoutput1)
	{
		output1 =  aoutput1;
		add(new JLabel("明    文:"));
		JScrollPane jsp = new JScrollPane (output1,v,h);
		add(jsp);
	}
	private JTextArea output1;
	int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
	int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
}

class MyPanel_button extends JPanel
{
	public MyPanel_button(JTextField aP,JTextField aQ,JTextField ad,JTextField ae,
	                      JTextArea ainput,JTextArea aoutput,JTextArea aoutput1)
	{
		
		P = aP;
		Q = aQ;
		e = ae;
		d = ad;
		input = ainput ;
		output = aoutput ;
		output1 = aoutput1 ;
		randProduce.addActionListener(new RandListener( P, Q ));
		add(randProduce);
		randD.addActionListener(new RandDListener( P, Q, d, e ));
		add(randD);
		encode.addActionListener(new EncodeListener( P, Q, d, e, input, output ));
		add(encode);
		decode.addActionListener(new DecodeListener( P, Q, d, e, output, output1 ));
		add(decode);

	}
	private JTextField P;
	private JTextField Q;
	private JTextField d;
	private JTextField e;
	private JTextArea input;
	private JTextArea output;
	private JTextArea output1;
	private JButton randProduce = new JButton("生成质数P和Q");
	private JButton randD = new JButton("生成公钥和私钥");
	private JButton encode = new JButton("加密");
	private JButton decode = new JButton("解密");
}

class FileEncodeListener implements ActionListener
{
	public FileEncodeListener(Frame f,JTextField ap, JTextField aq,JTextField ae,JTextField ad)
	{
		P = ap;
		Q = aq;
		E = ae;
		D = ad;
		fr = f;
	}
	public void actionPerformed(ActionEvent ee)
	{
		FileDialog fd = new FileDialog(fr);
		fd.setVisible(true);
		String infileName =fd.getDirectory()+fd.getFile();
		String inStr = new String();
	    inStr = PublicMethod.read(infileName);
		BigInteger PrimeP = new BigInteger(P.getText());
		BigInteger PrimeQ = new BigInteger(Q.getText());
		BigInteger n =PrimeP.multiply(PrimeQ);
		int nLen = n.bitLength();
		int m=(int)(Math.ceil((double)(nLen)/16.0));
		nLen = (nLen-1) / 16;
		String outStr = new String();
		outStr = PublicMethod.Encode(inStr,PrimeP,PrimeQ,n,nLen,m,D);
		for(i=infileName.length()-1;i>=0;--i)
		{
			if(infileName.charAt(i)=='.')  break;	
		}
		String outfileName = infileName.substring(0,i);
		outfileName = outfileName + new String(".EncodeRsa") + infileName.substring(i,infileName.length());
		PublicMethod.output(outfileName,outStr);
	}
	private JTextField P;
	private JTextField Q;
	private JTextField E;
	private JTextField D;
	private Frame fr;
	int i;
}

class FileDecodeListener implements ActionListener
{
	public FileDecodeListener(Frame f,JTextField ap, JTextField aq,JTextField ae,JTextField ad)
	{
		P = ap;
		Q = aq;
		E = ae;
		D = ad;
		fr = f;
	}
	public void actionPerformed(ActionEvent ee)
	{
		FileDialog fd = new FileDialog(fr);
		fd.setVisible(true);
		String infileName =fd.getDirectory()+fd.getFile();
		String inStr = new String();
	    inStr = PublicMethod.input(infileName);
	    System.out.println(inStr);
		BigInteger PrimeP = new BigInteger(P.getText());
		BigInteger PrimeQ = new BigInteger(Q.getText());
		BigInteger n =PrimeP.multiply(PrimeQ);
		int nLen = n.bitLength();
		int m=(int)(Math.ceil((double)(nLen)/16.0));
		nLen = (nLen-1) / 16;
		String outStr = new String();
		outStr = PublicMethod.Decode(inStr,PrimeP,PrimeQ,n,nLen,m,E);
		for(i=infileName.length()-1;i>=0;--i)
		{
			if(infileName.charAt(i)=='.')  break;	
		}
		String outfileName = infileName.substring(0,i);
		outfileName = outfileName + new String(".DecodeRsa") + infileName.substring(i,infileName.length());
	 	PublicMethod.write(outfileName,outStr);
	}
	private JTextField P;
	private JTextField Q;
	private JTextField E;
	private JTextField D;
	private Frame fr;
	int i;
}

class RandListener implements ActionListener
{
	public RandListener(JTextField aP, JTextField aQ)
	{
		P = aP;
		Q = aQ;
	}
	public void actionPerformed(ActionEvent e)
	{
		PublicMethod.GetPrime( P );
		PublicMethod.GetPrime( Q );
	}
	
	private JTextField P;
	private JTextField Q;
}

class RandDListener implements ActionListener
{
	public RandDListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae)
	{
		P = aP;
		Q = aQ;
		d = ad;
		e = ae;
	}
	public void actionPerformed(ActionEvent ee)
	{
		BigInteger PP = new BigInteger(P.getText());
		BigInteger QQ = new BigInteger(Q.getText());
		BigInteger temp = (PP.subtract(new BigInteger("1"))).multiply(QQ.subtract(new BigInteger("1")));
		BigInteger temp1;
		do
		{
			temp1=new BigInteger(100, new Random()).mod(temp);  
		}
		while(PublicMethod.MillerRobin(temp1)==false);
		d.setText(temp1.toString());
		e.setText(PublicMethod.invmod(temp1, temp).toString());
	}
	private JTextField P;
	private JTextField Q;
	private JTextField d;
	private JTextField e;
}

class EncodeListener implements ActionListener
{
	public EncodeListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae,
			JTextArea in, JTextArea out)
	{
		P = aP;
		Q = aQ;
		d = ad;
		e = ae;
		input = in;
		output = out;
	}
	public void actionPerformed(ActionEvent ee)
	{
		// TODO	
		BigInteger PrimeP = new BigInteger(P.getText());
		BigInteger PrimeQ = new BigInteger(Q.getText());
		BigInteger n =PrimeP.multiply(PrimeQ);
		int nLen = n.bitLength();
		int m=(int)(Math.ceil((double)(nLen)/16.0));
		nLen = (nLen-1) / 16;
		String inStr = input.getText();
		output.setText(PublicMethod.Encode(inStr,PrimeP,PrimeQ,n,nLen,m,e));
	}
	private JTextField P;
	private JTextField Q;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -