Fixed sonarqube findings

This commit is contained in:
2022-07-04 01:04:06 -04:00
parent 6f300a430a
commit b4817e8bb3
47 changed files with 289 additions and 311 deletions

View File

@@ -0,0 +1,193 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/combination/ADFGX.java
//Mattrixwv
// Created: 01-25-22
//Modified: 07-03-22
package com.mattrixwv.cipherstream.combination;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
public class ADFGX{
private String inputString; //This is the string that needs encoded/decoded
private String outputString; //This is the string that is output after encoding/decoding
private String squareKeyword; //The keyword used to create the Polybius Square
private String keyword; //The keyword used to create the Columnar cipher
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 PolybiusSquare polybiusSquare; //The first step in encoding
private Columnar columnar; //The second step in encoding
//Ensures Polybius keyword constraints
private void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
if(squareKeyword == null){
throw new InvalidKeywordException("Square Keyword cannot be null");
}
this.squareKeyword = squareKeyword;
}
//Ensures Columnar keyword constraints
private void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
}
this.keyword = keyword;
}
//Ensures inputString constraints
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
if(!preserveCapitals){
inputString = inputString.toUpperCase();
}
if(!preserveWhitespace){
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be blank");
}
}
//Format the output string with capitals, symbols, and numbers that are in the input string
private void formatOutputStringEncode(){
StringBuilder output = new StringBuilder();
int outputLocation = 0;
for(char ch : inputString.toCharArray()){
if(Character.isUpperCase(ch)){
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
}
else if(Character.isLowerCase(ch)){
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
}
else{
output.append(ch);
}
}
outputString = output.toString();
}
private void formatOutputStringDecode(){
StringBuilder output = new StringBuilder();
int outputLocation = 0;
for(int inputLocation = 0;inputLocation < inputString.length();++inputLocation){
char ch = inputString.charAt(inputLocation);
if(Character.isUpperCase(ch)){
output.append(Character.toUpperCase(outputString.charAt(outputLocation++)));
++inputLocation;
}
else if(Character.isLowerCase(ch)){
output.append(Character.toLowerCase(outputString.charAt(outputLocation++)));
++inputLocation;
}
else{
output.append(ch);
}
}
outputString = output.toString();
}
//Encodes the inputString and stores the result in outputString
private void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
//Encode the input with polybius
String polybiusOutput = polybiusSquare.encode(squareKeyword, inputString);
squareKeyword = polybiusSquare.getKeyword();
//Change polybius to use the correct symbols
polybiusOutput = polybiusOutput.replace("1", "A").replace("2", "D").replace("3", "F").replace("4", "G").replace("5", "X");
//Encode polybius's output with columnar
String columnarOutput = columnar.encode(keyword, polybiusOutput);
keyword = columnar.getKeyword();
outputString = columnarOutput;
//Add whatever is needed to the output string
formatOutputStringEncode();
}
//Decodes the inputString and stores the result in outputString
private void decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
//Decode the input with columnar
String columnarOutput = columnar.decode(keyword, inputString);
keyword = columnar.getKeyword();
//Change the symbols to the correct ones for polybius
columnarOutput = columnarOutput.replace("A", "1").replace("D", "2").replace("F", "3").replace("G", "4").replace("X", "5");
//Decode with polybius
String polybiusOutput = polybiusSquare.decode(squareKeyword, columnarOutput);
squareKeyword = polybiusSquare.getKeyword();
outputString = polybiusOutput;
//Add whatever is needed to the output string
formatOutputStringDecode();
}
//Constructor
public ADFGX() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
reset();
}
public ADFGX(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
reset();
}
//Encodes inputString using keyword and returns the result
public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword);
setKeyword(keyword);
setInputString(inputString);
encode();
return outputString;
}
//Decodes inputString using keyword and returns the result
public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword);
setKeyword(keyword);
setInputString(inputString);
decode();
return outputString;
}
//Returns the cleaned inputString
public String getInputString(){
return inputString;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Returns the cleaned Polybius Square keyword
public String getSquareKeyword(){
return squareKeyword;
}
//Returns the cleaned Columnar keyword
public String getKeyword(){
return keyword;
}
//Makes sure all of the variables are empty
public void reset() throws InvalidCharacterException{
polybiusSquare = new PolybiusSquare(false, false);
columnar = new Columnar(false, false, false, true, 'B');
inputString = "";
outputString = "";
squareKeyword = "";
keyword = "";
}
}