Rail Fence Technique:变换加密技术一种。
1、将明文消息写成对角线序列
2、将第一步写出的明文读入序列
RailFence.java
1 import java.io.BufferedReader;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.PrintWriter;
6
7 public class RailFence {
8
9 public static void main(String[] args) {
10 char[] buf;
11 String str = new String("");
12 StringBuffer sb1 = new StringBuffer("");
13 StringBuffer sb2 = new StringBuffer("");
14
15 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
16 try {
17 PrintWriter out = new PrintWriter(new FileWriter("d:""czl.txt"));
18 while((str=in.readLine())!= null) {
19 if(str.equalsIgnoreCase("exit")) break; //退出操作
20 System.out.println("明文:"+str); //将输入的字符串打印到控制台
21 out.println("============栅栏加密技术=============");
22 out.println("明文:"+str);
23 buf = str.toCharArray(); //将输入的字符串转变为字符数组
24 for(int i=0;i<buf.length;i++) {
25 //将位置为0、2、4..放在sb1中,单数放sb2中
26 if(i%2 == 0)
27 sb1.append(buf[i]);
28 else
29 sb2.append(buf[i]);
30 }
31 out.println("密文:"+sb1.append(sb2)); //合并两个数组
32 }
33 out.flush();
34 out.close();
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38
39 }
40
41 }