Added Javadoc comments to the classes
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Beaufort.java
|
||||
//Mattrixwv
|
||||
// Created: 02-23-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.monosubstitution;
|
||||
|
||||
|
||||
@@ -12,23 +28,53 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Beaufort cipher,
|
||||
* which is a variant of the Vigenère cipher with additional steps.
|
||||
*
|
||||
* <p>
|
||||
* The Beaufort cipher consists of three main steps:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>Atbash cipher for reversing the string</li>
|
||||
* <li>Caesar cipher for shifting characters by a fixed amount</li>
|
||||
* <li>Vigenère cipher for applying a keyword-based shift</li>
|
||||
* </ul>
|
||||
* This class allows you to encode and decode strings with options to preserve
|
||||
* capitalization, whitespace, and symbols.
|
||||
*/
|
||||
public class Beaufort{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Beaufort.class);
|
||||
//Fields
|
||||
protected String inputString; //This is the string that needs encoded/decoded
|
||||
protected String outputString; //This is the string that is output after encoding/decoding
|
||||
protected String keyword; //This is the keyword that is responsible for determining the offsets that you change each character by
|
||||
//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 Atbash atbash; //The first step in encoding/decoding the cipher
|
||||
protected Caesar caesar; //The second step in encoding/decoding the cipher
|
||||
protected Vigenere vigenere; //The third step in encoding/decoding the cipher
|
||||
//?Fields
|
||||
/** This is the string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** This is the string that is output after encoding/decoding */
|
||||
protected String outputString;
|
||||
/** This is the keyword that is responsible for determining the offsets that you change each character by */
|
||||
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/decoding the cipher */
|
||||
protected Atbash atbash;
|
||||
/** The second step in encoding/decoding the cipher */
|
||||
protected Caesar caesar;
|
||||
/** The third step in encoding/decoding the cipher */
|
||||
protected Vigenere vigenere;
|
||||
|
||||
|
||||
//Ensures inputString constraints
|
||||
/**
|
||||
* Sets the input string for encoding or decoding, applying removal options
|
||||
* for case, whitespace, and symbols.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
public void setInputString(String inputString) throws InvalidInputException{
|
||||
//Make sure the input isn't null
|
||||
if(inputString == null){
|
||||
@@ -63,7 +109,13 @@ public class Beaufort{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Ensures keyword constraints
|
||||
/**
|
||||
* Sets the keyword for encoding or decoding, ensuring it contains only
|
||||
* uppercase letters and is at least 2 letters long.
|
||||
*
|
||||
* @param keyword the keyword for the Vigenère cipher
|
||||
* @throws InvalidKeywordException if the keyword is null, blank, or less than 2 letters
|
||||
*/
|
||||
public void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Make sure the keyword isn't null
|
||||
if(keyword == null){
|
||||
@@ -88,20 +140,38 @@ public class Beaufort{
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Beaufort cipher and stores the result in {@code outputString}.
|
||||
*
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void encode() throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
code();
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Beaufort cipher and stores the result in {@code outputString}.
|
||||
* Decoding is the same process as encoding in this cipher.
|
||||
*
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
protected void decode() throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Decoding is just encoding again
|
||||
code();
|
||||
}
|
||||
//Codes input and saves to output
|
||||
/**
|
||||
* Performs the Beaufort cipher encoding/decoding process:
|
||||
* <ul>
|
||||
* <li>Encodes the input string using the Atbash cipher</li>
|
||||
* <li>Shifts the result by 1 using the Caesar cipher</li>
|
||||
* <li>Applies the Vigenère cipher using the keyword</li>
|
||||
* </ul>
|
||||
*/
|
||||
protected void code(){
|
||||
//Reverse the string
|
||||
logger.debug("Encoding with Atbash");
|
||||
@@ -120,7 +190,10 @@ public class Beaufort{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Beaufort} instance with default settings.
|
||||
*/
|
||||
public Beaufort(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
@@ -130,6 +203,13 @@ public class Beaufort{
|
||||
vigenere = new Vigenere(false, false, false);
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Beaufort} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Beaufort(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -140,7 +220,15 @@ public class Beaufort{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Encodes the input string using the specified keyword.
|
||||
*
|
||||
* @param keyword the keyword for the Vigenère cipher
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setKeyword(keyword);
|
||||
@@ -150,7 +238,15 @@ public class Beaufort{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
/**
|
||||
* Decodes the input string using the specified keyword.
|
||||
*
|
||||
* @param keyword the keyword for the Vigenère cipher
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the keyword is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
//Set the parameters
|
||||
setKeyword(keyword);
|
||||
@@ -161,17 +257,34 @@ public class Beaufort{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the input string, output string, and keyword to empty.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user