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/RailFence.java
//Mattrixwv
// Created: 03-21-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;
@@ -14,19 +30,35 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
/**
* Represents the Rail Fence cipher encryption and decryption.
* The Rail Fence cipher is a form of transposition cipher that writes the message in a zigzag pattern
* across multiple "rails" and then reads it off row by row to encode or decode the message.
*/
public class RailFence{
private static final Logger logger = LoggerFactory.getLogger(RailFence.class);
//Fields
protected String inputString; //The message that needs to be encoded/decoded
protected String outputString; //The encoded/decoded message
protected StringBuilder[] fence; //The fence used for encoding/decoding
//Settings
protected boolean preserveCapitals; //Persist capitals 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 fence used for encoding/decoding */
protected StringBuilder[] fence;
//?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;
//Strips invalid characters from the string that needs encoded/decoded
/**
* Strips invalid characters from the string that needs to be encoded/decoded.
*
* @param inputString the input string to be cleaned
* @throws InvalidInputException if the input string is null or invalid
*/
protected void setInputString(String inputString) throws InvalidInputException{
//Ensure the input string isn't null
if(inputString == null){
@@ -61,7 +93,12 @@ public class RailFence{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Ensures the number of rails is valid and sets up the fence
/**
* Ensures the number of rails is valid and sets up the fence.
*
* @param numRails the number of rails to be used
* @throws InvalidBaseException if the number of rails is less than 2
*/
protected void setNumRails(int numRails) throws InvalidBaseException{
if(numRails < 2){
throw new InvalidBaseException("You must use at least 2 rails");
@@ -74,13 +111,21 @@ public class RailFence{
fence[cnt] = new StringBuilder();
}
}
//Strip the inputString of all non-letter characters
/**
* Strips the input string of all non-letter characters.
*
* @return the cleaned input string
*/
protected String getCleanInputString(){
logger.debug("Getting input string for encoding");
return inputString.replaceAll("[^a-zA-Z]", "");
}
//Ensures capitals, lowercase, and symbols are displayed in the output string
/**
* Ensures capitals, lowercase, and symbols are displayed in the output string.
*
* @param outputString the encoded/decoded output string to be formatted
*/
protected void formatOutput(String outputString){
logger.debug("Formatting output string");
@@ -109,7 +154,11 @@ public class RailFence{
this.outputString = output.toString();
logger.debug("Formatted output '{}'", this.outputString);
}
//Returns the decoded string found in the fence after all characters are placed correctly
/**
* Returns the decoded string found in the fence after all characters are placed correctly.
*
* @return the decoded string
*/
protected String getDecodedStringFromFence(){
logger.debug("Getting decoded string from the fence");
@@ -161,7 +210,9 @@ public class RailFence{
logger.debug("Fence output '{}'", output);
return output.toString();
}
//Encodes inputString using the RailFence cipher and stores the result in outputString
/**
* Encodes the input string using the Rail Fence cipher and stores the result in the output string.
*/
protected void encode(){
logger.debug("Encoding");
@@ -204,7 +255,9 @@ public class RailFence{
//Format the output
formatOutput(output.toString());
}
//Decodes inputString using the RailFence cipher and stores the result in outputString
/**
* Decodes the input string using the Rail Fence cipher and stores the result in the output string.
*/
protected void decode(){
logger.debug("Decoding");
@@ -256,13 +309,23 @@ public class RailFence{
formatOutput(output);
}
//Constructor
//?Constructor
/**
* Constructs a RailFence cipher instance with default settings.
*/
public RailFence(){
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
reset();
}
/**
* Constructs a RailFence cipher instance with specified settings.
*
* @param preserveCapitals whether to preserve uppercase letters
* @param preserveWhitespace whether to preserve whitespace
* @param preserveSymbols whether to preserve symbols
*/
public RailFence(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
@@ -270,7 +333,15 @@ public class RailFence{
reset();
}
//Encodes inputString using a Rail Fence of length numRails and returns the result
/**
* Encodes the input string using a Rail Fence of the specified number of rails and returns the result.
*
* @param numRails the number of rails to use
* @param inputString the message to encode
* @return the encoded message
* @throws InvalidBaseException if the number of rails is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String encode(int numRails, String inputString) throws InvalidBaseException, InvalidInputException{
//Set the parameters
setNumRails(numRails);
@@ -280,7 +351,15 @@ public class RailFence{
encode();
return outputString;
}
//Decodes inputString using a Rail Fence of length numRails and returns the result
/**
* Decodes the input string using a Rail Fence of the specified number of rails and returns the result.
*
* @param numRails the number of rails to use
* @param inputString the encoded message to decode
* @return the decoded message
* @throws InvalidBaseException if the number of rails is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String decode(int numRails, String inputString) throws InvalidBaseException, InvalidInputException{
//Set the parameters
setNumRails(numRails);
@@ -291,7 +370,9 @@ public class RailFence{
return outputString;
}
//Makes sure all variables are empty
/**
* Resets all variables to their default values.
*/
public void reset(){
logger.debug("Resetting fields");
@@ -300,13 +381,28 @@ public class RailFence{
fence = null;
}
//Getters
//?Getters
/**
* 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 the number of rails used in the Rail Fence cipher.
*
* @return the number of rails
*/
public int getNumRails(){
return fence.length;
}