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,110 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
//Mattrixwv
// Created: 07-25-21
//Modified: 07-03-22
package com.mattrixwv.cipherstream.monosubstitution;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
public class Autokey extends Vigenere{
//Special rules for setting the strings for encoding
private void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the input
setInputString(inputString);
StringBuilder newKey = new StringBuilder();
//Remove all unneccessary elements from the key
setKeyword(keyword);
newKey.append(keyword);
//Remove all unneccessary elements from the input
setKeyword(inputString);
newKey.append(getKeyword());
//Make sure the key is not any longer than the input
keyword = newKey.substring(0, getKeyword().length());
//Set the new keyword
setKeyword(keyword);
//Make sure to update the offset
offset.clear();
setOffset();
}
//Setting the strings for decoding
private void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Remove all unneccessary elements from the key
setKeyword(keyword);
//Remove all unneccessary elements from the input
setInputString(inputString);
}
//Decodes the inputString
@Override
protected String decode(){
//Decode what the key will allow, add that to the key and continue
StringBuilder currentOutput = new StringBuilder();
StringBuilder fullOutput = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
int offsetCnt = 0;
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
//If we have reached the end of the keyword add what we have to it and continue
if(offsetCnt == keyword.length()){
setKeyword(keyword + currentOutput.toString());
fullOutput.append(currentOutput);
currentOutput = new StringBuilder();
}
char letter = inputString.charAt(letterCnt);
if(Character.isUpperCase(letter)){
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'A'){
letter += 26;
}
else if(letter > 'Z'){
letter -= 26;
}
}
else if(Character.isLowerCase(letter)){
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'a'){
letter += 26;
}
else if(letter > 'z'){
letter -= 26;
}
}
currentOutput.append(letter);
}
//Empty the last character that were decoded
fullOutput.append(currentOutput);
//Save and return the results
outputString = fullOutput.toString();
return outputString;
}
//Constructor
public Autokey(){
super();
}
public Autokey(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
super(preserveCapitals, preserveWhitespace, preserveSymbols);
}
//Encodes inputString using the Autokey cipher
@Override
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();
encodeSet(keyword, inputString);
return encode();
}
//Decodes inputString using the Autokey cipher
@Override
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();
decodeSet(keyword, inputString);
return decode();
}
}