功能划分:
1)要有三个轮子的基本结构;
2)轮子的旋转方法的实现;
3)明文的输入及大小写的规定转化(可以直接在输入时转化);
4)密文的输出
代码:
package asouwn.com;
public class wheel {
public int []L;
private int []R;
/**
* 轮的构建
* @param L 轮的左半
* @param R 轮的右半
*/
public wheel(int[] L, int[] R) {
this.L=L.clone();
this.R=R.clone();
}
/**
* 轮的数字下标查找
* @param num 在右半轮中要查找的数
* @return 该数的下标
*/
public int search(int num){
int i;
for (i=0;i<26;i++){
if (num==R[i]){
break;
}
}
return i;
}
private void roll(int []L){
int tern=L[0];
int i;
for (i=0;i<L.length-1;i++){
L[i]=L[i+1];
}
L[i]=tern;
}
/**
* 转轮的实现,左右轮各转一圈
*/
public void Roll(){
roll(this.L);
roll(this.R);
}
}
import asouwn.com.wheel;
import java.io.*;
import java.util.regex.Pattern;
public class Main {
//慢轮
static int []S_l= {24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
static int []S_r= {21,3,15,1,19,10,14,26,20,8,16,7,22,4,11,5,17,9,12,23,18,2,25,6,24,13};
//中轮
static int []M_l= {26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,16,17,18,19,20,21,22,23,24,25};
static int []M_r= {20,1,6,4,15,3,14,12,23,5,16,2,22,19,11,18,25,24,13,7,10,8,21,9,26,17};
//快轮
static int []F_l= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,1,16,17,18,19,20,21,22,23,24,25,26};
static int []F_r= {8,18,26,17,20,22,10,3,13,11,4,23,5,24,9,12,25,16,19,6,15,21,2,7,1,14};
static char []io= new char[]{'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'};
static int roll=0;
//轮子的初始化
static wheel S;
static wheel M;
static wheel F;
/**
* 返回输入字母的下标
* @param ch 输入的字符
* @return 该字符的下标,-1时报错
*/
public static int search(char ch) {
int i;
for (i=0;i<26;i++){
if (ch==io[i]){
// System.out.println(i);
break;
}
}
if (i==26){
return -1;
}
else return i;
}
/**
* 字节流读取明文文件
* @param path 文件路径
* @return 读取到的字节流
* @throws IOException
*/
public static char[] file_reader(String path) throws IOException {
File file=new File(path);
int size= (int) file.length();
FileReader fileReader=new FileReader(file);
char[]b=new char[size];
fileReader.read(b);
fileReader.close();
return b;
}
/**
* 密文输出
* @param cyber 密文
* @throws IOException
*/
public static void file_writer(char[] cyber) throws IOException {
File file=new File("cyber.txt");
FileWriter fileWriter=new FileWriter(file);
fileWriter.write(cyber);
fileWriter.flush();
fileWriter.close();
}
/**
* 加密操作
* @param ioe 明文
* @return 密文,!号时报错
*/
public static char cyber(char ioe){
int i=search(ioe);
if (i==-1){
System.out.println("加密出错,search环节出现i=-1");
return '!';
}
int a=S.L[i];
i=S.search(a);
a=M.L[i];
i=M.search(a);
a=F.L[i];
i=F.search(a);
return io[i];
}
/**
* 主函数部分
* @param args
*/
public static void main(String[] args) throws IOException {
char io;
int i=0;
S=new wheel(S_l,S_r);
M=new wheel(M_l,M_r);
F=new wheel(F_l,F_r);
String path;//需要自己初始化为明文的txt文本地址
char[] file=file_reader(path);
char[] cyber=new char[file.length];//密文
String pattern="[a-zA-Z]";
// 字母部分加密,非字母部分直接输出
try{
do {
io = file[i];
if (Pattern.matches(pattern, String.valueOf(io))) {
io=Character.toUpperCase(io);
io = cyber(io);
if (io == '!') {
return;
}
F.Roll();
roll++;
if (roll % 26 == 0)
M.Roll();
if (roll % (26 * 26) == 0)
S.Roll();
}
cyber[i++] = io;
} while (i <= file.length);
}catch (IndexOutOfBoundsException indexOutOfBoundsException) {
throw new RuntimeException(indexOutOfBoundsException);
}
//输出密文cyber
file_writer(cyber);
}
}