程序笔记   发布时间:2022-06-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Java-列移位(Columnar Transposition Cipher)算法实现版本二大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

这是几年前写的旧文,此前发布Wordpress小站上,现在又重新整理。算是温故知新,后续会继续整理。如有错误望及时指出,在此感谢。

前文

这里提供一个另种版本的实现,上一篇的实现中,为了讲明白算法逻辑,写的过于啰嗦。

  • 实现思路如下:

    • 对密文按字符顺序进行排序,并记录排序索引值;
    • 对密文进行迭代,求出每个密文字符对应的目标偏移量;
    • 循环按照密文字符偏移量读取原文字符,将其写到密文字符串中;

代码如下

import java.util.Arrays;
import java.util.objects;

public class columnarTranspositionCipherTest {

    private static String mKey = "geRMAN";

    public static void main(String[] args) {
        String inputStr = "defend*the*east*wall*of*the*castle";
        String encipher = encipher(inputStr);
        String decrypt = decrypt(encipher);
        System.out.println("inputStr:" + inputStr);
        System.out.println("encipher:" + encipher);
        System.out.println("decrypt:" + decrypt);
        System.out.println("加解密结果:" + inputStr.equals(decrypt));
    }

    /**
     * 加密
     *
     * @param inputStr
     * @return
     */
    public static String encipher(String inputStr) {

        if (Objects.isNull(inputStr) || Objects.isNull(mKey)) {
            return "";
        }

        byte[] keyBytes = mKey.getBytes();
 
        int[] keyBytesSorted = getKeyBytesSorted(keyBytes);

        final int inputLength = inputStr.getBytes().length;
        String output = "";
        for (int i = 0; i < keyBytes.length; i++) {
            int r = 0;
            int arrySortedIndex = getArrySortedIndex(keyBytesSorted, i);

            int offset;
            while ((offset = r * keyBytes.length + arrySortedIndeX) < inputLength) {
                output = output + inputStr.charAt(offset);
                r++;
            }
        }
 
        return new String(output);
    }

    /**
     * 对密钥字符进行排序,返回的是密钥字符排序后值
     *
     * @param keyBytes
     * @return
     */
    private static int[] getKeyBytesSorted(byte[] keyBytes) {
  
        int[] keyBytesSorted = new int[keyBytes.length];
        for (int i = 0; i < keyBytes.length; i++) {

            byte min = 0;
            int minIndex = -1;
            for (int j = 0; j < keyBytes.length; j++) {
                if (keyBytes[j] >= 0) {
                    min = keyBytes[j];
                    minIndex = j;
                    break;
                }
            }
 
            for (int j = 0; j < keyBytes.length; j++) {
                if (keyBytes[j] >= 0 && keyBytes[j] < min) {
                    min = keyBytes[j];
                    minIndex = j;
                }
            }
 
            keyBytes[minIndex] = -1;
            keyBytesSorted[minIndex] = i;
        }
        return keyBytesSorted;
    }

    /**
     * 从密钥字符排序数组中,找出排序值对应的下标索引
     *
     * @param keyBytesSorted
     * @param num
     * @return
     */
    public static int getArrySortedIndex(int[] keyBytesSorted, int num) {
        for (int i = 0; i < keyBytesSorted.length; i++) {
            if (keyBytesSorted[i] == num)
                return i;
        }
        return -1;
    }

    /**
     * 解密
     *
     * @param inputStr
     * @return
     */
    public static String decrypt(String inputStr) {
        if (Objects.isNull(inputStr) || Objects.isNull(mKey)) {
            return "";
        }

        byte[] keyBytes = mKey.getBytes();

        int[] keyBytesSorted = getKeyBytesSorted(keyBytes);
        final int keyBytesLength = keyBytes.length;
        int[] decrypTindexArray = new int[keyBytesLength];
        
        for (int i = 0; i < keyBytesLength; i++) {
            decrypTindexArraY[i] = getArrySortedIndex(keyBytesSorted, i);
        }

        char[] inputChars = inputStr.toCharArray();
        char[] outputChars = new char[inputChars.length];

        int inpuTindex = 0;
        for (int i = 0; i < decrypTindexArray.length; i++) {
            int originIndexOffset = decrypTindexArraY[i];
            int r = 0;

            int offset;
            while ((offset = r * keyBytesLength + originIndexOffset) < inputChars.length) {
                outputChars[offset] = inputChars[inpuTindex];
                r++;
                inpuTindex++;
            }
        }
        return new String(outputChars);
    }
}

结论

整体思路更简洁,希望对大家有帮助;

大佬总结

以上是大佬教程为你收集整理的Java-列移位(Columnar Transposition Cipher)算法实现版本二全部内容,希望文章能够帮你解决Java-列移位(Columnar Transposition Cipher)算法实现版本二所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。