Added Javadoc comments to the classes
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Playfair.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-30-21
|
||||
//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,40 +31,77 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* Represents the Playfair cipher encryption and decryption.
|
||||
* The Playfair cipher is a digraph substitution cipher that encrypts pairs of letters.
|
||||
*/
|
||||
public class Playfair{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Playfair.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 */
|
||||
protected int x;
|
||||
/** The y location in the grid */
|
||||
protected 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 replaced replaced in the input string or keyword
|
||||
protected char doubled; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist captials in the output string
|
||||
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 replaced replaced in the input string or keyword */
|
||||
protected char replacer;
|
||||
/** The letter that will be placed between double letters in the input string if necessary or to make the string length even */
|
||||
protected char doubled;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Set the doubled character
|
||||
/**
|
||||
* Sets the doubled character.
|
||||
*
|
||||
* @param doubled the character to be used as the doubled character
|
||||
* @throws InvalidCharacterException if the character is not a letter or is invalid
|
||||
*/
|
||||
protected void setDoubled(char doubled) throws InvalidCharacterException{
|
||||
logger.debug("Setting doubled");
|
||||
logger.debug("Original character {}", doubled);
|
||||
@@ -80,7 +133,12 @@ public class Playfair{
|
||||
logger.debug("Setting doubled to {}", doubled);
|
||||
this.doubled = doubled;
|
||||
}
|
||||
//Set the replacer character
|
||||
/**
|
||||
* Sets the replacer character.
|
||||
*
|
||||
* @param replacer the character to replace the replaced character
|
||||
* @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);
|
||||
@@ -112,7 +170,12 @@ public class Playfair{
|
||||
logger.debug("Setting replacer to {}", replacer);
|
||||
this.replacer = replacer;
|
||||
}
|
||||
//Set the replaced character
|
||||
/**
|
||||
* 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);
|
||||
@@ -144,7 +207,9 @@ public class Playfair{
|
||||
logger.debug("Setting replaced to {}", replaced);
|
||||
this.replaced = replaced;
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
/**
|
||||
* Creates the grid from the keyword.
|
||||
*/
|
||||
protected void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
@@ -158,7 +223,14 @@ public class Playfair{
|
||||
|
||||
logger.debug("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
|
||||
* @param encoding true if encoding, false if decoding
|
||||
* @throws InvalidCharacterException if an invalid character is found
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void setInputString(String inputString, boolean encoding) throws InvalidCharacterException, InvalidInputException{
|
||||
logger.debug("Setting input string");
|
||||
|
||||
@@ -214,6 +286,11 @@ public class Playfair{
|
||||
throw new InvalidInputException("Input must have at least 1 letter");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Cleans up the input string for encoding.
|
||||
*
|
||||
* @param inputString the input string to be cleaned
|
||||
*/
|
||||
protected void setInputStringEncode(String inputString){
|
||||
logger.debug("Cleaning up input string for encoding");
|
||||
|
||||
@@ -286,7 +363,11 @@ public class Playfair{
|
||||
this.inputString = cleanInput.toString();
|
||||
logger.debug("Cleaned input string '{}'", this.inputString);
|
||||
}
|
||||
//Returns the input string ready for encoding
|
||||
/**
|
||||
* Returns the input string ready for encoding.
|
||||
*
|
||||
* @return the prepared input string
|
||||
*/
|
||||
protected String getPreparedInputString(){
|
||||
logger.debug("Getting input string ready for encoding");
|
||||
|
||||
@@ -297,7 +378,12 @@ public class Playfair{
|
||||
|
||||
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
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
logger.debug("Setting keyword");
|
||||
|
||||
@@ -333,7 +419,13 @@ public class Playfair{
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Returns the location of the given character 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 character in grid {}", letter);
|
||||
|
||||
@@ -350,7 +442,13 @@ public class Playfair{
|
||||
//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
|
||||
/**
|
||||
* Returns the character in the grid at the specified coordinates.
|
||||
*
|
||||
* @param x the x-coordinate
|
||||
* @param y the y-coordinate
|
||||
* @return the character at the specified coordinates
|
||||
*/
|
||||
protected char getGridChar(int x, int y){
|
||||
logger.debug("Getting character from grid[{}][{}]", x, y);
|
||||
|
||||
@@ -365,7 +463,11 @@ public class Playfair{
|
||||
logger.debug("Character {}", letter);
|
||||
return letter;
|
||||
}
|
||||
//Adds characters that aren't letters to the output
|
||||
/**
|
||||
* Adds characters that aren't letters to the output.
|
||||
*
|
||||
* @param cleanString the cleaned string to be formatted
|
||||
*/
|
||||
protected void addCharactersToCleanString(String cleanString){
|
||||
logger.debug("Formatting output string");
|
||||
|
||||
@@ -394,7 +496,11 @@ public class Playfair{
|
||||
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 Playfair 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");
|
||||
|
||||
@@ -438,7 +544,11 @@ public class Playfair{
|
||||
//Add other characters to the output string
|
||||
addCharactersToCleanString(output.toString());
|
||||
}
|
||||
//Decodes inputString using the Playfair cipher and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Playfair cipher and stores the result in the output string.
|
||||
*
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decode() throws InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -483,7 +593,12 @@ public class Playfair{
|
||||
addCharactersToCleanString(output.toString());
|
||||
}
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a Playfair cipher instance with default settings.
|
||||
*
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public Playfair() throws InvalidCharacterException{
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
@@ -493,6 +608,14 @@ public class Playfair{
|
||||
setReplacer('i');
|
||||
setDoubled('x');
|
||||
}
|
||||
/**
|
||||
* Constructs a Playfair cipher instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters
|
||||
* @param preserveWhitespace whether to preserve whitespace
|
||||
* @param preserveSymbols whether to preserve symbols
|
||||
* @throws InvalidCharacterException if default characters are invalid
|
||||
*/
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
@@ -502,6 +625,17 @@ public class Playfair{
|
||||
setReplacer('i');
|
||||
setDoubled('x');
|
||||
}
|
||||
/**
|
||||
* Constructs a Playfair cipher instance with specified settings and characters.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capital letters
|
||||
* @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
|
||||
* @param doubled the character to use for doubled letters
|
||||
* @throws InvalidCharacterException if any character is invalid
|
||||
*/
|
||||
public Playfair(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char replaced, char replacer, char doubled) throws InvalidCharacterException{
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
@@ -512,7 +646,15 @@ public class Playfair{
|
||||
setDoubled(doubled);
|
||||
}
|
||||
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
/**
|
||||
* Sets the keyword and input string and encodes the message.
|
||||
*
|
||||
* @param keyword the keyword for the cipher
|
||||
* @param input 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 input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -520,7 +662,15 @@ public class Playfair{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
/**
|
||||
* Sets the keyword and input string and decodes the message.
|
||||
*
|
||||
* @param keyword the keyword for the cipher
|
||||
* @param input 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 input) throws InvalidCharacterException, InvalidInputException{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -529,7 +679,9 @@ public class Playfair{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all variables to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
@@ -538,25 +690,60 @@ public class Playfair{
|
||||
outputString = "";
|
||||
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 doubled character.
|
||||
*
|
||||
* @return the doubled character
|
||||
*/
|
||||
public char getDoubled(){
|
||||
return doubled;
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
/**
|
||||
* Return the grid used for encoding/decoding.
|
||||
*
|
||||
* @return the grid as a string
|
||||
*/
|
||||
public String getGrid(){
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user