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/combination/ADFGX.java
//Mattrixwv
// Created: 01-25-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.combination;
@@ -15,24 +31,50 @@ import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
/**
* Implements the ADFGX cipher, which is a combination of a Polybius square and a columnar transposition cipher.
* This class provides methods to encode and decode strings using the ADFGX cipher.
*
* <p>
* The cipher involves two main steps:
* </p>
* <ol>
* <li>Encoding/decoding with a Polybius square (PolybiusSquare)</li>
* <li>Encoding/decoding with a columnar transposition cipher (Columnar)</li>
* </ol>
*/
public class ADFGX{
private static final Logger logger = LoggerFactory.getLogger(ADFGX.class);
//Internal fields
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The string that is output after encoding/decoding
protected String squareKeyword; //The keyword used in the Polybius Square
protected String keyword; //The keyword used in the Columnar cipher
//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
//Internal ciphers
protected PolybiusSquare polybiusSquare; //The first step in encoding
protected Columnar columnar; //The second step in encoding
//?Internal fields
/** The string that needs encoded/decoded */
protected String inputString;
/** The string that is output after encoding/decoding */
protected String outputString;
/** The keyword used in the Polybius Square */
protected String squareKeyword;
/** The keyword used in the Columnar cipher */
protected String keyword;
//?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;
//?Internal ciphers
/** The first step in encoding */
protected PolybiusSquare polybiusSquare;
/** The second step in encoding */
protected Columnar columnar;
//Ensures Polybius keyword constraints
/**
* Sets the Polybius square keyword and validates it.
*
* @param squareKeyword the keyword for the Polybius square
* @throws InvalidKeywordException if the keyword is null
*/
protected void setSquareKeyword(String squareKeyword) throws InvalidKeywordException{
if(squareKeyword == null){
throw new InvalidKeywordException("Square keyword cannot be null");
@@ -41,7 +83,12 @@ public class ADFGX{
logger.debug("Square keyword '{}'", squareKeyword);
this.squareKeyword = squareKeyword;
}
//Ensures Columnar keyword constraints
/**
* Sets the columnar cipher keyword and validates it.
*
* @param keyword the keyword for the columnar cipher
* @throws InvalidKeywordException if the keyword is null
*/
protected void setKeyword(String keyword) throws InvalidKeywordException{
if(keyword == null){
throw new InvalidKeywordException("Keyword cannot be null");
@@ -50,7 +97,12 @@ public class ADFGX{
logger.debug("Keyword '{}'", keyword);
this.keyword = keyword;
}
//Ensures inputString constraints
/**
* Sets and sanitizes the input string according to the preservation settings.
*
* @param inputString the string to be processed
* @throws InvalidInputException if the input string is null or blank after processing
*/
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -80,7 +132,9 @@ public class ADFGX{
throw new InvalidInputException("Input cannot be blank");
}
}
//Format the output string with capitals, symbols, and numbers that are in the input string
/**
* Formats the output string to match the original input string when encoding.
*/
protected void formatOutputStringEncode(){
logger.debug("Formatting output string to match input string");
@@ -110,6 +164,9 @@ public class ADFGX{
outputString = output.toString();
logger.debug("Saving output string '{}'", outputString);
}
/**
* Formats the output string to match the original input string when decoding.
*/
protected void formatOutputStringDecode(){
logger.debug("Formatting output string to match input string");
StringBuilder output = new StringBuilder();
@@ -139,7 +196,13 @@ public class ADFGX{
outputString = output.toString();
logger.debug("Saving output string '{}'", outputString);
}
//Encodes the inputString and stores the result in outputString
/**
* Encodes the input string using the Polybius square and columnar cipher.
*
* @throws InvalidCharacterException if there are invalid characters in the ciphers
* @throws InvalidInputException if the input string is invalid
* @throws InvalidKeywordException if any of the keywords are invalid
*/
protected void encode() throws InvalidCharacterException, InvalidInputException, InvalidKeywordException{
//Encode the input with polybius
logger.debug("Encoding using Polybius Square");
@@ -158,7 +221,13 @@ public class ADFGX{
//Add whatever is needed to the output string
formatOutputStringEncode();
}
//Decodes the inputString and stores the result in outputString
/**
* Decodes the input string using the columnar cipher and Polybius square.
*
* @throws InvalidKeywordException if any of the keywords are invalid
* @throws InvalidCharacterException if there are invalid characters in the ciphers
* @throws InvalidInputException if the input string is invalid
*/
protected void decode() throws InvalidKeywordException, InvalidCharacterException, InvalidInputException{
//Decode the input with columnar
logger.debug("Decoding using columnar");
@@ -179,13 +248,27 @@ public class ADFGX{
}
//Constructor
//?Constructor
/**
* Constructs a new {@code ADFGX} instance with default settings:
* capitals, whitespace, and symbols are not preserved.
*
* @throws InvalidCharacterException if there are invalid characters in the ciphers
*/
public ADFGX() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
reset();
}
/**
* Constructs a new {@code ADFGX} instance with specified settings for preserving capitals, whitespace, and symbols.
*
* @param preserveCapitals whether to preserve capital letters in the output
* @param preserveWhitespace whether to preserve whitespace in the output
* @param preserveSymbols whether to preserve symbols in the output
* @throws InvalidCharacterException if there are invalid characters in the ciphers
*/
public ADFGX(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
@@ -193,7 +276,17 @@ public class ADFGX{
reset();
}
//Encodes inputString using keyword and returns the result
/**
* Encodes the provided input string using the specified keywords and returns the encoded result.
*
* @param squareKeyword the keyword for the Polybius square
* @param keyword the keyword for the columnar cipher
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidKeywordException if any of the keywords are invalid
* @throws InvalidInputException if the input string is invalid
* @throws InvalidCharacterException if the input contains invalid characters
*/
public String encode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword);
setKeyword(keyword);
@@ -201,7 +294,17 @@ public class ADFGX{
encode();
return outputString;
}
//Decodes inputString using keyword and returns the result
/**
* Decodes the provided input string using the specified keywords and returns the decoded result.
*
* @param squareKeyword the keyword for the Polybius square
* @param keyword the keyword for the columnar cipher
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidKeywordException if any of the keywords are invalid
* @throws InvalidInputException if the input string is invalid
* @throws InvalidCharacterException if the input contains invalid characters
*/
public String decode(String squareKeyword, String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
setSquareKeyword(squareKeyword);
setKeyword(keyword);
@@ -210,20 +313,44 @@ public class ADFGX{
return outputString;
}
//Getters
//?Getters
/**
* Returns the current input string.
*
* @return the input string
*/
public String getInputString(){
return inputString;
}
/**
* Returns the current output string.
*
* @return the output string
*/
public String getOutputString(){
return outputString;
}
/**
* Returns the current Polybius square keyword.
*
* @return the Polybius square keyword
*/
public String getSquareKeyword(){
return squareKeyword;
}
/**
* Returns the current columnar cipher keyword.
*
* @return the columnar cipher keyword
*/
public String getKeyword(){
return keyword;
}
//Makes sure all variables are empty
/**
* Resets all fields to their default values and reinitializes the internal ciphers.
*
* @throws InvalidCharacterException if there are invalid characters in the ciphers
*/
public void reset() throws InvalidCharacterException{
logger.debug("Resetting fields");