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";//////secret key//int[][] key = {{ 1,4,3},{5,6}};//Get maximum column int column = key[key.length-1][(key[key.length-1].length) -1];//get maximum row int row = calcrow (p,column);//Get a one-dimensional encrypted array via key for 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&NBSP;XC&NBSP;: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&Nbsp;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]] = &NBSP;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;}}
This article from "Try" blog, reproduced please contact the author!
Java Implementation column substitution password