Fixed sonarqube findings
This commit is contained in:
@@ -0,0 +1,352 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/PolybiusSquare.java
|
||||
//Mattrixwv
|
||||
// Created: 01-04-22
|
||||
//Modified: 02-17-22
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class PolybiusSquare{
|
||||
//A class representing the location of a character in the grid
|
||||
protected class CharLocation{
|
||||
private int x;
|
||||
private int y;
|
||||
public CharLocation(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
protected String inputString; //The message that needs to be encoded/decoded
|
||||
protected String outputString; //The encoded/decoded message
|
||||
protected String keyword; //The keyword used to create the grid
|
||||
protected char[][] grid; //The grid used to encode/decode the message
|
||||
protected char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
|
||||
protected char replacer; //The letter that replaces replaced in the input string or keyword
|
||||
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
|
||||
//Create the grid from the keyword
|
||||
protected void createGrid(){
|
||||
for(int row = 0;row < 5;++row){
|
||||
for(int col = 0;col < 5;++col){
|
||||
char letter = keyword.charAt((5 * row) + col);
|
||||
grid[row][col] = letter;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
protected void setInputStringEncoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
//Make sure the string doesn't contain any numbers
|
||||
for(char ch = '0';ch <= '9';++ch){
|
||||
if(inputString.contains(Character.toString(ch))){
|
||||
throw new InvalidCharacterException("Inputs for encoding cannot contain numbers");
|
||||
}
|
||||
}
|
||||
|
||||
//Change to upper case
|
||||
inputString = inputString.toUpperCase();
|
||||
|
||||
//Remove any whitespace if selected
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
|
||||
//Remove any symbols if selected
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
if(!preserveWhitespace && !preserveSymbols){
|
||||
//Add whitespace after every character for the default look
|
||||
StringJoiner spacedString = new StringJoiner(" ");
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
spacedString.add(Character.toString(inputString.charAt(cnt)));
|
||||
}
|
||||
inputString = spacedString.toString();
|
||||
}
|
||||
|
||||
//Replace any characters that need replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputStringEncoding().isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
protected void setInputStringDecoding(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
//Make sure the string contains an even number of digits and no letters
|
||||
int numberOfDigits = 0;
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char ch = inputString.charAt(cnt);
|
||||
if(Character.isDigit(ch)){
|
||||
++numberOfDigits;
|
||||
}
|
||||
else if(Character.isAlphabetic(ch)){
|
||||
throw new InvalidCharacterException("Inputs for decoding cannot contains letters");
|
||||
}
|
||||
}
|
||||
if((numberOfDigits % 2) != 0){
|
||||
throw new InvalidCharacterException("There must be an even number of digits in an encoded string");
|
||||
}
|
||||
|
||||
//Remove any whitespace if selected
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
|
||||
//Remove any symbols if selected
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^0-9\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputStringDecoding().isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
protected String getPreparedInputStringEncoding(){
|
||||
String cleanString = inputString.toUpperCase();
|
||||
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
||||
return cleanString;
|
||||
}
|
||||
protected String getPreparedInputStringDecoding(){
|
||||
return inputString.replaceAll("[^0-9]", "");
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
protected void setKeyword(String keyword){
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
//Change everything to uppercase
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Remove everything except capital letters
|
||||
keyword = keyword.replaceAll("[^A-Z]", "");
|
||||
|
||||
//Add all letters in the alphabet to the key
|
||||
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
//Replace all replaced characters
|
||||
keyword = keyword.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Remove all duplicate characters
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given charcter in the grid
|
||||
protected CharLocation findChar(char letter) throws InvalidInputException{
|
||||
for(int row = 0;row < grid.length;++row){
|
||||
for(int col = 0;col < grid[row].length;++col){
|
||||
if(grid[row][col] == letter){
|
||||
return new CharLocation(row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
//If it was not found something went wrong
|
||||
throw new InvalidInputException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
protected void addCharactersToCleanStringEncode(String cleanString){
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
//Add both numbers of any letters to the output
|
||||
if(Character.isAlphabetic(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
}
|
||||
//Add any other characters that appear to the output
|
||||
else{
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
protected void addCharactersToCleanStringDecode(String cleanString){
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
//Add the letter to the output and skip the second number
|
||||
if(Character.isDigit(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
++inputCnt;
|
||||
}
|
||||
//Add any other characters that appear to the output
|
||||
else{
|
||||
fullOutput.append(inputString.charAt(inputCnt));
|
||||
}
|
||||
}
|
||||
outputString = fullOutput.toString();
|
||||
}
|
||||
//Encodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String encode() throws InvalidInputException{
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringEncoding();
|
||||
for(int cnt = 0;cnt < cleanString.length();++cnt){
|
||||
//Get the next character to be encoded
|
||||
char ch = cleanString.charAt(cnt);
|
||||
|
||||
//Find the letter in the grid
|
||||
CharLocation location = findChar(ch);
|
||||
|
||||
//Add the grid location to the output
|
||||
output.append(location.getX() + 1);
|
||||
output.append(location.getY() + 1);
|
||||
}
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanStringEncode(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
String cleanString = getPreparedInputStringDecoding();
|
||||
for(int cnt = 0;cnt < cleanString.length();){
|
||||
//Get the digits indicationg the location of the next character
|
||||
char firstDigit = cleanString.charAt(cnt++);
|
||||
char secondDigit = cleanString.charAt(cnt++);
|
||||
|
||||
//Get the next character
|
||||
char letter = grid[Integer.valueOf(Character.toString(firstDigit)) - 1][Integer.valueOf(Character.toString(secondDigit)) - 1];
|
||||
|
||||
//Add the new letter to the output
|
||||
output.append(letter);
|
||||
}
|
||||
|
||||
//Add other characters to the output
|
||||
addCharactersToCleanStringDecode(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public PolybiusSquare() throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
}
|
||||
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer) throws InvalidCharacterException{
|
||||
reset();
|
||||
setReplaced(replaced);
|
||||
setReplacer(replacer);
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
return encode("", inputString);
|
||||
}
|
||||
public String encode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringEncoding(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
return decode("", inputString);
|
||||
}
|
||||
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputStringDecoding(inputString);
|
||||
return decode();
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
grid = new char[5][5];
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
//Gets
|
||||
public char getReplaced(){
|
||||
return replaced;
|
||||
}
|
||||
public void setReplaced(char replaced) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replaced)){
|
||||
throw new InvalidCharacterException("The replaced character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replaced letter cannot be the same as the replacing letter");
|
||||
}
|
||||
|
||||
this.replaced = Character.toUpperCase(replaced);
|
||||
}
|
||||
public char getReplacer(){
|
||||
return replacer;
|
||||
}
|
||||
public void setReplacer(char replacer) throws InvalidCharacterException{
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer character must be a letter");
|
||||
}
|
||||
|
||||
if(replaced == replacer){
|
||||
throw new InvalidCharacterException("The replacer letter cannot be the same as the replaced letter");
|
||||
}
|
||||
|
||||
this.replacer = replacer;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getGrid(){
|
||||
StringBuilder gridString = new StringBuilder();
|
||||
for(char[] row : grid){
|
||||
for(char col : row){
|
||||
gridString.append(col);
|
||||
}
|
||||
}
|
||||
|
||||
return gridString.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user