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

📄 encrypt.java

📁 RSA算法实现
💻 JAVA
字号:
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.*;

import javax.crypto.*;
import javax.swing.JOptionPane;

import java.awt.*;
// 用RSA公钥算法进行加密
public class Encrypt extends Frame implements ActionListener{ 
	 TextField text1 ;
	 TextArea text2;
	 TextArea text3;
	 KeyPairGenerator keyGen = null;
	 byte[] plainText = null;
	 byte[] cipherText =null;
	 Cipher cipher = null;
	 KeyPair key =null;
	
	
    public void lanuch(){
    	this.setTitle("加密");
    	this.setSize(650,550);
    	this.setLocation(300,300);
    	this.setBackground(new Color(215,235,110));
    	this.setResizable(false);
    	//setBackground(Color.BLUE);
        this.setLayout(new GridLayout(3,1));
        
        Panel p1 = new Panel();      
        p1.setLayout(new FlowLayout());
        Label l1 = new Label("请你输入要加密的明文:");
        text1 = new TextField();
        //text1.setText("");
        text1.setColumns(60);
        Button button1 = new Button("加密"); 
        button1.addActionListener(this);
        p1.add(l1);
        p1.add(text1);
        p1.add(button1);
       
                      
        Panel p2 = new Panel();
        p1.setLayout(new FlowLayout());
        Label l2 = new Label("加密后的密文是:");
        text2 = new TextArea(5,40);
        text2.setColumns(60);
        Button button2 = new Button("解密");  
        button2.addActionListener(this);
        p2.add(l2);
        p2.add(text2);
        p2.add(button2);
       
               
        Panel p3 = new Panel();
        p3.setLayout(new FlowLayout());
        Label l3 = new Label("解密后的密文是:");
         text3 = new TextArea(5,40);
        text3.setColumns(60);
        p3.add(l3);
        p3.add(text3);
        
        
        this.add(p1);
        this.add(p2);
        this.add(p3);
        //this.pack();	
        
        
    	this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){
    		System.exit(0);}} );
    	this.setVisible(true);
	}
    
    //处理事件
	public void actionPerformed(ActionEvent e)  {
		
		
		if(e.getActionCommand().equals("加密")) {
			if(text1.getText().trim().equals("")){
				JOptionPane.showMessageDialog(null,"你没有输入,请你重新输入你要加密的明文!!!!!!!!");
			}else{
				String s = text1.getText();
				try {
					plainText = s.getBytes("utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				// 生成RAS密钥	
				
				
				try {
					keyGen = KeyPairGenerator.getInstance("RSA");
					keyGen.initialize(1024);	
					key = keyGen.generateKeyPair();
				} catch (NoSuchAlgorithmException e1) {
					e1.printStackTrace();
				}
				
				
				// 得到RSA cipher 对象,即是算法的提供者   
			
				try {
					cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
								
				// 使用公钥对明文进行加密
				try {
					cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
				    cipherText = cipher.doFinal(plainText);
					text2.setText(new String(cipherText,"utf-8"));
				} catch ( Exception e1) {
					e1.printStackTrace();
				}
			}
			
			//使用私钥对密文解密
			
		}else if(e.getActionCommand().equals("解密")) {
			if(text1.getText().trim().equals("") || text2.getText().trim().equals("")){
				JOptionPane.showMessageDialog(null,"你没有加密!!!");
				
			}else{
				try {
					cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
					byte[] newPlainText = cipher.doFinal(cipherText);
					text3.setText(new String(newPlainText, "utf-8"));
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
		
	}
    
	public static void main(String[] args) {	
		Encrypt  myframe = new Encrypt();
		myframe.lanuch();
		
		//输入要加密的内容
		
	}

}

⌨️ 快捷键说明

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