java原始加密算法

关键字:信息安全、古典加密、java实现

(本信息来自广州日新网,查看连接古典加密java实现

 

作者:陈奉斌 华东交通大学

个人网站:广州日新网

 

(古典加密,java实现,我大三时学信息安全时编写)

 

 

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.JEditorPane;

import java.awt.Dimension;

import javax.swing.JPasswordField;

import javax.swing.JButton;

public class zhihuanFrame extends JFrame {

 

       private static final long serialVersionUID = 1L;

       private JPanel jPanel = null;

       private JPanel jPanel1 = null;

       private JPanel jPanel2 = null;

       private JLabel jLabel = null;

       private JPanel jPanel3 = null;

       private JButton jButton = null;

       private JButton jButton1 = null;

       private JPanel jPanel4 = null;

       private JLabel jLabel1 = null;

       private JPanel jPanel5 = null;

       private JButton jButton2 = null;

       private JButton jButton3 = null;

       private JLabel jLabel2 = null;

       private JButton jButton4 = null;

       private JLabel jLabel3 = null;

       private JButton jButton5 = null;

       private JPanel jPanel6 = null;

       private String st=null;             //用于密文

       private String st1=null;            //用于明文

       private int k=0,k1=0,mk=0,mk1=0;

      

       private char[][] char1=new char[100][100];

       private char[][] char2=new char[100][100];

       private char[][] char3=new char[100][100];

       private char[][] char4=new char[100][100];

       private char[] chm=new char[100];    //用于密文

       private char[] chm1=new char[100];   //用于明文

       private char[] cps=new char[100];

       private char[] cps1=new char[100];

 

       private int[] mmkk=new int[100];

       private int[] mmk=new int[100];

       private int[] mmkk1=new int[100];

       private int[] mmk1=new int[100];

       private int[] cpsk=new int[100];

       private int[] cpsk1=new int[100];

 

       private JEditorPane jEditorPane1 = null;

       private JEditorPane jEditorPane2 = null;

       private JEditorPane jEditorPane3 = null;

       private JPasswordField jPasswordField = null;

       private JPasswordField jPasswordField1 = null;

       /**

        * This is the default constructor

        */

       public zhihuanFrame() {

              super();

              initialize();

       }

 

       /**

        * This method initializes this

        *

        * @return void

        */

       private void initialize() {

              this.setSize(463, 333);

              this.setContentPane(getJPanel());

              this.setTitle("古典置换加密器  作者:陈奉斌 QQ 402027721");

       }

 

       /**

        * This method initializes jPanel   

        *  

        * @return javax.swing.JPanel    

        */

       private JPanel getJPanel() {

              if (jPanel == null) {

                     FlowLayout flowLayout = new FlowLayout();

                     flowLayout.setAlignment(java.awt.FlowLayout.LEFT);

                     jPanel = new JPanel();

                     jPanel.setLayout(flowLayout);

                     jPanel.add(getJPanel1(), null);

                     jPanel.add(getJPanel2(), null);

              }

              return jPanel;

       }

 

       /**

        * This method initializes jPanel1 

        *  

        * @return javax.swing.JPanel    

        */

       private JPanel getJPanel1() {

              if (jPanel1 == null) {

                     FlowLayout flowLayout1 = new FlowLayout();

                     flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);

                     flowLayout1.setVgap(5);

                     flowLayout1.setHgap(5);

                    

                     jLabel = new JLabel();

                     jLabel.setText(" 请设置密码  ");

                     jPanel1 = new JPanel();

                     jPanel1.setBackground(new Color(153, 153, 255));

                     jPanel1.setLayout(flowLayout1);

                     jPanel1.setPreferredSize(new Dimension(220, 290));

                     jPanel1.add(jLabel, null);

                     jPanel1.add(getJPasswordField(), null);

                     jPanel1.add(getJPanel3(), null);

                     jPanel1.add(getJEditorPane3(), null);

                     jPanel1.add(getJPanel6(), null);

                     jPanel1.add(getJPanel4(), null);

              }

              return jPanel1;

       }

 

       /**

        * This method initializes jPanel2 

        *  

        * @return javax.swing.JPanel    

        */

       private JPanel getJPanel2() {

              if (jPanel2 == null) {

                     jLabel3 = new JLabel();

                     jLabel3.setText("明文显示区");

                     jLabel3.setFont(new Font("Dialog", Font.BOLD, 12));

                     FlowLayout flowLayout4 = new FlowLayout();

                     flowLayout4.setAlignment(java.awt.FlowLayout.LEFT);

                     flowLayout4.setVgap(5);

                     flowLayout4.setHgap(6);

                     jLabel2 = new JLabel();

                     jLabel2.setText("密文显示区");

                     jLabel2.setFont(new Font("Dialog", Font.BOLD, 12));

                     jPanel2 = new JPanel();

                     jPanel2.setBackground(new Color(113, 163, 242));

                     jPanel2.setLayout(flowLayout4);

                     jPanel2.setPreferredSize(new Dimension(220, 290));

                     jPanel2.add(jLabel2, null);

                     jPanel2.add(getJEditorPane1(), null);

                     jPanel2.add(jLabel3, null);

                     jPanel2.add(getJEditorPane2(), null);

              }

              return jPanel2;

       }

 

       /**

        * This method initializes jPanel3 

        *  

        * @return javax.swing.JPanel    

        */

       private JPanel getJPanel3() {

              if (jPanel3 == null) {

                     FlowLayout flowLayout2 = new FlowLayout();

                     flowLayout2.setAlignment(java.awt.FlowLayout.CENTER);

                     flowLayout2.setVgap(1);

                     flowLayout2.setHgap(28);

                     jPanel3 = new JPanel();

                     jPanel3.setBackground(new Color(174, 231, 225));

                     jPanel3.setLayout(flowLayout2);

                     jPanel3.setPreferredSize(new Dimension(210, 30));

                     jPanel3.add(getJButton(), null);

                     jPanel3.add(getJButton1(), null);

              }

              return jPanel3;

       }

 

       /**

        * This method initializes jButton 

        *  

        * @return javax.swing.JButton  

        */

       private JButton getJButton() {

              if (jButton == null) {

                     jButton = new JButton();

                     jButton.setText("确定");

                     jButton.addActionListener(new java.awt.event.ActionListener() {

                            public void actionPerformed(java.awt.event.ActionEvent e) {

                                   try{

                        cps=jPasswordField.getPassword();

                        k=cps.length;

                        //jEditorPane1.setText(String.valueOf(k));

                        jEditorPane2.setText(String.valueOf(cps));

                                   }catch(Exception event){}                        

                            }

                     });

                     jButton.addKeyListener(new KeyListener() {

                            public void keyPressed(KeyEvent e) {

                                   int key=e.getKeyCode();

                                   if(key==KeyEvent.VK_ENTER){

                                          cps=jPasswordField.getPassword();

                        k=cps.length;

                        jEditorPane1.setText(String.valueOf(k));

                                   }

                            }

                            public void keyTyped(java.awt.event.KeyEvent e) {

                            }

                            public void keyReleased(java.awt.event.KeyEvent e) {

                            }

                     });

              }

              return jButton;

       }

 

       /**

        * This method initializes jButton1

        *  

        * @return javax.swing.JButton  

        */

       private JButton getJButton1() {

              if (jButton1 == null) {

                     jButton1 = new JButton();

                     jButton1.setText("更改密码");

                     jButton1.addActionListener(new java.awt.event.ActionListener() {

                            public void actionPerformed(java.awt.event.ActionEvent e) {

                    cps=null;

                    k=0;

                                   jPasswordField.setText("");

                            }

                     });

              }

              return jButton1;

       }

 

       /**

        * This method initializes jPanel4 

        *  

        * @return javax.swing.JPanel    

        */

       private JPanel getJPanel4() {

              if (jPanel4 == null) {

                     FlowLayout flowLayout3 = new FlowLayout();

                     flowLayout3.setAlignment(java.awt.FlowLayout.LEFT);

                     flowLayout3.setHgap(1);

                     jLabel1 = new JLabel();

                     jLabel1.setText("输入解锁密码");

                     jPanel4 = new JPanel();

                     jPanel4.setBackground(new Color(126, 134, 21));

                     jPanel4.setLayout(flowLayout3);

                     jPanel4.setPreferredSize(new Dimension(210, 80));

                     jPanel4.add(jLabel1, null);

                     jPanel4.add(getJPasswordField1(), null);

                     jPanel4.add(getJPanel5(), null);

              }

              return jPanel4;

       }

 

       /**

        * This method initializes jPanel5 

        *  

        * @return javax.swing.JPanel    

        */

       private JPanel getJPanel5() {

              if (jPanel5 == null) {

                     FlowLayout flowLayout5 = new FlowLayout();

                     flowLayout5.setHgap(22);

                     jPanel5 = new JPanel();

                     jPanel5.setLayout(flowLayout5);

                     jPanel5.setPreferredSize(new Dimension(205, 45));

                     jPanel5.setBackground(new Color(126, 134, 26));

                     jPanel5.add(getJButton2(), null);

                     jPanel5.add(getJButton3(), null);

              }

              return jPanel5;

       }

 

       /**

        * This method initializes jButton2

        *  

        * @return javax.swing.JButton  

        */

       private JButton getJButton2() {

              if (jButton2 == null) {

                     jButton2 = new JButton();

                     jButton2.setText("确定");

                     jButton2.addActionListener(new java.awt.event.ActionListener() {

                            public void actionPerformed(java.awt.event.ActionEvent e) {

                                   try{

                                          cps1=jPasswordField.getPassword();

                        k1=cps1.length;

                                          st1=jEditorPane1.getText();

                                          chm1=st1.toCharArray();

                                          mk1=chm1.length;

                                         

                                          //根据密码的长度把输入的字符组分成一个二维数组

                                          if(mk1%k1!=0){

                                              for(int i=0,a=0;i<=((mk1-mk1%k1)/k1);i++){

                                                        for(int j=0;j<k1&&a<mk1;j++){

                                                               char3[i][j]=chm1[a];

                                                               a++;

                                                        }

                                                 }

                                          }

                                          else {

                                                 for(int i=0,a=0;i<=mk1/k1;i++){

                                                        for(int j=0;j<=k1&&a<mk1;j++){

                                                               char3[i][j]=chm1[a];

                                                               a++;

                                                     }

                                              }

                                          }

                                          //到这里结束

                                         

                                          //根据密码来分析排列顺序

                                          for(int n=0;n<k1;n++){

                                                 cpsk1[n]=cps1[n];//转换成整形

                                          }

                                          for(int n=0;n<k1;n++){

                                                 mmk1[n]=k1-1;

                                          }

                                          //排序

                                          for(int n=0;n<k1;n++){

                                                 mmkk1[n]=cps1[n];

                                                 for(int m=0;m<k1;m++){

                                                        if(mmkk1[n]<cps1[m])mmk1[n]--;

                                                        else if(mmkk1[n]==cps1[m]&&n<m)mmk1[n]--;

                                                        else mmk1[n]=mmk1[n];

                                                 }

                                          }

                                          //到这里结束

                                         

                                          //根据密码的排列顺序来排列输出的数组顺序

                                          if(mk1%k1!=0){

                                                 int i=0;

                                                 for(;i<(mk1-mk1%k1)/k1;i++)

                                                        for(int j=0;j<k1;j++)

                                                               char4[i][mmk1[j]]=char3[i][j];

                                                 for(;i==(mk1-mk1%k1)/k1;i++){

                                                        int j=0;

                                                        for(;j<k1&&mmk1[j]<=mk1%k1-1;j++)char4[i][j]=char3[i][mmk1[j]];

                                                        if(mmk1[j]>mk1%k1-1)

                                                               for(;j<k1;j++)

                                                                      for(int aj=j+1;aj<k1;aj++){

                                                                             if(mmk1[aj]<=mk1%k1-1){

                                                                                    char4[i][mmk1[aj]]=char3[i][j];

                                                                                    j++;

                                                                             }

                                                               }

                                                 }

                                          }

                                          else if(mk1%k1==0){

                                                 for(int i=0;i<=mk1/k1;i++){

                                                        for(int j=0;j<k1;j++)

                                                        char4[i][mmk1[j]]=char3[i][j];

                                                 }

                                          }

                                          //到这里结束

                                         

                                          //把二维数组变成一维数组,好转换成String类型

                                          if(mk1%k1!=0){

                                              for(int i=0,a=0;i<=((mk1-mk1%k1)/k1);i++){

                                                        for(int j=0;j<k1&&a<mk1;j++){

                                                               chm1[a]=char4[i][j];

                                                               a++;

                                                        }

                                                 }

                                          }

                                          else {

                                                 for(int i=0,a=0;i<=mk1/k1;i++){

                                                        for(int j=0;j<=k1&&a<mk1;j++){

                                                               char4[i][j]=chm1[a];

                                                               a++;

                                                     }

                                              }

                                          }

                                       //到这里结束;

                                         

                                          st1=String.valueOf(chm1);

                                          jEditorPane2.setText("");

                                          jEditorPane2.setText(st1);

                                                    

                                   }catch(Exception event){}

                            }

                     });

              }

              return jButton2;

       }

 

       /**

        * This method initializes jButton3

        *  

        * @return javax.swing.JButton  

        */

       private JButton getJButton3() {

              if (jButton3 == null) {

                     jButton3 = new JButton();

                     jButton3.setText("重新输入");

                     jButton3.addActionListener(new java.awt.event.ActionListener() {

                            public void actionPerformed(java.awt.event.ActionEvent e) {

                                   chm1=null;

                                   k1=0;

                                   mk1=0;

                    jPasswordField1.setText("");

                    jEditorPane2.setText("");

                            }

                     });

              }

              return jButton3;

       }

 

       /**

        * This method initializes jButton4

        *  

        * @return javax.swing.JButton  

        */

       private JButton getJButton4() {

              if (jButton4 == null) {

                     jButton4 = new JButton();

                     jButton4.setText("输入完毕");

                     jButton4.addActionListener(new java.awt.event.ActionListener() {

                            public void actionPerformed(java.awt.event.ActionEvent e) {

                                   try{

                                          st=jEditorPane3.getText();

                                          chm=st.toCharArray();

                                          mk=chm.length;

                                         

                                          //根据密码的长度把输入的字符组分成一个二维数组

                                          if(mk%k!=0){

                                              for(int i=0,a=0;i<=((mk-mk%k)/k);i++){

                                                        for(int j=0;j<k&&a<mk;j++){

                                                               char1[i][j]=chm[a];

                                                               a++;

                                                        }

                                                 }

                                          }

                                          else {

                                                 for(int i=0,a=0;i<=mk/k;i++){

                                                        for(int j=0;j<=k&&a<mk;j++){

                                                               char1[i][j]=chm[a];

                                                               a++;

                                                     }

                                              }

                                          }

                                          //到这里结束

                                         

                                          //根据密码来分析排列顺序

                                          for(int n=0;n<k;n++){

                                                 cpsk[n]=cps[n];//转换成整形

                                          }

                                          for(int n=0;n<k;n++){

                                                 mmk[n]=k-1;

                                          }

                                          //排序

                                          for(int n=0;n<k;n++){

                                                 mmkk[n]=cps[n];

                                                 for(int m=0;m<k;m++){

                                                        if(mmkk[n]<cps[m])mmk[n]--;

                                                        else if(mmkk[n]==cps[m]&&n<m)mmk[n]--;

                                                        else mmk[n]=mmk[n];

                                                 }

                                          }

                                          //到这里结束

                                         

                                          //根据密码的排列顺序来排列输出的数组顺序

                                          if(mk%k!=0){

                                                 int i=0;

                                                 for(;i<(mk-mk%k)/k;i++)

                                                        for(int j=0;j<k;j++)

                                                               char2[i][j]=char1[i][mmk[j]];

                                                 for(;i==(mk-mk%k)/k;i++){

                                                        int j=0;

                                                        for(;j<k&&mmk[j]<=mk%k-1;j++)char2[i][j]=char1[i][mmk[j]];

                                                        if(mmk[j]>mk%k-1)

                                                               for(;j<k;j++)

                                                                      for(int aj=j+1;aj<k;aj++){

                                                                             if(mmk[aj]<=(mk%k-1)){

                                                                                    char2[i][j]=char1[i][mmk[aj]];

                                                                                 j++;

                                                                             }

                                                               }

                                                 }

                                          }

                                          else if(mk%k==0){

                                                 for(int i=0;i<=mk/k;i++){

                                                        for(int j=0;j<k;j++)

                                                        char2[i][j]=char1[i][mmk[j]];

                                                 }

                                          }

                                          //到这里结束

                                         

                                          //把二维数组变成一维数组,好转换成String类型

                                          if(mk%k!=0){

                                              for(int i=0,a=0;i<=((mk-mk%k)/k);i++){

                                                        for(int j=0;j<k&&a<mk;j++){

                                                               chm[a]=char2[i][j];

                                                               a++;

                                                        }

                                                 }

                                          }

                                          else {

                                                 for(int i=0,a=0;i<=mk/k;i++){

                                                        for(int j=0;j<=k&&a<mk;j++){

                                                               char2[i][j]=chm[a];

                                                               a++;

                                                     }

                                              }

                                          }

                                       //到这里结束;

                                         

                                          st=String.valueOf(chm);

                                          jEditorPane1.setText("");

                                          jEditorPane1.setText(st);

                                   }

                                   catch(Exception event){}

                            }

                     });

              }

              return jButton4;

       }

 

       /**

        * This method initializes jButton5

        *  

        * @return javax.swing.JButton  

        */

       private JButton getJButton5() {

              if (jButton5 == null) {

                     jButton5 = new JButton();

                     jButton5.setText("更改输入");

                     jButton5.addActionListener(new java.awt.event.ActionListener() {

                            public void actionPerformed(java.awt.event.ActionEvent e) {

                                   chm=null;

                                   mk=0;

                                   jEditorPane3.setText("");

                                   jEditorPane1.setText("");

                            }

                     });

              }

              return jButton5;

       }

 

       /**

        * This method initializes jPanel6 

        *  

        * @return javax.swing.JPanel    

        */

       private JPanel getJPanel6() {

              if (jPanel6 == null) {

                     FlowLayout flowLayout6 = new FlowLayout();

                     flowLayout6.setHgap(13);

                     flowLayout6.setVgap(0);

                     jPanel6 = new JPanel();

                     jPanel6.setLayout(flowLayout6);

                     jPanel6.setPreferredSize(new Dimension(210, 29));

                     jPanel6.setBackground(new Color(153, 153, 255));

                     jPanel6.add(getJButton4(), null);

                     jPanel6.add(getJButton5(), null);

              }

              return jPanel6;

       }

 

       /**

        * This method initializes jEditorPane1 

        *  

        * @return javax.swing.JEditorPane   

        */

       private JEditorPane getJEditorPane1() {

              if (jEditorPane1 == null) {

                     jEditorPane1 = new JEditorPane();

                     jEditorPane1.setPreferredSize(new Dimension(209, 112));

              }

              return jEditorPane1;

       }

 

       /**

        * This method initializes jEditorPane2 

        *  

        * @return javax.swing.JEditorPane   

        */

       private JEditorPane getJEditorPane2() {

              if (jEditorPane2 == null) {

                     jEditorPane2 = new JEditorPane();

                     jEditorPane2.setPreferredSize(new Dimension(209, 112));

              }

              return jEditorPane2;

       }

 

       /**

        * This method initializes jEditorPane3 

        *  

        * @return javax.swing.JEditorPane   

        */

       private JEditorPane getJEditorPane3() {

              if (jEditorPane3 == null) {

                     jEditorPane3 = new JEditorPane();

                     jEditorPane3.setPreferredSize(new Dimension(210, 110));

                     jEditorPane3.setText("请在这里输入,你要加密的内容.");

                     jEditorPane3.addMouseListener(new java.awt.event.MouseAdapter() {

                            public void mousePressed(java.awt.event.MouseEvent e) {

                                   jEditorPane3.setText("");

                            }

                     });

              }

              return jEditorPane3;

       }

 

       /**

        * This method initializes jPasswordField    

        *  

        * @return javax.swing.JPasswordField     

        */

       private JPasswordField getJPasswordField() {

              if (jPasswordField == null){

                     jPasswordField = new JPasswordField();

                     jPasswordField.setPreferredSize(new Dimension(130, 22));

              }

              return jPasswordField;

       }

 

       /**

        * This method initializes jPasswordField1  

        *  

        * @return javax.swing.JPasswordField     

        */

       private JPasswordField getJPasswordField1() {

              if (jPasswordField1 == null) {

                     jPasswordField1 = new JPasswordField();

                     jPasswordField1.setPreferredSize(new Dimension(130, 22));

              }

              return jPasswordField1;

       }

 

}  //  @jve:decl-index=0:visual-constraint="17,-13"

 

 

 

 

import javax.swing.JFrame;

 

public class zhihuan {

       public static void main(String[] args){

              zhihuanFrame frame = new zhihuanFrame();

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frame.setVisible(true);

       }2010-11-26

}

 

 

(本信息来自广州日新网,查看连接古典加密java实现

 

作者:陈奉斌 华东交通大学

个人网站:广州日新网

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值