Package demo_01;public class cipher_main {public static void main (String[] args) {//plaintext string p = "beijing2008olympicgames";//Key int[][] key = {{ 1,4,3},{5,6}}; String cip =permutationcipher.run (P, key); SYSTEM.OUT.PRINTLN (CIP); Int[][] rekey = decryptkey.run (key); String cy =permutationcipher.run (Cip, rekey); System.out.println (CY);}} Package demo_01;public class permutationcipher {public static string run ( String p,int[][] key) {////plaintext//string p = "beijing2008olympicgames";//////key// int[][] key = {{1,4,3},{5,6}};//gets the maximum column int column = key[key.length-1][(key[ Key.length-1].length) -1];//get the maximum line Int row = calcrow (p,column);//Key to obtain a one-dimensional encrypted array, easy subsequent encryption int[] Keyarry = calckeyarry (key);//Generate plaintext P-matrix char[][]mp = calcmp (P,column,row);//Generate Ciphertext matrix CHAR[][]MK = traNsposition (Mp,keyarry);//Cipher string cip = ""; for (CHAR[]&NBSP;XD&NBSP;:MK) {for (char xc : XD) {cip+=xc;}} Return cip;} Generate Ciphertext Matrix private static char[][] transposition (Char[][] mp, int[] keyarry) { char[][] newmp = new char[mp.length][mp[0].length];for (int i = 1; i < keyarry.length; i++) {for (int j = 0; j < mp.length; j++) {newmp[j][i-1]=mp[j][(Keyarry[i])-1];}} RETURN&NBSP;NEWMP;} Generate PlainText Matrix private static char[][] calcmp (String p, int column, int row) {char[][] mp = new char [row][column];char[] c = p.tochararray () ; for (int i=0, j=0, k=0; i<c.length ;i++) {mp[j][k] = c[i];k++;if (k== column) {j++;k=0;}} RETURN&NBSP;MP;} The rekey assignment , rekey is labeled as the displaced column  , and the value is the column to be replaced, rekey[0]=0private static iNt[] calckeyarry (Int[][] key) {int[] rekey = new int[key[key.length-1][(key [Key.length-1].length] -1]+1];//two-layer loop traversal keyfor (Int a[]: key) {for (int i = 0 ; i <=a.length-1; i++) {//Determines whether it is the last, if the value is first, no is the next if (i==a.length-1) {rekey[a[i]] = a[0];} ELSE{REKEY[A[I]]=A[I+1];}}} for (int i = 0 ; i<=rekey.length-1; i++) {if (rekey[i]==0) {rekey[i] without permutation = i;}} Return rekey;} Calculates the maximum column private static int calcrow (string p, int column) {char[] a = p.tochararray (); int b = a.length;int c;if (b%column==0) {C = b/column ;} else{c = (B/column) +1;} Return c;}} Package demo_01;public class decryptkey {public static int[][] run (int[][] key) {//To be Continued ... int[][] rekey = new int[key.length][];return rekey;}}
This article from "Try" blog, reproduced please contact the author!
Java Implementation column substitution password