Added Columnar
This commit is contained in:
@@ -4,82 +4,279 @@
|
||||
//Modified: 01-16-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Columnar{
|
||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
private boolean removePadding; //Whether to respect the padding letters added to the cipher
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
//TODO: Make this more robust
|
||||
private char characterToAdd; //The character that is added to the end of a string to bring it to the correct length
|
||||
private int charsAdded; //The number of characters that were added to the end of the message
|
||||
private ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
|
||||
|
||||
private String getCleanInputString(){
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
private void createGrid(){
|
||||
private void createGridEncode(){
|
||||
//Add the keyword to the first row in the array
|
||||
grid = new ArrayList<ArrayList<Character>>();
|
||||
grid.add(new ArrayList<Character>(Arrays.asList(keyword.chars().mapToObj(c -> (char)c).toArray(Character[]::new))));
|
||||
|
||||
//Create the input that will be used for encoding
|
||||
StringBuilder gridInputString = new StringBuilder();
|
||||
gridInputString.append(inputString.toUpperCase().replaceAll("[^A-Za-z]", ""));
|
||||
|
||||
//Make sure the input is the correct length
|
||||
while((gridInputString.length() % keyword.length()) != 0){
|
||||
gridInputString.append(characterToAdd);
|
||||
}
|
||||
String gridInputString = getCleanInputString();
|
||||
|
||||
//Create arrays from the inputString of keyword.size length and add them each as new rows to the grid
|
||||
//?gridRow could be changed to grid.size(), but would it be worth the extra confusion? Probably not unless I really want to min/max
|
||||
for(int gridRow = 1;(keyword.length() * gridRow) <= gridInputString.length();++gridRow){
|
||||
grid.add(new ArrayList<Character>(Arrays.asList(
|
||||
gridInputString.substring(keyword.length() * (gridRow - 1), (keyword.length() * gridRow) + 1)
|
||||
gridInputString.substring(keyword.length() * (gridRow - 1), (keyword.length() * gridRow))
|
||||
.chars().mapToObj(c->(char)c).toArray(Character[]::new)
|
||||
)));
|
||||
}
|
||||
}
|
||||
private void createGridDecode(){
|
||||
//Add the keyword to the first row in the array
|
||||
grid = new ArrayList<ArrayList<Character>>();
|
||||
StringBuilder orderedKeyword = new StringBuilder();
|
||||
for(int cnt : getKeywordAlphaLocations()){
|
||||
orderedKeyword.append(keyword.charAt(cnt));
|
||||
}
|
||||
grid.add(new ArrayList<Character>(Arrays.asList(orderedKeyword.chars().mapToObj(c -> (char)c).toArray(Character[]::new))));
|
||||
|
||||
//Create the input that will be used for encoding
|
||||
String gridInputString = getCleanInputString();
|
||||
|
||||
//Make sure the grid has the appropritate number of rows
|
||||
int numRows = inputString.length() / keyword.length();
|
||||
while(grid.size() <= numRows){
|
||||
grid.add(new ArrayList<Character>());
|
||||
}
|
||||
//Add each character to the grid
|
||||
int rowCnt = 1;
|
||||
for(char ch : gridInputString.toCharArray()){
|
||||
grid.get(rowCnt++).add(ch);
|
||||
if(rowCnt > numRows){
|
||||
rowCnt = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
private void setInputString(String inputString){
|
||||
private void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toLowerCase();
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("[\\s+]", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
//TODO: Should numbers be treated like symbols? Probably, so this might need modified as well as testing; For all ciphers
|
||||
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s]", "");
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
//TODO: Make sure that the input is not blank. This should be added to all ciphers
|
||||
|
||||
//Make sure the input is the correct length
|
||||
this.inputString = inputString;
|
||||
StringBuilder inputStringBuilder = new StringBuilder();
|
||||
inputStringBuilder.append(inputString);
|
||||
int cleanLength = getCleanInputString().length();
|
||||
int charsToAdd = (cleanLength % keyword.length());
|
||||
if(charsToAdd != 0){
|
||||
charsToAdd = keyword.length() - charsToAdd;
|
||||
}
|
||||
for(int cnt = 0;cnt < charsToAdd;++cnt){
|
||||
inputStringBuilder.append(characterToAdd);
|
||||
}
|
||||
charsAdded = charsToAdd;
|
||||
inputString = inputStringBuilder.toString();
|
||||
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getCleanInputString().isBlank()){
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Creates the output string from the grid
|
||||
private String createOutputString(){
|
||||
//Check every character of the inputString and add the appropriate character to the output
|
||||
int row = 1;
|
||||
int col = 0;
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(char ch : inputString.toCharArray()){
|
||||
//If the character is a letter get the next letter from the grid
|
||||
if(Character.isUpperCase(ch)){
|
||||
output.append(grid.get(row++).get(col));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
output.append(grid.get(row++).get(col));
|
||||
}
|
||||
//If the character is a symbol or whitespace add it to the output
|
||||
else{
|
||||
output.append(ch);
|
||||
private void setInputStringDecode(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("[\\s+]", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Make sure the input is the correct length
|
||||
charsAdded = 0;
|
||||
this.inputString = inputString;
|
||||
//Figure out how many rows there will be
|
||||
int numRows = getCleanInputString().length() / keyword.length();
|
||||
//Get the number of characters the input is over the limit
|
||||
int numCharsOver = getCleanInputString().length() % keyword.length();
|
||||
if(numCharsOver > 0){
|
||||
//Get the first n of the characters in the keyword and their encoded column locations
|
||||
ArrayList<Integer> originalLocations = getKeywordOriginalLocations();
|
||||
ArrayList<Integer> longColumns = new ArrayList<Integer>();
|
||||
for(int cnt = 0;cnt < numCharsOver;++cnt){
|
||||
longColumns.add(originalLocations.get(cnt));
|
||||
}
|
||||
|
||||
//Wrap around the grid when you reach the bottom
|
||||
if(row == grid.size()){
|
||||
row = 1;
|
||||
//Add letters where they need to be added to fill out the grid
|
||||
StringBuilder input = new StringBuilder();
|
||||
int col = 0;
|
||||
int row = 0;
|
||||
for(char ch : inputString.toCharArray()){
|
||||
if(!Character.isAlphabetic(ch)){
|
||||
input.append(ch);
|
||||
continue;
|
||||
}
|
||||
if(row < numRows){
|
||||
input.append(ch);
|
||||
++row;
|
||||
}
|
||||
else{
|
||||
if(longColumns.contains(col)){
|
||||
input.append(ch);
|
||||
row = 0;
|
||||
}
|
||||
else{
|
||||
input.append(characterToAdd);
|
||||
++charsAdded;
|
||||
input.append(ch);
|
||||
row = 1;
|
||||
}
|
||||
++col;
|
||||
}
|
||||
}
|
||||
if(!longColumns.contains(col)){
|
||||
input.append(characterToAdd);
|
||||
++charsAdded;
|
||||
}
|
||||
inputString = input.toString();
|
||||
}
|
||||
|
||||
//Save the input
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getCleanInputString().isBlank()){
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Creates the output string from the grid
|
||||
private String createOutputStringFromColumns(){
|
||||
//Get the current rows of any characters that you added
|
||||
ArrayList<Integer> colsAddedTo = new ArrayList<Integer>();
|
||||
if(removePadding){
|
||||
ArrayList<Integer> cols = getKeywordOriginalLocations();
|
||||
Collections.reverse(cols);
|
||||
for(int cnt = 0;cnt < charsAdded;++cnt){
|
||||
colsAddedTo.add(cols.get(cnt));
|
||||
}
|
||||
}
|
||||
else{
|
||||
charsAdded = 0;
|
||||
}
|
||||
|
||||
//Turn the grid into a string
|
||||
StringBuilder gridOutput = new StringBuilder();
|
||||
for(int col = 0;col < grid.get(0).size();++col){
|
||||
for(int row = 1;row < grid.size();++row){
|
||||
gridOutput.append(grid.get(row).get(col));
|
||||
}
|
||||
if(colsAddedTo.contains(col)){
|
||||
gridOutput.deleteCharAt(gridOutput.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
//Preserve any remaining symbols in the string
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int outputLoc = 0, inputLoc = 0;inputLoc < (inputString.length() - charsAdded);){
|
||||
char inputChar = inputString.charAt(inputLoc++);
|
||||
if(Character.isUpperCase(inputChar)){
|
||||
output.append(Character.toUpperCase(gridOutput.charAt(outputLoc++)));
|
||||
}
|
||||
else if(Character.isLowerCase(inputChar)){
|
||||
output.append(Character.toLowerCase(gridOutput.charAt(outputLoc++)));
|
||||
}
|
||||
else{
|
||||
output.append(inputChar);
|
||||
}
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
private String createOutputStringFromRows(){
|
||||
//Turn the grid into a string
|
||||
StringBuilder gridOutput = new StringBuilder();
|
||||
for(int row = 1;row < grid.size();++row){
|
||||
for(int col = 0;col < grid.get(row).size();++col){
|
||||
gridOutput.append(grid.get(row).get(col));
|
||||
}
|
||||
}
|
||||
//Remove any added characters
|
||||
if(removePadding){
|
||||
for(int cnt = 0;cnt < charsAdded;++cnt){
|
||||
gridOutput.deleteCharAt(gridOutput.length() - 1);
|
||||
}
|
||||
}
|
||||
else{
|
||||
charsAdded = 0;
|
||||
}
|
||||
|
||||
ArrayList<Integer> colsAddedTo = new ArrayList<Integer>();
|
||||
if(removePadding){
|
||||
ArrayList<Integer> cols = getKeywordOriginalLocations();
|
||||
Collections.reverse(cols);
|
||||
for(int cnt = 0;cnt < charsAdded;++cnt){
|
||||
colsAddedTo.add(cols.get(cnt));
|
||||
}
|
||||
}
|
||||
//Preserve any remaining symbols in the string
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int outputLoc = 0, inputLoc = 0, row = 1, col = 0;inputLoc < inputString.length();){
|
||||
char inputChar = inputString.charAt(inputLoc++);
|
||||
if(((row + 1) == grid.size()) && (colsAddedTo.contains(col)) && (Character.isAlphabetic(inputChar))){
|
||||
++col;
|
||||
row = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(Character.isUpperCase(inputChar)){
|
||||
output.append(Character.toUpperCase(gridOutput.charAt(outputLoc++)));
|
||||
++row;
|
||||
}
|
||||
else if(Character.isLowerCase(inputChar)){
|
||||
output.append(Character.toLowerCase(gridOutput.charAt(outputLoc++)));
|
||||
++row;
|
||||
}
|
||||
else{
|
||||
output.append(inputChar);
|
||||
}
|
||||
|
||||
if(row == grid.size()){
|
||||
++col;
|
||||
row = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,57 +285,137 @@ public class Columnar{
|
||||
return outputString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword){
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
this.keyword = keyword.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
|
||||
//Make sure a valid keyword is present
|
||||
if(this.keyword.length() < 2){
|
||||
throw new InvalidKeywordException("The keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Set the character that is added to the end of the string
|
||||
private void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(characterToAdd)){
|
||||
throw new InvalidCharacterException("Character to add must be a letter");
|
||||
}
|
||||
|
||||
if(!preserveCapitals){
|
||||
this.characterToAdd = Character.toUpperCase(characterToAdd);
|
||||
}
|
||||
else{
|
||||
this.characterToAdd = characterToAdd;
|
||||
}
|
||||
}
|
||||
//Returns a list of integers that represents the location of the characters of the keyword in alphabetic order
|
||||
private ArrayList<Integer> getKeywordAlphaLocations(){
|
||||
ArrayList<Integer> orderedLocations = new ArrayList<Integer>();
|
||||
//go through every letter and check it against the keyword
|
||||
for(char ch = 'A';ch <= 'Z';++ch){
|
||||
for(int cnt = 0;cnt < keyword.length();++cnt){
|
||||
//If the current letter is the same as the letter we are looking for add the location to the array
|
||||
if(keyword.charAt(cnt) == ch){
|
||||
orderedLocations.add(cnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Return the alphabetic locations
|
||||
return orderedLocations;
|
||||
}
|
||||
private ArrayList<Integer> getKeywordOriginalLocations(){
|
||||
//Figure out the order the columns are in
|
||||
ArrayList<Integer> orderedLocations = getKeywordAlphaLocations();
|
||||
//Figure out what order the columns need rearanged to
|
||||
ArrayList<Integer> originalOrder = new ArrayList<Integer>();
|
||||
for(int orgCnt = 0;orgCnt < orderedLocations.size();++orgCnt){
|
||||
for(int orderedCnt = 0;orderedCnt < orderedLocations.size();++orderedCnt){
|
||||
if(orderedLocations.get(orderedCnt) == orgCnt){
|
||||
originalOrder.add(orderedCnt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return originalOrder;
|
||||
}
|
||||
//Rearanges the grid based on the list of numbers given
|
||||
private void rearangeList(ArrayList<Integer> listOrder){
|
||||
//TODO:
|
||||
private void rearangeGrid(ArrayList<Integer> listOrder){
|
||||
//Create a new grid and make sure it is the same size as the original grid
|
||||
int numCol = grid.get(0).size();
|
||||
ArrayList<ArrayList<Character>> newGrid = new ArrayList<ArrayList<Character>>(grid.size());
|
||||
for(int cnt = 0;cnt < grid.size();++cnt){
|
||||
newGrid.add(new ArrayList<Character>(numCol));
|
||||
}
|
||||
|
||||
//Step through the list order, pull out the columns, and add them to the new grid
|
||||
for(int col : listOrder){
|
||||
for(int row = 0;row < grid.size();++row){
|
||||
newGrid.get(row).add(grid.get(row).get(col));
|
||||
}
|
||||
}
|
||||
|
||||
//Save the new grid
|
||||
grid = newGrid;
|
||||
}
|
||||
//Encodes inputString using the Columnar cipher and stores the result in outputString
|
||||
private String encode(){
|
||||
//TODO:
|
||||
return null;
|
||||
//Create the grid
|
||||
createGridEncode();
|
||||
//Figure out the new column order
|
||||
ArrayList<Integer> orderedLocations = getKeywordAlphaLocations();
|
||||
//Rearange the grid to the new order
|
||||
rearangeGrid(orderedLocations);
|
||||
//Create and return the output
|
||||
return createOutputStringFromColumns();
|
||||
}
|
||||
//Decodes inputString using the Columnar cipher and stores the result in outputString
|
||||
private String decode(){
|
||||
//TODO:
|
||||
return null;
|
||||
//Create the grid
|
||||
createGridDecode();
|
||||
ArrayList<Integer> originalOrder = getKeywordOriginalLocations();
|
||||
//Rearange the grid to the original order
|
||||
rearangeGrid(originalOrder);
|
||||
//Create and return the output
|
||||
return createOutputStringFromRows();
|
||||
}
|
||||
|
||||
public Columnar(){
|
||||
public Columnar() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
characterToAdd = 'X';
|
||||
removePadding = false;
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
characterToAdd = 'X';
|
||||
this.removePadding = removePadding;
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char characterToAdd){
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding, char characterToAdd) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
characterToAdd = 'X';
|
||||
this.removePadding = removePadding;
|
||||
setCharacterToAdd(characterToAdd);
|
||||
reset();
|
||||
}
|
||||
//Sets the keyword, grid, and inputString and encodes the message
|
||||
public String encode(String keyword, String inputString){
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
createGrid();
|
||||
setInputStringEncode(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Sets the keyword, grid, and inputString and decodes the message
|
||||
public String decode(String keyword, String inputString){
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
createGrid();
|
||||
setInputStringDecode(inputString);
|
||||
return decode();
|
||||
}
|
||||
|
||||
@@ -148,6 +425,7 @@ public class Columnar{
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
grid = new ArrayList<ArrayList<Character>>();
|
||||
charsAdded = 0;
|
||||
}
|
||||
//Gets
|
||||
public String getInputString(){
|
||||
|
||||
Reference in New Issue
Block a user