Added Javadoc comments to the classes
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Vigenere.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-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.monosubstitution;
|
||||
|
||||
|
||||
@@ -15,20 +31,38 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Vigenère cipher.
|
||||
* This cipher uses a keyword to determine the shift for each character in the input string.
|
||||
*
|
||||
* <p>
|
||||
* The Vigenère cipher applies a series of Caesar ciphers based on the letters of a keyword.
|
||||
* The keyword determines the shift for each letter in the input string.
|
||||
* </p>
|
||||
*/
|
||||
public class Vigenere{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Vigenere.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 resposible for determining the offsets that you change each character by
|
||||
protected ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
|
||||
/** 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 resposible for determining the offsets that you change each character by */
|
||||
protected String keyword;
|
||||
/** Holds the offsets coputed from each character in the keyword */
|
||||
protected ArrayList<Integer> offset;
|
||||
//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
|
||||
/** 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;
|
||||
|
||||
|
||||
//Uses keyword to calculate the offset for the Caesar cipher for each character
|
||||
/**
|
||||
* Calculates the offsets for the Vigenère cipher from the keyword.
|
||||
*/
|
||||
protected void setOffset(){
|
||||
logger.debug("Setting offset array from keyword");
|
||||
|
||||
@@ -43,7 +77,12 @@ public class Vigenere{
|
||||
|
||||
logger.debug("Offset {}", offset);
|
||||
}
|
||||
//Sets inputString
|
||||
/**
|
||||
* Sets the input string and applies transformation settings.
|
||||
*
|
||||
* @param inputString the string to be encoded/decoded
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -74,7 +113,12 @@ public class Vigenere{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Sets keyword
|
||||
/**
|
||||
* Sets the keyword for the Vigenère cipher and computes the offsets.
|
||||
*
|
||||
* @param keyword the keyword to be used for encoding/decoding
|
||||
* @throws InvalidKeywordException if the keyword is null, too short, or contains invalid characters
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
@@ -102,7 +146,9 @@ public class Vigenere{
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Encodes inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the Vigenère cipher and stores the result.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -147,7 +193,9 @@ public class Vigenere{
|
||||
outputString = output.toString();
|
||||
logger.debug("Encoded message '{}'", outputString);
|
||||
}
|
||||
//Decodes inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the Vigenère cipher and stores the result.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -194,7 +242,10 @@ public class Vigenere{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Vigenere} instance with default settings.
|
||||
*/
|
||||
public Vigenere(){
|
||||
offset = new ArrayList<>();
|
||||
reset();
|
||||
@@ -202,6 +253,13 @@ public class Vigenere{
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Vigenere} 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 Vigenere(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
offset = new ArrayList<>();
|
||||
reset();
|
||||
@@ -210,7 +268,15 @@ public class Vigenere{
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
|
||||
//Encodes input using key and returns the result
|
||||
/**
|
||||
* Encodes the input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword to use for encoding
|
||||
* @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{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -218,7 +284,15 @@ public class Vigenere{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
/**
|
||||
* Decodes the input string using the provided keyword.
|
||||
*
|
||||
* @param keyword the keyword to use for decoding
|
||||
* @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{
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
@@ -227,21 +301,42 @@ public class Vigenere{
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* Gets the current offset list.
|
||||
*
|
||||
* @return the offset list
|
||||
*/
|
||||
public List<Integer> getOffsets(){
|
||||
return offset;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user