2016-08-04 12:10:50 -05:00
|
|
|
using System;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace AniNIX.Crypto {
|
|
|
|
|
|
|
|
public class CharGrid {
|
|
|
|
|
|
|
|
protected char[][] theGrid;
|
|
|
|
|
|
|
|
///<Summary>
|
|
|
|
///Use this to even out grids so that columnar transpositions can be regular.
|
|
|
|
///</summary>
|
|
|
|
///<param name=input>The String to pad</param>
|
|
|
|
///<param name=width>How wide the grid should be</param>
|
|
|
|
///<returns>A paddded string</returns>
|
|
|
|
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<pad.Length; i++) {
|
|
|
|
pad[i] = (char)(sRand.Next()%26 + (int)'A');
|
|
|
|
}
|
|
|
|
return input+(new String(pad));
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:10:34 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Create a grid from the input and row length
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="input">The string to make the grid from</param>
|
|
|
|
/// <param name="width">The width of a row</param>
|
|
|
|
/// <returns>a grid</returns>
|
2016-08-04 12:10:50 -05:00
|
|
|
private char[][] MakeGrid(String input, int width) {
|
|
|
|
int k=0;
|
|
|
|
int y=(input.Length%width == 0) ? input.Length/width : input.Length/width+1;
|
|
|
|
int remainingLength=input.Length;
|
|
|
|
char[][] newGrid = new char[y][];
|
|
|
|
for (int i=0; i < y; i++) {
|
|
|
|
newGrid[i] = new char[(remainingLength > width) ? width : remainingLength];
|
|
|
|
remainingLength -= width;
|
|
|
|
for (int j=0; j < newGrid[i].Length; j++) {
|
|
|
|
newGrid[i][j] = input[k++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newGrid;
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:10:34 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Create a grid from a width and length
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="length">The length of a column </param>
|
|
|
|
/// <param name="width">The width of a row</param>
|
|
|
|
/// <returns>a grid</returns>
|
2016-08-04 12:10:50 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Make a horizontal grid from the input of certain width. Make regular if wanted.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name=input>String to make from</param>
|
|
|
|
/// <param name=width>How wide a grid to make </param>
|
|
|
|
/// <param name=isRegular>Should random padding be added to make this not a jagged array</param>
|
|
|
|
public CharGrid(String input,int width,bool isRegular=false) {
|
|
|
|
if (isRegular) input = RandPad(input,width);
|
|
|
|
theGrid = MakeGrid(input,width);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Make a vertical grid from the input of certain width. Make regular if wanted.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name=input>String to make from</param>
|
|
|
|
/// <param name=width>How wide a grid to make </param>
|
|
|
|
/// <param name=order>What order should the columns be populated in?</param>
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:10:34 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Create a string representation
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>representation</returns>
|
2016-08-04 12:10:50 -05:00
|
|
|
public override String ToString() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2016-09-21 19:10:34 -05:00
|
|
|
// Include a line to indicate height vs. width
|
2016-08-04 12:10:50 -05:00
|
|
|
sb.Append(String.Format("{0} {1} ------------->\n",theGrid.Length,theGrid[0].Length));
|
2016-09-21 19:10:34 -05:00
|
|
|
// Iterate through the arrays
|
2016-08-04 12:10:50 -05:00
|
|
|
for (int j=0; j<theGrid.Length; j++) {
|
|
|
|
sb.Append("| ");
|
|
|
|
for (int i=0; i<theGrid[j].Length; i++) {
|
2016-09-21 19:10:34 -05:00
|
|
|
// Print the letters as either letters or ASCII codes.
|
2016-08-04 12:10:50 -05:00
|
|
|
if (Char.IsLetter(theGrid[j][i])) {
|
|
|
|
sb.Append(String.Format("{0} ",theGrid[j][i]));
|
|
|
|
} else {
|
|
|
|
sb.Append(String.Format("A#{0} ",(int)theGrid[j][i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.AppendLine();
|
|
|
|
}
|
|
|
|
sb.Append("V\n");
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:10:34 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Return the array for manipulation
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>the array</returns>
|
2016-08-04 12:10:50 -05:00
|
|
|
public char[][] ToArray() {
|
|
|
|
return theGrid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is leftover in case you want to debug chargrid
|
|
|
|
/*public static void Main(String[] args) {
|
|
|
|
CharGrid cg = new CharGrid("helloiamanewcipher",5);
|
|
|
|
Console.Write(cg.ToString());
|
|
|
|
cg = new CharGrid("four",5,false);
|
|
|
|
Console.Write(cg.ToString());
|
|
|
|
return;
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|