置换密码的加密解密

Java实现置换密码

代码

分组长度为7
加密顺序为{5, 3, 2, 0, 6, 4, 1}

package com.jingfei.Encryptoin;

import java.util.Scanner;

public class ZhiHuan {
    public static void main(String[] args) {

        final char[] CHARS = {'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', '0'
                , '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        int leng = 0;
        final int N = 7;//分组长度
        char[] transChars = new char[N];
        final int[] encryptionWay = {5, 3, 2, 0, 6, 4, 1};//规定置换顺序
        final int[] decryptionWay = {3, 6, 2, 1, 5, 0, 4};

        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要加密的字符:");
        String m = scanner.nextLine();

        StringBuffer m1 = new StringBuffer(m);//14
        int add = 0;//补位添加的0的个数
        System.out.println("加密前:\n" + m1);
        if (m1.length() % N != 0) {
            leng = m1.length() / N + 1;
            //System.out.println(leng);
            add = N - m1.length() % N;
            for (int i = 0; i < add; i++) {
                m1.append(0);
                //补足7的整数倍
            }
        } else {
            leng = m1.length() / N;
        }
        System.out.println("补充后:\n" + m1);

        //将输入的字符串,分组存到二维数组中
        char[][] arr = new char[leng][N];//{{},{}}
        for (int i = 0; i < leng; i++) {
            for (int j = i * N, z = 0; j < N * (i + 1) && z < N; j++, z++) {
                arr[i][z] = m1.charAt(j);

            }
        }

        //加解密就置换数组内容不一样,其他都一样就抽方法了
        //加密

        System.out.println("加密后:");
        encryptionAndDecryption(leng, N, transChars, encryptionWay, arr);
        print(leng, arr);

        //解密
        System.out.println("解密后:");
        encryptionAndDecryption(leng, N, transChars, decryptionWay, arr);
        char[] charsRemove = new char[N-add];
        for (int i = 0; i < N-add; i++) {
            charsRemove[i]=arr[arr.length-1][i];//去除末尾添加的0
        }
        arr[arr.length-1]=charsRemove;
        print(leng, arr);
    }

    static void print(int leng, char[][] arr) {
        for (int i = 0; i < leng; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]);//遍历全部置换后的加密结果
            }
        }
        System.out.println();
    }

    static void encryptionAndDecryption(int leng, int n, char[] transChars, int[] encryptionWay, char[][] arr) {
        for (int i = 0; i < leng; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                transChars[j] = arr[i][encryptionWay[j]];//遍历每个二维数组进行置换

            }
            for (int k = 0; k < n; k++) {
                arr[i][k] = transChars[k];//将每一维的置换结果又存回去
            }
        }


    }
}

结果

输入你要加密的字符:
helloWORLD
加密前:
helloWORLD
补充后:
helloWORLD0000
加密后:
WllhOoe00DR00L
解密后:
helloWORLD

Process finished with exit code 0

-------------------------------------------------------------
谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值