Added Javadoc comments to the classes

This commit is contained in:
2024-08-11 23:48:02 -04:00
parent b16956b184
commit c10df8e5df
29 changed files with 3270 additions and 431 deletions

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/PolybiusSquare.java
//Mattrixwv
// Created: 01-04-22
//Modified: 05-04-23
//Modified: 08-11-24
/*
Copyright (C) 2024 Mattrixwv
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv.cipherstream.polysubstitution;
@@ -15,38 +31,74 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
/**
* Represents the Polybius square cipher encryption and decryption.
* The Polybius square cipher is a classical encryption method that uses a 5x5 grid
* to encode and decode messages based on the positions of letters in the grid.
*/
public class PolybiusSquare{
private static final Logger logger = LoggerFactory.getLogger(PolybiusSquare.class);
//A class representing the location of a character in the grid
/** A class representing the location of a character in the grid */
protected class CharLocation{
/** The x location in the grid */
private int x;
/** The y location in the grid */
private int y;
/**
* Constructs a CharLocation with the specified x and y coordinates.
*
* @param x the x-coordinate of the character's location
* @param y the y-coordinate of the character's location
*/
public CharLocation(int x, int y){
this.x = x;
this.y = y;
}
/**
* Returns the x-coordinate of the character's location.
*
* @return the x-coordinate
*/
public int getX(){
return x;
}
/**
* Returns the y-coordinate of the character's location.
*
* @return the y-coordinate
*/
public int getY(){
return y;
}
}
//Fields
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
//Settings
protected boolean preserveWhitespace; //Persist whitespace in the output string
protected boolean preserveSymbols; //Persist symbols in the output string
//?Fields
/** The message that needs to be encoded/decoded */
protected String inputString;
/** The encoded/decoded message */
protected String outputString;
/** The keyword used to create the grid */
protected String keyword;
/** The grid used to encode/decode the message */
protected char[][] grid;
/** The letter that will need to be replaced in the grid and any input string or keyword */
protected char replaced;
/** The letter that replaces replaced in the input string or keyword */
protected char replacer;
//?Settings
/** Persist whitespace in the output string */
protected boolean preserveWhitespace;
/** Persist symbols in the output string */
protected boolean preserveSymbols;
//Setting the character to be replaced
/**
* Sets the replaced character.
*
* @param replaced the character to be replaced
* @throws InvalidCharacterException if the character is not a letter or is invalid
*/
protected void setReplaced(char replaced) throws InvalidCharacterException{
logger.debug("Setting replaced");
logger.debug("Original character {}", replaced);
@@ -63,7 +115,12 @@ public class PolybiusSquare{
this.replaced = Character.toUpperCase(replaced);
logger.debug("Cleaned character {}", this.replaced);
}
//Setting the character that replaces replaced
/**
* Sets the replacer character.
*
* @param replacer the character the replaces replaced
* @throws InvalidCharacterException if the character is not a letter or is invalid
*/
protected void setReplacer(char replacer) throws InvalidCharacterException{
logger.debug("Setting replacer");
logger.debug("Original character {}", replacer);
@@ -80,7 +137,9 @@ public class PolybiusSquare{
this.replacer = Character.toUpperCase(replacer);
logger.debug("Cleaned character {}", this.replacer);
}
//Create the grid from the keyword
/**
* Creates the grid from the keyword.
*/
protected void createGrid(){
logger.debug("Creating grid from keyword");
@@ -93,7 +152,13 @@ public class PolybiusSquare{
logger.debug("Created grid\n{}", getGrid());
}
//Strips invalid characters from the string that needs encoded/decoded
/**
* Strips invalid characters from the string that needs encoding/decoding.
*
* @param inputString the input string to be cleaned
* @throws InvalidCharacterException if an invalid character is found
* @throws InvalidInputException if the input string is invalid
*/
protected void setInputStringEncode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -148,6 +213,13 @@ public class PolybiusSquare{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
/**
* Strips invalid characters from the string that needs decoding.
*
* @param inputString the input string to be cleaned
* @throws InvalidCharacterException if an invalid character is found
* @throws InvalidInputException if the input string is invalid
*/
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -193,7 +265,11 @@ public class PolybiusSquare{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Returns the input string ready for encoding
/**
* Returns the input string ready for encoding.
*
* @return the prepared input string
*/
protected String getPreparedInputStringEncode(){
logger.debug("Preparing input string for encoding");
@@ -203,6 +279,11 @@ public class PolybiusSquare{
logger.debug("Prepared string '{}'", cleanString);
return cleanString;
}
/**
* Returns the input string ready for decoding.
*
* @return the prepared input string
*/
protected String getPreparedInputStringDecode(){
logger.debug("Preparing input string for decoding");
@@ -211,7 +292,11 @@ public class PolybiusSquare{
logger.debug("Prepared string '{}'", cleanString);
return cleanString;
}
//Strips invalid characters from the keyword and creates the grid
/**
* Strips invalid characters from the keyword and creates the grid.
*
* @param keyword the keyword to be processed
*/
protected void setKeyword(String keyword){
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
@@ -244,7 +329,13 @@ public class PolybiusSquare{
//Create the grid from the sanitized keyword
createGrid();
}
//Returns the location of the given charcter in the grid
/**
* Returns the location of the given character in the grid.
*
* @param letter the character whose location is to be found
* @return the location of the character in the grid
* @throws InvalidInputException if the character is not found in the grid
*/
protected CharLocation findChar(char letter) throws InvalidInputException{
logger.debug("Finding {} in grid", letter);
@@ -260,7 +351,11 @@ public class PolybiusSquare{
//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
/**
* Adds characters that aren't letters to the output during encoding.
*
* @param cleanString the cleaned string to be formatted
*/
protected void addCharactersToCleanStringEncode(String cleanString){
logger.debug("Formatting output string for encoding");
@@ -287,6 +382,11 @@ public class PolybiusSquare{
outputString = fullOutput.toString();
logger.debug("Formatted output '{}'", outputString);
}
/**
* Adds characters that aren't letters to the output during decoding.
*
* @param cleanString the cleaned string to be formatted
*/
protected void addCharactersToCleanStringDecode(String cleanString){
logger.debug("Formatting output string for decoding");
@@ -313,7 +413,11 @@ public class PolybiusSquare{
outputString = fullOutput.toString();
logger.debug("Formatted output '{}'", outputString);
}
//Encodes inputString using the Playfair cipher and stores the result in outputString
/**
* Encodes the input string using the Polybius cipher and stores the result in the output string.
*
* @throws InvalidInputException if the input string is invalid
*/
protected void encode() throws InvalidInputException{
logger.debug("Encoding");
@@ -337,7 +441,11 @@ public class PolybiusSquare{
//Add other characters to the output string
addCharactersToCleanStringEncode(output.toString());
}
//Decodes inputString using the Playfair cipher and stores the result in outputString
/**
* Decodes the input string using the Polybius cipher and stores the result in the output string.
*
* @throws InvalidInputException if the input string is invalid
*/
protected void decode(){
logger.debug("Decoding");
@@ -363,7 +471,12 @@ public class PolybiusSquare{
addCharactersToCleanStringDecode(output.toString());
}
//Constructors
//?Constructors
/**
* Constructs a PolybiusSquare cipher instance with default settings.
*
* @throws InvalidCharacterException if default characters are invalid
*/
public PolybiusSquare() throws InvalidCharacterException{
reset();
setReplaced('J');
@@ -371,6 +484,13 @@ public class PolybiusSquare{
preserveWhitespace = false;
preserveSymbols = false;
}
/**
* Constructs a PolybiusSquare cipher instance with specified settings.
*
* @param preserveWhitespace whether to preserve whitespace
* @param preserveSymbols whether to preserve symbols
* @throws InvalidCharacterException if default characters are invalid
*/
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
reset();
setReplaced('J');
@@ -378,6 +498,15 @@ public class PolybiusSquare{
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
}
/**
* Constructs a PolybiusSquare cipher instance with specified settings and characters.
*
* @param preserveWhitespace whether to preserve whitespace
* @param preserveSymbols whether to preserve symbols
* @param replaced the character to be replaced
* @param replacer the character to replace the replaced character
* @throws InvalidCharacterException if any character is invalid
*/
public PolybiusSquare(boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer) throws InvalidCharacterException{
reset();
setReplaced(replaced);
@@ -386,10 +515,26 @@ public class PolybiusSquare{
this.preserveSymbols = preserveSymbols;
}
//Sets the keyword and inputString and encodes the message
/**
* Sets the keyword and input string and encodes the message.
*
* @param inputString the message to encode
* @return the encoded message
* @throws InvalidCharacterException if any character is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String encode(String inputString) throws InvalidCharacterException, InvalidInputException{
return encode("", inputString);
}
/**
* Sets the keyword and input string and encodes the message.
*
* @param keyword the keyword for the cipher
* @param inputString the message to encode
* @return the encoded message
* @throws InvalidCharacterException if any character is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String encode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setKeyword(keyword);
@@ -397,10 +542,26 @@ public class PolybiusSquare{
encode();
return outputString;
}
//Sets the keyword and inputString and decodes the message
/**
* Sets the keyword and input string and decodes the message.
*
* @param inputString the encoded message to decode
* @return the decoded message
* @throws InvalidCharacterException if any character is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
return decode("", inputString);
}
/**
* Sets the keyword and input string and decodes the message.
*
* @param keyword the keyword for the cipher
* @param inputString the encoded message to decode
* @return the decoded message
* @throws InvalidCharacterException if any character is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setKeyword(keyword);
@@ -409,7 +570,9 @@ public class PolybiusSquare{
return outputString;
}
//Makes sure all variables are empty
/**
* Resets all variables to their default values.
*/
public void reset(){
logger.debug("Resetting fields");
@@ -419,22 +582,52 @@ public class PolybiusSquare{
keyword = "";
}
//Getters
//?Getters
/**
* Returns the replaced character.
*
* @return the replaced character
*/
public char getReplaced(){
return replaced;
}
/**
* Returns the replacer character.
*
* @return the replacer character
*/
public char getReplacer(){
return replacer;
}
/**
* Returns the keyword used in the cipher.
*
* @return the keyword
*/
public String getKeyword(){
return keyword;
}
/**
* Returns the input string that was set for encoding/decoding.
*
* @return the input string
*/
public String getInputString(){
return inputString;
}
/**
* Returns the output string after encoding/decoding.
*
* @return the output string
*/
public String getOutputString(){
return outputString;
}
/**
* Returns a string representation of the grid.
*
* @return the grid as a string
*/
public String getGrid(){
logger.debug("Creating string from grid");