【加密解密】ADFGX密码 --图形界面实现加密解密

(一)、加密:

Encrypt.java类:

package cn.hncu;

import java.util.ArrayList;
import java.util.Arrays;

public class Encrypt {
    private String message;
    private String key;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }

    public Encrypt() {
    }

    //(1)自定义字母表填满替换方格
    public  char[][] letterTable(){
        char[][] a=new char[6][6];//建立一个6行6列的二维数组,利用密文来对照此表中的每个字符,
        a[0][1]='A';a[0][2]='D';a[0][3]='F';a[0][4]='G';a[0][5]='X';
        a[1][0]='A';a[2][0]='D';a[3][0]='F';a[4][0]='G';a[5][0]='X';
        a[1][1]='b';a[1][2]='t';a[1][3]='a';a[1][4]='l';a[1][5]='p';
        a[2][1]='d';a[2][2]='h';a[2][3]='o';a[2][4]='z';a[2][5]='k';
        a[3][1]='q';a[3][2]='f';a[3][3]='v';a[3][4]='s';a[3][5]='n';
        a[4][1]='g';a[4][2]='j';a[4][3]='c';a[4][4]='u';a[4][5]='x';
        a[5][1]='m';a[5][2]='r';a[5][3]='e';a[5][4]='w';a[5][5]='y';
        return a;
    }

    //(2)获得中间替换结果
    //中间替换结果是建立一个集合,集合中的第一个list放置明文,第二个list放置替换后的结果,以供下次使用替换结果
    public ArrayList midRes(){
        char[] chMess=message.toCharArray();//将明文变为字符数组
        char[][] chLetter=letterTable();//调用整个字母表替换方格
        ArrayList chList=new ArrayList();//用于放置明文的集合
        ArrayList strList=new ArrayList();//用户放置替换结果的集合
        ArrayList list=new ArrayList();//用于放置两个集合的总集合
        list.add(chList);//将明文集合添加到list中
        list.add(strList);//将替换结果添加到list中
        //依次将明文的每个字符与字母表中的每个字符做比较
        for(int k=0;kfor(int i=0;i//第i行
                for(int j=0;j//第j列
                    if(chMess[k]==chLetter[i][j]){//第k个明文字符是否与字母表中的某一个相等
                        String ch=chMess[k]+"";//将字母转换为字符串
                        String str=(chLetter[i][0]+""+chLetter[0][j]);
                        chList.add(ch);
                        strList.add(str);
                    }
                }
            }
        }
        return list;//返回出集合结果
    }

    //(3)利用一个移位密钥加密
    //得到移位加密后的格子
    private char[][] shiftTable(){
        ArrayList list=(ArrayList) midRes().get(1);//得到中间替换结果的集合
        Object[] objs=list.toArray();//将其list转换为数组
        StringBuffer strMidRes=new StringBuffer();//此字符串用于追加加密后的每个字符
        for(Object s:objs){
            strMidRes.append(s);//进行追加
        }

        char[] chKey=key.toUpperCase().toCharArray();//将旧密钥全部变为大写并且转换为字符数组
        int n=strMidRes.length();//整个中间密文的长度
        int col=chKey.length;//新的移位加密后的格子的列数
        int row=n%col==0?n/col:n/col+1;//新的移位加密后的格子的行数
        int k=0;
        char[][] shiftTable=new char[row+1][col];//创建一个新的移位加密后的新格子
        for(int i=0;ifor(int j=0;jif(i==0){
                    shiftTable[i][j]=chKey[j];//
                }else{
                    if(k//获得中间密文的第k个
                        k++;
                    }
                    if(k==n && n%col!=0){//根据题目要求:最后不足一行的部分全部用X填充
                        for(int t=n%col;t'X';
                        }
                    }
                }
            }
        }
        return shiftTable;
    }

    //(4)按照字母顺序排列密钥,形成新密文
    public StringBuffer sortLetter(){
        StringBuffer strbuf=new StringBuffer();
        char[][] chTemp=shiftTable();//先得到整个移位后的格子
        char[] chKey=key.toUpperCase().toCharArray();
        Arrays.sort(chKey);
        for(int i=0;ifor(int j=0;j0].length;j++){
                if(chKey[i]==chTemp[0][j]){
                    for(int x=1;x" ");
                }
            }
        }
        System.out.println(strbuf);
        return strbuf;
    }

}
 
  

(二)解密:

Decrypted.java类:

package cn.hncu;

import java.util.Arrays;


public class Decrypted {
    private String ciphertext;
    private String key;
    public Decrypted(){
    }
    public String getCiphertext() {
        return ciphertext;
    }
    public void setCiphertext(String ciphertext) {
        this.ciphertext = ciphertext;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    /*排列密钥的字母顺序,根据新密钥、旧密钥排列顺序关系,
     *排列出旧密钥的密文:即得到sortLetter()数组
     */
    private char[][] sortLetter(){
        String key2=key;//将旧密钥key赋给新密钥key2
        char[] chKey=key.toCharArray();//旧密钥变为字符数组,需要使用
        char[] chKey2=key2.toCharArray();//新密钥也变为字符数组,也是需要使用到的
        Arrays.sort(chKey2);//最关键的一步:密钥排序

        char[] chCiphertext=ciphertext.replaceAll(" ", "").toCharArray();//替换密文中的所有空格为空,并将其变为字符数组
        int row=chCiphertext.length/chKey.length;//根据密文字符的总个数与密钥字符的个数计算出新格子的行数
        int col=chKey.length;//密钥字符的个数即为新格子的列数
        char[][] chCipher=new char[row][col];//建立密文二维数组

        /*
         * 用for循环依次遍历两个密钥中的所有字符,
         * 如果正好相等,则将旧密钥格子整列赋给新密钥格子对应的一列
         */
        for(int i=0;ifor(int j=0;jint len=col*j;
                if(chKey[i]==chKey2[j]){
                    for(int i2=0;i2//将整列的字符赋给新格子列
                    }
                }
            }
        }
        return chCipher;
    }
    /*
     * 解密查看中间替换结果:
     * 通过得到的反向密文,分别以两个字符为一组计算出其字符对应的坐标,
     * 将找到的字符追加到sb中,此sb便是最后解密出的明文
     */
    public StringBuffer shift(){
        StringBuffer res=new StringBuffer();
        char[][] chLetter=sortLetter();
        char[][] leters= new Encrypt().letterTable();//得到整个字母表
        char[] temp=new char[25];
        int k=0;
        for(int i=0;ifor(int j=0;j//依次拿出每个字母表中的字符
                if((k+1)%2==0){
                    int r=trans(temp[k-1]);//因为得到的是最外行和最外列的字符,需要得到其对应的数字坐标,这里用到一个转换方法
                    int c=trans(temp[k]);
                    res.append(leters[r][c]);
                }
                k++;
            }
        }
        return res;
    }

    private int trans(char ch){
        int n=0;
        switch(ch){
        case 'A':n=1;break;
        case 'D':n=2;break;
        case 'F':n=3;break;
        case 'G':n=4;break;
        case 'X':n=5;break;
        }
        return n;
    }
}

(三)、值对象:

FileMessage.java类:

package cn.hncu;

public class FileMessage {
    private String fileurlMessSrc;//加密明文地址
    private String fileurlCry;//加密密文地址
    private String fileurlCry2;//解密密文地址
    private String fileurlMessSrc2;//解密明文地址
    private String key;
    private char[][] letters;
    public String getFileurlMessSrc() {
        return fileurlMessSrc;
    }
    public void setFileurlMessSrc(String fileurlMessSrc) {
        this.fileurlMessSrc = fileurlMessSrc;
    }
    public String getFileurlCry() {
        return fileurlCry;
    }
    public void setFileurlCry(String fileurlCry) {
        this.fileurlCry = fileurlCry;
    }
    public String getFileurlCry2() {
        return fileurlCry2;
    }
    public void setFileurlCry2(String fileurlCry2) {
        this.fileurlCry2 = fileurlCry2;
    }
    public String getFileurlMessSrc2() {
        return fileurlMessSrc2;
    }
    public void setFileurlMessSrc2(String fileurlMessSrc2) {
        this.fileurlMessSrc2 = fileurlMessSrc2;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public char[][] getLetters() {
        return letters;
    }
    public void setLetters(char[][] letters) {
        this.letters = letters;
    }
}

(四)、文件工具:

FileUtils.java类:

package cn.hncu.utils;

import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JFileChooser;

import cn.hncu.Decrypted;
import cn.hncu.Encrypt;
import cn.hncu.FileMessage;

public class FileUtils {
    public static String openFile() {
        File file=null;
        try {
            JFileChooser jfile=new JFileChooser();
            jfile.showDialog(null, "打开");
            file=jfile.getSelectedFile();
            System.out.println(file.toString());
        } catch (HeadlessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            return file.toString();
        }
    }


}

(五)、图形界面:

/*
 * mainFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.ui;

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

import cn.hncu.Decrypted;
import cn.hncu.Encrypt;
import cn.hncu.FileMessage;
import cn.hncu.utils.FileUtils;

/**
 * 
 * @author __USER__
 */
public class mainFrame extends javax.swing.JFrame {

    /** Creates new form mainFrame */
    public mainFrame() {
        initComponents();
        setTitle("ADFGX密码加密和解密");

    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // "collapsed" desc="Generated Code">

    //初始化所有参数:
    FileMessage filemess=new FileMessage();


    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfdkey = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtrepl = new javax.swing.JTextArea();
        btnSave = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        tfdCry2 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfdmessSrc2 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfdmessSrc = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfdCry = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtRepRes = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtMess = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        jtCry = new javax.swing.JTextArea();
        btnEncrypt = new javax.swing.JButton();
        btnDecrypt = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        cutMenuItem = new javax.swing.JMenuItem();
        copyMenuItem = new javax.swing.JMenuItem();
        pasteMenuItem = new javax.swing.JMenuItem();
        deleteMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setFocusCycleRoot(false);
        setMinimumSize(new java.awt.Dimension(1400, 700));
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("宋体", 1, 24));
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("ADFGX\u5bc6\u7801");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(490, 10, 130, 30);

        jLabel2.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel2.setText("\u8bbe\u7f6e\u5bc6\u94a5\uff1a");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(60, 80, 90, 20);

        tfdkey.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
        getContentPane().add(tfdkey);
        tfdkey.setBounds(130, 80, 400, 30);

        jLabel3.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel3.setText("\u8bbe\u7f6e\u66ff\u6362\u65b9\u683c\uff1a");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(60, 120, 110, 20);

        jtrepl.setColumns(20);
        jtrepl.setFont(new java.awt.Font("Monospaced", 1, 18));
        jtrepl.setRows(5);
        jScrollPane1.setViewportView(jtrepl);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(60, 150, 470, 180);

        /////初始化字母表
        Encrypt en = new Encrypt();
        final char[][] chs = en.letterTable();
        for (int i = 0; i < chs.length; i++) {
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < chs[i].length; j++) {
                if (j == 0) {
                    sb.append(chs[i][j]);
                } else {
                    sb.append("\t" + chs[i][j]);
                }
            }
            String s = new String(sb);
            jtrepl.append(s + "\n");
            s = "";
        }


        /***
         * 保存设置
         */

        btnSave.setFont(new java.awt.Font("宋体", 1, 14));
        btnSave.setText("\u4fdd\u5b58\u8bbe\u7f6e");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }

            private void btnSaveActionPerformed(ActionEvent evt) {
                String key=tfdkey.getText();
                filemess.setKey(key);
                filemess.setLetters(chs);
                System.out.println("保存设置");
            }
        });
        getContentPane().add(btnSave);
        btnSave.setBounds(230, 350, 100, 30);

        jLabel4.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel4.setText("\u660e\u6587\u6587\u4ef6\uff1a");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(870, 140, 80, 30);

        /**
         * 解密:密文文件
         */
        tfdCry2.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
        tfdCry2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tfdCry2MouseClicked(evt);
            }
            private void tfdCry2MouseClicked(java.awt.event.MouseEvent evt) {
                System.out.println("解密密文文件");
                String fileurlCry2=FileUtils.openFile();
                tfdCry2.setText(fileurlCry2);
                filemess.setFileurlCry2(fileurlCry2);
            }
        });
        getContentPane().add(tfdCry2);
        tfdCry2.setBounds(630, 140, 210, 30);

        jLabel5.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel5.setText("\u5bc6\u6587\u6587\u4ef6\uff1a");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(550, 140, 80, 30);

        /**
         * 解密:明文文件
         */

        tfdmessSrc2.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
        tfdmessSrc2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tfdmessSrc2MouseClicked(evt);
            }

            private void tfdmessSrc2MouseClicked(MouseEvent evt) {
                System.out.println("解密明文文件");
                String fileurlMessSrc2=FileUtils.openFile();
                tfdmessSrc2.setText(fileurlMessSrc2);
                filemess.setFileurlMessSrc2(fileurlMessSrc2);
            }
        });
        getContentPane().add(tfdmessSrc2);
        tfdmessSrc2.setBounds(950, 140, 150, 30);

        jLabel6.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel6.setText("\u5bc6\u6587\u6587\u4ef6\uff1a");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(870, 80, 80, 30);

        /**
         * 加密:明文文件
         */

        tfdmessSrc.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
        tfdmessSrc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tfdmessSrcMouseClicked(evt);
            }

            private void tfdmessSrcMouseClicked(MouseEvent evt) {
                System.out.println("加密明文文件");
                String fileurlMessSrc=FileUtils.openFile();
                filemess.setFileurlMessSrc(fileurlMessSrc);
                tfdmessSrc.setText(fileurlMessSrc);
            }
        });
        getContentPane().add(tfdmessSrc);
        tfdmessSrc.setBounds(630, 80, 210, 30);

        jLabel7.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel7.setText("\u660e\u6587\u6587\u4ef6\uff1a");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(550, 80, 80, 30);

        /**
         * 加密密文文件
         */
        tfdCry.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
        tfdCry.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tfdCryMouseClicked(evt);
            }

            private void tfdCryMouseClicked(MouseEvent evt) {
                System.out.println("加密密文文件");
                String fileurlCry=FileUtils.openFile();
                filemess.setFileurlCry(fileurlCry);
                tfdCry.setText(fileurlCry);
            }
        });
        getContentPane().add(tfdCry);
        tfdCry.setBounds(950, 80, 150, 30);

        jLabel8.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel8.setText("\u66ff\u6362\u7ed3\u679c\uff1a");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(550, 240, 80, 30);

        jtRepRes.setColumns(20);
        jtRepRes.setEditable(false);
        jtRepRes.setFont(new java.awt.Font("Monospaced", 1, 18));
        jtRepRes.setRows(5);
        jScrollPane2.setViewportView(jtRepRes);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(630, 200, 650, 90);

        jLabel9.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel9.setText("\u660e\u6587\uff1a");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(560, 300, 50, 30);

        jLabel10.setFont(new java.awt.Font("宋体", 1, 14));
        jLabel10.setText("\u5bc6\u6587\uff1a");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(930, 310, 50, 20);

        jtMess.setColumns(20);
        jtMess.setEditable(false);
        jtMess.setFont(new java.awt.Font("Monospaced", 1, 18));
        jtMess.setRows(5);
        jScrollPane3.setViewportView(jtMess);

        getContentPane().add(jScrollPane3);
        jScrollPane3.setBounds(630, 310, 290, 250);

        jtCry.setColumns(20);
        jtCry.setEditable(false);
        jtCry.setFont(new java.awt.Font("Monospaced", 1, 18));
        jtCry.setRows(5);
        jScrollPane4.setViewportView(jtCry);

        getContentPane().add(jScrollPane4);
        jScrollPane4.setBounds(980, 310, 300, 250);

        /**
         * 按钮:加密
         */
        btnEncrypt.setFont(new java.awt.Font("宋体", 1, 14));
        btnEncrypt.setText("\u52a0\u5bc6");
        btnEncrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncryptActionPerformed(evt);
            }

            private void btnEncryptActionPerformed(ActionEvent evt) {
                System.out.println("按钮:加密");
                //FileUtils.encryptFile(filemess);

                Encrypt en=new Encrypt();
                en.setKey(filemess.getKey());
                BufferedReader br=null;
                BufferedWriter bw=null;
                try {
                    br=new BufferedReader(new FileReader(new File(filemess.getFileurlMessSrc())));
                    bw=new BufferedWriter(new FileWriter(new File(filemess.getFileurlCry())));
                    String str=null;
                    while((str=br.readLine())!=null){
                        jtMess.append(str+"\n");
                        en.setMessage(str.replaceAll(" ", "").toLowerCase());
                        String s=new String(en.sortLetter());
                        bw.write(s);
                        jtCry.append(s+"\n");
                        String s1=en.midRes().get(0).toString().replaceAll(",", " ");
                        String s2=en.midRes().get(1).toString().replaceAll(",", "");
                        jtRepRes.append(s1+"\n");
                        jtRepRes.append(s2+"\n");
                        bw.write("\r\n");
                        bw.flush();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    try {
                        br.close();
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "文件加密成功!");
                }



            }
        });
        getContentPane().add(btnEncrypt);
        btnEncrypt.setBounds(1120, 80, 80, 30);

        /**
         * 按钮:解密
         */
        btnDecrypt.setFont(new java.awt.Font("宋体", 1, 14));
        btnDecrypt.setText("\u89e3\u5bc6");
        btnDecrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDecryptActionPerformed(evt);
            }

            private void btnDecryptActionPerformed(ActionEvent evt) {
                System.out.println("按钮:解密");

                Decrypted de=new Decrypted();
                de.setKey(filemess.getKey());
                BufferedReader br=null;
                BufferedWriter bw=null;
                try {
                    br=new BufferedReader(new FileReader(new File(filemess.getFileurlCry2())));
                    bw=new BufferedWriter(new FileWriter(new File(filemess.getFileurlMessSrc2())));
                    String str=null;
                    while((str=br.readLine())!=null){
                        System.out.println("密文:"+str);
                        jtCry.append(str+"\n");
                        de.setCiphertext((str.replaceAll(" ", "").toUpperCase()));
                        String s=new String(de.shift());
                        System.out.println("明文:"+s);
                        bw.write(s);
                        jtMess.append(s+"\n");
                        bw.write("\r\n");
                        bw.flush();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    try {
                        br.close();
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                JOptionPane.showMessageDialog(null, "文件解密成功!");

            }
        });
        getContentPane().add(btnDecrypt);
        btnDecrypt.setBounds(1120, 140, 80, 30);

        fileMenu.setText("File");

        openMenuItem.setText("Open");
        fileMenu.add(openMenuItem);

        saveMenuItem.setText("Save");
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setText("Save As ...");
        fileMenu.add(saveAsMenuItem);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText("Edit");

        cutMenuItem.setText("Cut");
        editMenu.add(cutMenuItem);

        copyMenuItem.setText("Copy");
        editMenu.add(copyMenuItem);

        pasteMenuItem.setText("Paste");
        editMenu.add(pasteMenuItem);

        deleteMenuItem.setText("Delete");
        editMenu.add(deleteMenuItem);

        menuBar.add(editMenu);

        helpMenu.setText("Help");

        contentsMenuItem.setText("Contents");
        helpMenu.add(contentsMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        pack();
    }// 
    //GEN-END:initComponents



    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }// GEN-LAST:event_exitMenuItemActionPerformed

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mainFrame().setVisible(true);
            }
        });
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JButton btnDecrypt;
    private javax.swing.JButton btnEncrypt;
    private javax.swing.JButton btnSave;
    private javax.swing.JMenuItem contentsMenuItem;
    private javax.swing.JMenuItem copyMenuItem;
    private javax.swing.JMenuItem cutMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea jtCry;
    private javax.swing.JTextArea jtMess;
    private javax.swing.JTextArea jtRepRes;
    private javax.swing.JTextArea jtrepl;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem pasteMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JTextField tfdCry;
    private javax.swing.JTextField tfdCry2;
    private javax.swing.JTextField tfdkey;
    private javax.swing.JTextField tfdmessSrc;
    private javax.swing.JTextField tfdmessSrc2;
    // End of variables declaration//GEN-END:variables

}

(六)、明文文件、密文文件、解密后文件:

这里写图片描述
这里写图片描述
这里写图片描述

(七)、运行结果:

1、完整运行结果:

【加密解密】ADFGX密码 --图形界面实现加密解密_第1张图片

2、加密结果:

【加密解密】ADFGX密码 --图形界面实现加密解密_第2张图片

得到明文路径、密文路径,点击加密:

这里写图片描述

得到加密后的结果:

【加密解密】ADFGX密码 --图形界面实现加密解密_第3张图片

3、解密结果:

【加密解密】ADFGX密码 --图形界面实现加密解密_第4张图片

你可能感兴趣的:(❶JavaSE,❻项目案例演示,----,----①JavaSE项目)