Fixed sonarqube findings
This commit is contained in:
@@ -0,0 +1,437 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Playfair.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//Modified: 02-17-22
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Playfair{
|
||||
//A class representing the location of a character in the grid
|
||||
private 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
|
||||
private boolean preserveCapitals; //Whether to respect captials 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 char replaced; //The letter that will need to be replaced in the grid and any input string or keyword
|
||||
private char replacer; //The letter that replaced replaced in the input string or keyword
|
||||
private char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
|
||||
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[][] grid; //The grid used to encode/decode the message
|
||||
//Create the grid from the keyword
|
||||
private 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
|
||||
private void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
|
||||
//Make sure the input string is not null
|
||||
if(inputString == null){
|
||||
throw new NullPointerException("The input string cannot be null");
|
||||
}
|
||||
|
||||
//Set the options
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Make replace all of the replacers with replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//If there is nothing in the input string throw an exception
|
||||
if(inputString.isBlank()){
|
||||
throw new InvalidCharacterException("The input string cannot be blank");
|
||||
}
|
||||
|
||||
//If this is encoding parse it and clean up an problems
|
||||
if(encoding){
|
||||
setEncodingInputString(inputString);
|
||||
}
|
||||
//If this is decoding just add it without parsing it
|
||||
else{
|
||||
//Throw an exception if replaced is included
|
||||
if(inputString.contains(Character.toString(replaced))){
|
||||
throw new InvalidCharacterException("An encoded message cannot contain a letter that needs replaced");
|
||||
}
|
||||
|
||||
this.inputString = inputString;
|
||||
}
|
||||
|
||||
if(this.inputString.isBlank() || getPreparedInputString().isBlank()){
|
||||
throw new InvalidInputException("Input must have at least 1 letter");
|
||||
}
|
||||
}
|
||||
private void setEncodingInputString(String inputString){
|
||||
//Replace characters that need replaced
|
||||
inputString = inputString.replaceAll(Character.toString(replaced), Character.toString(replacer));
|
||||
|
||||
//Check if there are any doubled characters
|
||||
StringBuilder cleanInput = new StringBuilder();
|
||||
int letterCount = 0;
|
||||
for(int cnt = 0;cnt < inputString.length();){
|
||||
//Advance until you find a letter, saving the input for inclusion
|
||||
StringBuilder prepend = new StringBuilder();
|
||||
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
|
||||
prepend.append(inputString.charAt(cnt++));
|
||||
}
|
||||
//If we have reached the end of the string end the loop
|
||||
if(cnt == inputString.length()){
|
||||
cleanInput.append(prepend);
|
||||
break;
|
||||
}
|
||||
//Get the next character
|
||||
char firstLetter = inputString.charAt(cnt++);
|
||||
++letterCount;
|
||||
|
||||
//Advance until you find a letter, saving the input for inclusion
|
||||
StringBuilder middle = new StringBuilder();
|
||||
while((cnt < inputString.length()) && !Character.isAlphabetic(inputString.charAt(cnt))){
|
||||
middle.append(inputString.charAt(cnt++));
|
||||
}
|
||||
char secondLetter = '\0';
|
||||
//If we have not reached the end of the string get the next character
|
||||
if(cnt != inputString.length()){
|
||||
secondLetter = inputString.charAt(cnt++);
|
||||
++letterCount;
|
||||
}
|
||||
//If the second character is the same as the first character set the pointer back and use the doubled character
|
||||
if(secondLetter == firstLetter){
|
||||
--cnt;
|
||||
secondLetter = doubled;
|
||||
}
|
||||
|
||||
//Add all of the gathered input to the cleaned up input
|
||||
cleanInput.append(prepend);
|
||||
cleanInput.append(firstLetter);
|
||||
cleanInput.append(middle);
|
||||
if(secondLetter != '\0'){
|
||||
cleanInput.append(secondLetter);
|
||||
}
|
||||
}
|
||||
|
||||
//Check if there are an odd number of characters
|
||||
if((letterCount % 2) == 1){
|
||||
int lastLetterLocation = cleanInput.length() - 1;
|
||||
while(!Character.isAlphabetic(cleanInput.charAt(lastLetterLocation))){
|
||||
--lastLetterLocation;
|
||||
}
|
||||
if(cleanInput.charAt(lastLetterLocation) == doubled){
|
||||
cleanInput.append(replacer);
|
||||
}
|
||||
else{
|
||||
cleanInput.append(doubled);
|
||||
}
|
||||
}
|
||||
|
||||
this.inputString = cleanInput.toString();
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
private String getPreparedInputString(){
|
||||
String cleanString = inputString.toUpperCase();
|
||||
cleanString = cleanString.replaceAll("[^A-Z]", "");
|
||||
return cleanString;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword){
|
||||
if(keyword == null){
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Change everything to uppercase
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Removing 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 chatacters
|
||||
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 character in the grid
|
||||
private 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");
|
||||
}
|
||||
//Returns the location in the grid of x and y, adjusting for out of bounds
|
||||
private char getGridChar(int x, int y){
|
||||
if(x < 0){
|
||||
x += 5;
|
||||
}
|
||||
if(y < 0){
|
||||
y += 5;
|
||||
}
|
||||
return grid[x % 5][y % 5];
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
private void addCharactersToCleanString(String cleanString){
|
||||
int outputCnt = 0;
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
|
||||
if(Character.isUpperCase(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(cleanString.charAt(outputCnt++));
|
||||
}
|
||||
else if(Character.isLowerCase(inputString.charAt(inputCnt))){
|
||||
fullOutput.append(Character.toLowerCase(cleanString.charAt(outputCnt++)));
|
||||
}
|
||||
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();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
while(inputCnt < cleanString.length()){
|
||||
//Get the next 2 letters to be encoded
|
||||
char firstLetter = cleanString.charAt(inputCnt++);
|
||||
char secondLetter = cleanString.charAt(inputCnt++);
|
||||
|
||||
//Find the letters in the grid
|
||||
CharLocation firstLocation = findChar(firstLetter);
|
||||
CharLocation secondLocation = findChar(secondLetter);
|
||||
|
||||
//Encode the letters
|
||||
if(firstLocation.getX() == secondLocation.getX()){
|
||||
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() + 1);
|
||||
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() + 1);
|
||||
}
|
||||
else if(firstLocation.getY() == secondLocation.getY()){
|
||||
firstLetter = getGridChar(firstLocation.getX() + 1, firstLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX() + 1, secondLocation.getY());
|
||||
}
|
||||
else{
|
||||
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
|
||||
}
|
||||
|
||||
//Add the new letters to the output string
|
||||
output.append(firstLetter);
|
||||
output.append(secondLetter);
|
||||
}
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
private String decode() throws InvalidInputException{
|
||||
StringBuilder output = new StringBuilder();
|
||||
int inputCnt = 0;
|
||||
String cleanString = getPreparedInputString();
|
||||
while(inputCnt < cleanString.length()){
|
||||
//Get the next 2 letters to be encoded
|
||||
char firstLetter = cleanString.charAt(inputCnt++);
|
||||
char secondLetter = cleanString.charAt(inputCnt++);
|
||||
|
||||
//Find the letters in the grid
|
||||
CharLocation firstLocation = findChar(firstLetter);
|
||||
CharLocation secondLocation = findChar(secondLetter);
|
||||
|
||||
//Decode the letters
|
||||
if(firstLocation.getX() == secondLocation.getX()){
|
||||
firstLetter = getGridChar(firstLocation.getX(), firstLocation.getY() - 1);
|
||||
secondLetter = getGridChar(secondLocation.getX(), secondLocation.getY() - 1);
|
||||
}
|
||||
else if(firstLocation.getY() == secondLocation.getY()){
|
||||
firstLetter = getGridChar(firstLocation.getX() - 1, firstLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX() - 1, secondLocation.getY());
|
||||
}
|
||||
else{
|
||||
firstLetter = getGridChar(firstLocation.getX(), secondLocation.getY());
|
||||
secondLetter = getGridChar(secondLocation.getX(), firstLocation.getY());
|
||||
}
|
||||
|
||||
//Add the new letters to the output string
|
||||
output.append(firstLetter);
|
||||
output.append(secondLetter);
|
||||
}
|
||||
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
|
||||
//Return the output string
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public Playfair() throws InvalidCharacterException{
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
setDoubled('X');
|
||||
}
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
setReplaced('J');
|
||||
setReplacer('I');
|
||||
setDoubled('X');
|
||||
}
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
setReplaced(replaced);
|
||||
setReplacer(replacer);
|
||||
setDoubled(doubled);
|
||||
}
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
public String encode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(input, true);
|
||||
return encode();
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
public String decode(String keyword, String input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(input, false);
|
||||
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");
|
||||
}
|
||||
|
||||
if(replaced == doubled){
|
||||
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{
|
||||
//Make sure the character is a letter
|
||||
if(!Character.isAlphabetic(replacer)){
|
||||
throw new InvalidCharacterException("The replacer must be a letter");
|
||||
|
||||
}
|
||||
|
||||
//Make sure the character isn't the same as what it is supposed to replace
|
||||
if(replacer == replaced){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same letter as what it is replacing");
|
||||
}
|
||||
|
||||
//Make sure the replacer isn't the same as the double letter replacer
|
||||
if(replacer == doubled){
|
||||
throw new InvalidCharacterException("The replacer cannot be the same as the double letter replacer");
|
||||
}
|
||||
|
||||
this.replacer = Character.toUpperCase(replacer);
|
||||
}
|
||||
public char getDoubled(){
|
||||
return doubled;
|
||||
}
|
||||
public void setDoubled(char doubled) throws InvalidCharacterException{
|
||||
//Make sure the character is a letter
|
||||
if(!Character.isAlphabetic(doubled)){
|
||||
throw new InvalidCharacterException("The double letter replacement must be a letter");
|
||||
}
|
||||
|
||||
//Make sure the 2 replacers are the same
|
||||
if(doubled == replacer){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the regular replacer");
|
||||
}
|
||||
|
||||
//Make sure the double letter replacement isn't the same as the letter being replaced
|
||||
if(doubled == replaced){
|
||||
throw new InvalidCharacterException("The double letter replacement cannot be the same as the letter replaced");
|
||||
}
|
||||
|
||||
this.doubled = Character.toUpperCase(doubled);
|
||||
}
|
||||
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