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,270 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java
//Mattrixwv
// Created: 01-31-22
//Modified: 02-17-22
package com.mattrixwv.cipherstream.polysubstitution;
import java.security.InvalidKeyException;
import java.util.ArrayList;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.matrix.ModMatrix;
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
public class Hill{
private boolean preserveCapitals;
private boolean preserveWhitespace;
private boolean preserveSymbols;
private boolean removePadding;
private String inputString;
private String outputString;
private char characterToAdd;
private int charsAdded;
private ModMatrix key;
private void setKey(ModMatrix key) throws InvalidKeyException{
//Make sure the mod is correct
if(key.getMod() != 26){
throw new InvalidKeyException("This algorithm uses the english alphabet, so the mod for the key must be 26");
}
//Make sure the matrix is square
if(!key.isSquare()){
throw new InvalidKeyException("The key must be a square matrix");
}
//Make sure the matrix is invertable
try{
key.inverse();
}
catch(InvalidGeometryException | InvalidScalarException error){
throw new InvalidKeyException("The key does not have an inverse mod 26");
}
//Set the key
this.key = new ModMatrix(key);
}
private void setInputString(String inputString) throws InvalidInputException{
//Remove anything that needs removed
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 correct length
this.inputString = inputString;
int cleanLength = getCleanInputString().length();
int charsToAdd = (cleanLength % key.getNumRows());
StringBuilder inputStringBuilder = new StringBuilder();
inputStringBuilder.append(inputString);
if(charsToAdd != 0){
charsToAdd = key.getNumRows() - charsToAdd;
}
for(int cnt = 0;cnt < charsToAdd;++cnt){
inputStringBuilder.append(characterToAdd);
}
charsAdded = charsToAdd;
inputString = inputStringBuilder.toString();
this.inputString = inputString;
//Make sure the input isn't blank
if(this.inputString.isBlank() || getCleanInputString().isBlank()){
throw new InvalidInputException("Input cannot be blank");
}
}
private String getCleanInputString(){
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
}
private void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
//Make sure the character is a letter
if(!Character.isAlphabetic(characterToAdd)){
throw new InvalidCharacterException("Character to add must be a letter");
}
//Save the characterToAdd
if(!preserveCapitals){
this.characterToAdd = Character.toUpperCase(characterToAdd);
}
else{
this.characterToAdd = characterToAdd;
}
}
private String polishOutputString(){
//Add the extra characters back to the output and remove the added characters
int outputCnt = 0;
StringBuilder outputBuilder = new StringBuilder();
for(char ch : inputString.toCharArray()){
if(Character.isUpperCase(ch)){
outputBuilder.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
}
else if(Character.isLowerCase(ch)){
outputBuilder.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
}
else{
outputBuilder.append(ch);
}
}
if(removePadding){
String tempString = outputBuilder.substring(0, outputBuilder.length() - charsAdded);
outputBuilder = new StringBuilder();
outputBuilder.append(tempString);
}
return outputBuilder.toString();
}
private ArrayList<ModMatrix> getInputVectors(){
//Get the number of columns in the key
int numCols = key.getNumCols();
//Get a clean inputString
String cleanInput = getCleanInputString();
//Break the inputString up into lengths of numCols
ArrayList<ModMatrix> vectors = new ArrayList<>();
for(int cnt = 0;cnt < cleanInput.length();cnt += numCols){
String subString = cleanInput.substring(cnt, cnt + numCols);
int[] grid = new int[numCols];
//Subtract 65 from each character so that A=0, B=1, ...
for(int subCnt = 0;subCnt < subString.length();++subCnt){
grid[subCnt] = subString.charAt(subCnt) - 65;
}
//Create a vector from the new values
ModMatrix vector = new ModMatrix(26);
vector.addCol(grid);
//Add the vector to the array
vectors.add(vector);
}
//Return the array of vectors
return vectors;
}
private String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){
//Go through each element in the vector
StringBuilder outputBuilder = new StringBuilder();
for(ModMatrix vector : outputVectors){
//Add 65 to each element and add it to the string
for(int cnt = 0;cnt < vector.getNumRows();++cnt){
outputBuilder.append((char)(vector.get(cnt, 0) + 65));
}
}
//Return the new string
return outputBuilder.toString();
}
private String encode(){
//Get an array of vectors that we are going to encode
ArrayList<ModMatrix> inputVectors = getInputVectors();
//Multiply the key by each vector and add the result to a new vector
ArrayList<ModMatrix> outputVectors = new ArrayList<>();
for(ModMatrix inputVector : inputVectors){
ModMatrix outputVector = key.multiply(inputVector);
outputVectors.add(outputVector);
}
//Take the array of results and turn them back into letters
outputString = getOutputFromVectors(outputVectors);
//Add the extra characters back to the output and remove the added characters
outputString = polishOutputString();
//Save the output
return outputString;
}
private String decode(){
//Get the array of vectors that we are going to decode
ArrayList<ModMatrix> inputVectors = getInputVectors();
//Multiply the inverse of the key by each vector and add the result to a new vector
ModMatrix inverseKey = key.inverse();
ArrayList<ModMatrix> outputVectors = new ArrayList<>();
for(ModMatrix inputVector : inputVectors){
ModMatrix outputVector = inverseKey.multiply(inputVector);
outputVectors.add(outputVector);
}
//Take the array of results and turn them back into letters
outputString = getOutputFromVectors(outputVectors);
//Add the extra characters back to the output and remove the added characters
outputString = polishOutputString();
//Save the output
return outputString;
}
public Hill() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
removePadding = false;
setCharacterToAdd('x');
reset();
}
public Hill(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 Hill(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();
}
public String encode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
return encode(new ModMatrix(key, 26), inputString);
}
public String encode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
setKey(key);
setInputString(inputString);
return encode();
}
public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
return decode(new ModMatrix(key, 26), inputString);
}
public String decode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
setKey(key);
setInputString(inputString);
return decode();
}
public void reset(){
inputString = "";
outputString = "";
key = new ModMatrix(26);
charsAdded = 0;
}
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
public ModMatrix getKey(){
return key;
}
}