Simple Columnar Transposition Technique(简单分栏式变换加密技术)

简单分栏式变换加密技术:
  (1)将明文消息一行一行写入预定长度的矩形中
  (2)一列一列读消息,但不一定按1、2、3列的顺序,也可以按随机顺序,如2、3、1
  (3)得到的消息就是密文消息
  注:密钥为字符的话,就根据ASII码的大小进行组合。
  实现的算法:按照排序好的数组bufClone的字符遍历寻找对应buf的字符的位置,然
  后取出该位置整数倍的位置的字符
  SimpleColumar.java
 1 package SimpleColumnarTransposition;
 2 import javax.swing.JFrame;
 3 public class SimpleColumar {
 4     public static void main(String[] args) {
 5         //实例化一个窗体
 6         SimpleColumnarFrame frame = new SimpleColumnarFrame();
 7         frame.setVisible(true);
 8         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 9     }    
10 }
11 
12 

  SimpleColumnarFrame.java
  1 package SimpleColumnarTransposition;
  2 
  3 import java.awt.event.ActionEvent;
  4 import java.awt.event.ActionListener;
  5 import java.util.Arrays;
  6 
  7 import javax.swing.*;
  8 
  9 @SuppressWarnings({ "static-access""static-access""static-access""static-access" })
 10 public class SimpleColumnarFrame extends JFrame {
 11     JButton btnKey = new JButton();
 12     JButton btnReset = new JButton();
 13     JLabel lbTest = new JLabel();
 14     JLabel lbKey = new JLabel();
 15     JPasswordField jpf = new JPasswordField();
 16     JTextField tfDarkPass = new JTextField();
 17     JTextField tfKey = new JTextField();
 18     char[] arr;
 19     byte[] buf,bufClone;
 20     
 21     public SimpleColumnarFrame() {
 22         this.setSize(300,200);        //设置窗体大小
 23         this.setTitle("简单变换栅栏加密法");
 24         this.setResizable(false);    //设置窗体大小不可变
 25         Init();        //填充窗体内容
 26     }
 27     
 28     
 29     private void makeBrightText() {
 30         tfDarkPass.setEditable(true);
 31         arr = jpf.getPassword();     //获取明文
 32         buf = tfKey.getText().getBytes();     //获取密钥,用ASII码进行比较
 33         bufClone = buf.clone();     //克隆数组
 34         Arrays.sort(bufClone);         // 根据ASII码排序
 35         reform();        //重组字符
 36         tfDarkPass.setEditable(false);
 37     }
 38     
 39     private  void reform() {
 40         /* 按照排序好的数组bufClone的字符遍历寻找对应buf的字符的位置,然
 41            后取出该位置整数倍的位置的字符*/
 42         char[] a = new char[arr.length];
 43         int index = 0;
 44         for(int i=0;i<bufClone.length;i++)
 45             for(int j=0;j<buf.length;j++
 46                 if(buf[j]==bufClone[i]) 
 47                     for(int k=0;k<arr.length;k++
 48                         if((k+1)%buf.length==(j+1)%buf.length)
 49                             a[index++= arr[k];        
 50         String str = new String(a);
 51         tfDarkPass.setText(str);
 52     }
 53     
 54     private void reset() {
 55         tfDarkPass.setEditable(true);
 56         jpf.setText("");
 57         tfKey.setText("");
 58         tfDarkPass.setText("");
 59         tfDarkPass.setEditable(false);
 60     }
 61 
 62     private void Init() {
 63         //    设置窗体内容        
 64         this.setLayout(null);
 65         lbTest.setText("输入明文:"); 
 66         lbTest.setBounds(30208030);
 67         lbKey.setText("密钥: ");
 68         lbKey.setBounds(30708030);
 69         btnKey.setText("生成密文:");
 70         btnKey.setBounds(7511010030);    
 71         btnReset.setText("重置");
 72         btnReset.setBounds(51106030);    
 73         jpf.setBounds(160208030);
 74         jpf.setEchoChar('*');        
 75         tfDarkPass.setBounds(1901108030);
 76         tfDarkPass.setEditable(false);
 77         tfKey.setBounds(160708030);
 78         
 79         this.add(lbTest);
 80         this.add(jpf);
 81         this.add(lbKey);
 82         this.add(btnKey);
 83         this.add(btnReset);
 84         this.add(tfDarkPass);
 85         this.add(tfKey);
 86         
 87         btnKey.addActionListener(new ActionListener(){
 88             //触发事件,生成明文
 89             public void actionPerformed(ActionEvent e) {
 90                 makeBrightText();            
 91             }
 92 
 93         });
 94         
 95         btnReset.addActionListener(new ActionListener(){
 96             //触发事件,生成明文
 97             public void actionPerformed(ActionEvent e) {
 98                 reset();    //    重置    
 99             }
100 
101         });
102     }
103     
104 }
105 

 现实图解:
 SimpleColumar.jpg
 输入明文及密钥,点击生成密文按钮,则产生相应密文,重置,则重新填写相关信息。
 

posted on 2008-09-08 11:57 nonels 阅读(769) 评论(0)  编辑  收藏 所属分类: J2SE


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(2)

随笔分类(16)

随笔档案(16)

搜索

最新评论

阅读排行榜

评论排行榜