Fixed sonarqube findings
This commit is contained in:
@@ -0,0 +1,459 @@
|
||||
//MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java
|
||||
//Mattrixwv
|
||||
// Created: 01-16-22
|
||||
//Modified: 03-03-22
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Columnar{
|
||||
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
|
||||
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 boolean preserveCapitals; //Persist capitals in the output string
|
||||
private boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
private boolean preserveSymbols; //Persist symbols in the output string
|
||||
private boolean removePadding; //Remove the padding letters added to the cipher
|
||||
|
||||
//Strip the inputString of all non-letter characters and change them to capitals
|
||||
private String getCleanInputString(){
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
private void createGridEncode(){
|
||||
//Add the keyword to the first row in the array
|
||||
grid = new ArrayList<>();
|
||||
grid.add(new ArrayList<>(Arrays.asList(keyword.chars().mapToObj(c -> (char)c).toArray(Character[]::new))));
|
||||
|
||||
//Create the input that will be used for encoding
|
||||
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<>(Arrays.asList(
|
||||
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<>();
|
||||
StringBuilder orderedKeyword = new StringBuilder();
|
||||
for(int cnt : getKeywordAlphaLocations()){
|
||||
orderedKeyword.append(keyword.charAt(cnt));
|
||||
}
|
||||
grid.add(new ArrayList<>(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<>());
|
||||
}
|
||||
//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 setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
//Apply removal options
|
||||
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
|
||||
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();
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
if(this.inputString.isBlank() || getCleanInputString().isBlank()){
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
private void setInputStringDecode(String inputString) throws InvalidInputException{
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
//Apply removal options
|
||||
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<>();
|
||||
for(int cnt = 0;cnt < numCharsOver;++cnt){
|
||||
longColumns.add(originalLocations.get(cnt));
|
||||
}
|
||||
|
||||
//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;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
if(this.inputString.isBlank() || getCleanInputString().isBlank()){
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Creates the output string from the grid
|
||||
private void createOutputStringFromColumns(){
|
||||
//Get the current rows of any characters that you added
|
||||
ArrayList<Integer> colsAddedTo = new ArrayList<>();
|
||||
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();
|
||||
}
|
||||
private void 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<>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
outputString = output.toString();
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Strip all non-letter characters and change them to uppercase
|
||||
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<>();
|
||||
//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<>();
|
||||
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 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<>(grid.size());
|
||||
for(int cnt = 0;cnt < grid.size();++cnt){
|
||||
newGrid.add(new ArrayList<>(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 void encode(){
|
||||
//Create the grid
|
||||
createGridEncode();
|
||||
//Figure out the new column order
|
||||
ArrayList<Integer> orderedLocations = getKeywordAlphaLocations();
|
||||
//Rearange the grid to the new order
|
||||
rearangeGrid(orderedLocations);
|
||||
//Create the output
|
||||
createOutputStringFromColumns();
|
||||
}
|
||||
//Decodes inputString using the Columnar cipher and stores the result in outputString
|
||||
private void decode(){
|
||||
//Create the grid
|
||||
createGridDecode();
|
||||
ArrayList<Integer> originalOrder = getKeywordOriginalLocations();
|
||||
//Rearange the grid to the original order
|
||||
rearangeGrid(originalOrder);
|
||||
//Create the output
|
||||
createOutputStringFromRows();
|
||||
}
|
||||
|
||||
//Constructor
|
||||
public Columnar() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
removePadding = false;
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
this.removePadding = removePadding;
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
public Columnar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding, char characterToAdd) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
this.removePadding = removePadding;
|
||||
setCharacterToAdd(characterToAdd);
|
||||
reset();
|
||||
}
|
||||
//Encodes inputString using keyword and returns the result
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringEncode(inputString);
|
||||
|
||||
//Encode and return the message
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Encodes inputString using keyword and returns the result
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringDecode(inputString);
|
||||
|
||||
//Decode and return the message
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
grid = new ArrayList<>();
|
||||
charsAdded = 0;
|
||||
}
|
||||
//Gets
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user