JAVA_维吉利亚密码_加密算法

Look类 界面

package jiemian;  
import java.awt.*;  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;  
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
  
public class Look extends JFrame{  //界面  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
         
    }  
    
    public Look()  
    {  
    	 
          final JTextArea area1=new JTextArea(8, 32);//构造一个文本域  
          area1.setLineWrap(true);//如果内容过长,自动换行,在文本域加上滚动条,水平和垂直滚动条始终出现。  
          JScrollPane pane=new JScrollPane(area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);  
          final JTextField area2=new JTextField(15);
          JLabel jLabel=new JLabel("明文:"); 
          JLabel jLabe2=new JLabel("密钥:"); 
          JButton jBu1 = new JButton("加密");
          JButton jBu2 = new JButton("重置");
        //改变窗体大小  
          this.setResizable(false);  
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
          
          this.setVisible(true);
          this.setLayout(new  FlowLayout());  
          this.add(jLabel);  
          this.add(pane);  
          this.add(jLabe2);
          this.add(area2);
          this.add(jBu1);
          this.add(jBu2);
          this.setSize(400,250);  
          this.setLocation(700, 500);  
          this.setVisible(true);  
          
          jBu1.addActionListener(new ActionListener() {
              
              public void actionPerformed(ActionEvent e) {
                  // 进行逻辑处理即可
            	 Vigenre vigenre = new Vigenre();
            	 vigenre.jiami(area1.getText(),area2.getText());
              }

          }); 
          
          jBu2.addActionListener(new ActionListener() {
              
              public void actionPerformed(ActionEvent e) {
                  // 进行逻辑处理即可
                  System.out.println("触发了重置");
              }

          });
    }  

  
} 

Vigenre.java类  算法类
package jiemian;

import java.io.File; 
import java.io.FileWriter;
import java.io.IOException;

public class Vigenre {	//加密算法
	 public static void main(String[] args) {  
	        // TODO Auto-generated method stub  
		 Look test3=new Look(); 
	    }

	public void jiami(String mingWen,String miYao) {
	        String strW=mingWen.replaceAll(" ","");	//去掉所有空格
	        System.out.println(strW);
	        String strY=miYao.replaceAll(" ","");
	        System.out.println(strY);
	        int N =strW.length();;
//	        int N=strW.length()<strY.length()? strW.length() : strY.length();
	       
	        char [] strM=new char[strW.length()];
	        	for(int i =0; i<N;i++){
	        		int W=arr((strW.charAt(i)));  //a为0
	        		int Y1=i%strY.length();
	        		int Y=arr((strY.charAt(Y1)));
	        		int Z=(W + Y)%26;
	        		char x = aee(Z);
//	        		System.out.println(strW.charAt(i));
//	        		System.out.println(x);
	        		strM[i] = x;
	        	}
	        	String miwen = new String(strM);
	        	System.out.println(miwen);
	        	writeFile(miwen);
	}
	
	int arr(char c){//字母转数字
		 int b = 28;
		 char[] arr = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
		 for(int i =0; i<26; i++){
			 if(arr[i]==c){
				 return i;
			 }
		 }
		 return b;
	}
	
	char aee(int c){//数字转字母
		 char b = 'a';
		 char[] arr = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
		 for(int i =0; i<26; i++){
			 if(i==c){
				 return arr[i];
			 }
		 }
		 return b;
	}
	
	void writeFile(String c){
		 try  
		    {      
			 String dirName = "D:/Bob";  
		     createDir(dirName); 
		      // 创建文件对象  
		      File fileText = new File("D:/Bob/Bob.txt");  
		      // 向文件写入对象写入信息  
		      FileWriter fileWriter = new FileWriter(fileText);  
		  
		      // 写文件        
		      fileWriter.write(c);  
		      // 关闭  
		      fileWriter.close();  
		    }  
		    catch (IOException e)  
		    {  
		      //  
		      e.printStackTrace();  
		    }  
		 System.out.println("加密成功");
	}
	
	 public static boolean createDir(String destDirName) {  //创建文件夹目录
	        File dir = new File(destDirName);  
	        if (dir.exists()) {  
	            System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");  
	            return false;  
	        }  
	        if (!destDirName.endsWith(File.separator)) {  
	            destDirName = destDirName + File.separator;  
	        }  
	        //创建目录  
	        if (dir.mkdirs()) {  
	            System.out.println("创建目录" + destDirName + "成功!");  
	            return true;  
	        } else {  
	            System.out.println("创建目录" + destDirName + "失败!");  
	            return false;  
	        }  
	    }  

}

需要将两个类的代码添加进项目包才能运行,其中Look.java是界面 Vigenre.java是算法类

    原文作者:维吉尼亚加密问题
    原文地址: https://blog.csdn.net/Plum_guo/article/details/78608570
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞