Added Javadoc comments to the classes
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Porta.java
|
||||
//Mattrixwv
|
||||
// Created: 02-28-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,9 +28,22 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Porta cipher,
|
||||
* which is a variant of the Vigenère cipher that uses a tableau of
|
||||
* alphabets to encode and decode messages.
|
||||
*
|
||||
* <p>
|
||||
* The Porta cipher uses a series of Caesar ciphers based on a repeating
|
||||
* keyword, with a pre-defined set of alphabetic shifts. This implementation
|
||||
* allows for encoding and decoding of messages while preserving or removing
|
||||
* certain characters based on configuration settings.
|
||||
* </p>
|
||||
*/
|
||||
public class Porta{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Porta.class);
|
||||
|
||||
/** Predefined alphabetic tableau used for encoding and decoding */
|
||||
private static final String[] tableau = {
|
||||
"NOPQRSTUVWXYZABCDEFGHIJKLM", //A-B
|
||||
"OPQRSTUVWXYZNMABCDEFGHIJKL", //C-D
|
||||
@@ -31,17 +60,28 @@ public class Porta{
|
||||
"ZNOPQRSTUVWXYBCDEFGHIJKLMA" //Y-Z
|
||||
};
|
||||
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
protected String keyword; //The keyword used to encode the input string
|
||||
//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 string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
/** The keyword used to encode the input string */
|
||||
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;
|
||||
|
||||
|
||||
//Ensure all keyword constraints are followed
|
||||
/**
|
||||
* Ensures all keyword constraints are followed.
|
||||
*
|
||||
* @param keyword the keyword to be used for encoding/decoding
|
||||
* @throws InvalidKeywordException if the keyword is null, empty, or less than 2 characters
|
||||
*/
|
||||
protected void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Make sure the keyword isn't null
|
||||
if(keyword == null){
|
||||
@@ -67,7 +107,12 @@ public class Porta{
|
||||
throw new InvalidKeywordException("Keyword must contain at least 2 letters");
|
||||
}
|
||||
}
|
||||
//Ensure all input constraints are followed
|
||||
/**
|
||||
* Ensures all input constraints are followed.
|
||||
*
|
||||
* @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{
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
@@ -102,7 +147,13 @@ public class Porta{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the letter that replaces the passed in letter
|
||||
/**
|
||||
* Returns the letter that replaces the passed-in letter based on the keyword and tableau.
|
||||
*
|
||||
* @param keywordCnt the index of the keyword character to use
|
||||
* @param letter the letter to be replaced
|
||||
* @return the replacement letter
|
||||
*/
|
||||
protected char getReplacer(int keywordCnt, char letter){
|
||||
logger.debug("Getting letter that replaces {} at {}", letter, keywordCnt);
|
||||
|
||||
@@ -130,21 +181,30 @@ public class Porta{
|
||||
logger.debug("Replacer {}", replacer);
|
||||
return replacer;
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the inputString and stores the result in outputString.
|
||||
* Encoding is the same as decoding for this cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Encoding is the same as decoding
|
||||
code();
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the inputString and stores the result in outputString.
|
||||
* Decoding is the same as encoding for this cipher.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Decoding is the same as encoding
|
||||
code();
|
||||
}
|
||||
//Codes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes or decodes the inputString based on the provided keyword.
|
||||
* Uses the tableau to replace characters.
|
||||
*/
|
||||
protected void code(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
@@ -175,13 +235,23 @@ public class Porta{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Porta} instance with default settings.
|
||||
*/
|
||||
public Porta(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Porta} 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 Porta(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -189,7 +259,15 @@ public class Porta{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Sets the keyword and inputString and encodes the message
|
||||
/**
|
||||
* Sets the keyword and inputString and encodes the message.
|
||||
*
|
||||
* @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{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -200,7 +278,15 @@ public class Porta{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the keyword and inputString and decodes the message
|
||||
/**
|
||||
* Sets the keyword and inputString and decodes the message.
|
||||
*
|
||||
* @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{
|
||||
//Set the parameters
|
||||
reset();
|
||||
@@ -212,17 +298,34 @@ public class Porta{
|
||||
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 fields are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user