代码简单实现三转轮密码机

功能划分:

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);

    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ASouwn_T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值