using System;
using System.Text;
namespace AniNIX.Crypto {
public class CharGrid {
protected char[][] theGrid;
///
///Use this to even out grids so that columnar transpositions can be regular.
///
///The String to pad
///How wide the grid should be
///A paddded string
public static String RandPad(String input, int width) {
Random sRand = new Random();
int mod = input.Length%width;
if (mod == 0) return input;
char[] pad = new char[width-mod];
for (int i=0; i width) ? width : remainingLength];
remainingLength -= width;
for (int j=0; j < newGrid[i].Length; j++) {
newGrid[i][j] = input[k++];
}
}
return newGrid;
}
private char[][] MakeVGrid(int length, int width) {
int y = (length%width == 0) ? length/width : length/width+1;
char[][] newGrid = new char[y][];
for (int i = 0; i < y; i++) {
newGrid[i] = new char[(length > width) ? width : length];
length -= width;
}
return newGrid;
}
///
/// Make a horizontal grid from the input of certain width. Make regular if wanted.
///
/// String to make from
/// How wide a grid to make
/// Should random padding be added to make this not a jagged array
public CharGrid(String input,int width,bool isRegular=false) {
if (isRegular) input = RandPad(input,width);
theGrid = MakeGrid(input,width);
}
///
/// Make a vertical grid from the input of certain width. Make regular if wanted.
///
/// String to make from
/// How wide a grid to make
/// What order should the columns be populated in?
public CharGrid(String input,int width,int[] order) {
// Make a grid first.
theGrid = MakeVGrid(input.Length,width);
//Populate
int k = 0;
for (int j = 0; j < theGrid[0].Length; j++) {
for (int i = 0; i < theGrid.Length; i++) {
if (i != theGrid.Length-1 || order[j] < theGrid[i].Length) {
theGrid[i][order[j]] = input[k];
k++;
}
}
}
}
public override String ToString() {
StringBuilder sb = new StringBuilder();
sb.Append(String.Format("{0} {1} ------------->\n",theGrid.Length,theGrid[0].Length));
for (int j=0; j