本文主要是介绍置换密码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原理:
置换密码算法的原理是不改变明文字符,而是按照某一规则重新排列消息中的比特或字符顺序,才而实现明文信息的加密。加密过程:将明文中的字母按照给定的顺序安排在一个矩阵中,然后用根据密钥提供的顺序重新组合矩阵中的字母,从而形成密文。例如,明文为attack begins at five,密钥为cipher,将明文按照每行6个字母的形式排在矩阵中,形成如下形式:
根据密钥cipher中各个字母在字母表中出现的先后顺序,给定一个置换:
根据上面的置换,将原有居住中的字母按照第1列、第4裂、第5裂、第3裂、第2列、第6列的顺序排列,则有下面的形式:
当输入的字母数不是密钥长度的整数倍时,在输入的备加密字符串加上几个“#”来完成补全,然后参加到加密的过程中去,从而得到如下的密文:
a siteaetb vci #agt#knf#
源代码:
package zhihuanmima;import java.util.Arrays;
import java.util.Scanner;public class ReplaceDemo {private String sourceString = null;private String keyString = null;private int[] secretMatrix;private char groupedSource[][];public ReplaceDemo() {getSource();}public static void main(String args[]) {ReplaceDemo RD = null;Scanner scanin = null;System.out.println("输入你的选择:(“0” 代表加密 ,“1”代表解密)");scanin = new Scanner(System.in);int selected = scanin.nextInt();while (selected == 0 || selected == 1) {RD = new ReplaceDemo();RD.getTheMatrix();if (selected == 0) {RD.print1();} else {RD.print2();}System.out.println("输入你的选择:(“0” 代表加密 “1” 代表解密)");scanin = new Scanner(System.in);selected = scanin.nextInt();}}private void getSource() {Scanner scan = new Scanner(System.in);System.out.print("输入文本的内容:");sourceString = scan.nextLine();System.out.print("输入密钥:");keyString = scan.next();}/** 按照密钥形式排列原始字符*/private void groupSourceString() {int sourceLen = sourceString.length();int keyLen = keyString.length();groupedSource = new char[sourceLen / keyLen + 1][keyLen];if (sourceLen % keyLen != 0) {for (int i = 0; i < (keyLen - sourceLen % keyLen); i++) {sourceString += "#";}}sourceLen = sourceString.length();for (int i = 6, j = 0; i <= sourceLen; i += keyLen, j++) {groupedSource[j] = sourceString.substring(i - keyLen, i).toCharArray();}}/** 得到加密矩阵*/private void getTheMatrix() {char[] temp01 = keyString.toCharArray();secretMatrix = new int[keyString.length()];Arrays.sort(temp01);for (int i = 0; i < keyString.length(); i++) {for (int j = 0; j < keyString.length(); j++) {if (keyString.toCharArray()[i] == temp01[j]) {secretMatrix[i] = j;}}}}private void changeSourceMatix() {groupSourceString();char temp[][] = new char[groupedSource.length][keyString.length()];for (int i = 0; i < groupedSource.length; i++) {for (int j = 0; j < keyString.length(); j++) {temp[i][j] = groupedSource[i][j];}}for (int i = 0; i < groupedSource.length - 1; i++) {for (int j = 0; j < keyString.length(); j++) {groupedSource[i][j] = temp[i][secretMatrix[j]];}}}/** 得到加密后的字符*/private void getEncryptedString() {groupSourceString();changeSourceMatix();char temp[][] = new char[groupedSource.length][keyString.length()];for (int i = 0; i < groupedSource.length; i++) {for (int j = 0; j < keyString.length(); j++) {temp[i][j] = groupedSource[i][j];}}for (int i = 0; i < groupedSource.length - 1; i++) {for (int j = 0; j < keyString.length(); j++) {groupedSource[i][j] = temp[i][secretMatrix[j]];}}}/** 打印加密后的字符*/public void print1() {getEncryptedString();for (int i = 0; i < keyString.length(); i++) {for (int j = 0; j < groupedSource.length - 1; j++) {System.out.print(groupedSource[j][i]);}}System.out.println();}/** 得到解密后的矩阵存储形式*/private void groupEncryptedString() {int sourceLen = sourceString.length();int keyLen = keyString.length();char[][] temp = new char[keyLen][sourceLen / keyLen + 1];for (int i = 0; i < keyLen; i++) {temp[i] = sourceString.substring(i * sourceLen / keyLen,(i + 1) * sourceLen / keyLen).toCharArray();}groupedSource = new char[sourceLen / keyLen + 1][keyLen];for (int j = 0; j < sourceLen / keyLen; j++) {for (int i = 0; i < keyLen; i++) {groupedSource[j][i] = temp[i][j];}}}/** 得到解密后的字符*/private void getDecryptString() {groupEncryptedString();char temp[][] = new char[groupedSource.length][keyString.length()];for (int i = 0; i < groupedSource.length; i++) {for (int j = 0; j < keyString.length(); j++) {temp[i][j] = groupedSource[i][j];}}for (int i = 0; i < groupedSource.length - 1; i++) {for (int j = 0; j < keyString.length(); j++) {groupedSource[i][j] = temp[i][secretMatrix[secretMatrix[j]]];}}}/** 打印解密字符*/public void print2() {getDecryptString();for (int j = 0; j < groupedSource.length - 1; j++) {for (int i = 0; i < keyString.length(); i++) {System.out.print(groupedSource[j][i]);}}System.out.println();}
结果:
这篇关于置换密码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!