Added Javadoc comments to the classes
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/polysubstitution/Affine.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-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;
|
||||
|
||||
|
||||
@@ -13,20 +29,44 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* Implements the Affine cipher, which is a monoalphabetic substitution cipher based on linear algebra.
|
||||
* This class provides methods to encode and decode strings using the Affine cipher with a specified key.
|
||||
*
|
||||
* <p>
|
||||
* The Affine cipher uses two keys:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li><strong>Key1</strong>: The multiplicative key (must be relatively prime to 26)</li>
|
||||
* <li><strong>Key2</strong>: The additive key</li>
|
||||
* </ul>
|
||||
*/
|
||||
public class Affine{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Affine.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The string that is output after encoding/decoding
|
||||
protected int key1; //The multiplicative key. Key1 must be relatively prime to 26
|
||||
protected int key2; //The additive key
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The string that is output after encoding/decoding */
|
||||
protected String outputString;
|
||||
/** The multiplicative key. Key1 must be relatively prime to 26 */
|
||||
protected int key1;
|
||||
/** The additive key */
|
||||
protected int key2;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
|
||||
|
||||
//Ensures key1 constraints
|
||||
/**
|
||||
* Sets the multiplicative key and validates it.
|
||||
*
|
||||
* @param key1 the multiplicative key
|
||||
* @throws InvalidKeywordException if the key1 is not relatively prime to 26
|
||||
*/
|
||||
protected void setKey1(int key1) throws InvalidKeywordException{
|
||||
logger.debug("Setting key1 {}", key1);
|
||||
|
||||
@@ -48,7 +88,11 @@ public class Affine{
|
||||
|
||||
logger.debug("Cleaned key1 {}", key1);
|
||||
}
|
||||
//Ensures key2 constraints
|
||||
/**
|
||||
* Sets the additive key.
|
||||
*
|
||||
* @param key2 the additive key
|
||||
*/
|
||||
protected void setKey2(int key2){
|
||||
logger.debug("Setting key2 {}", key2);
|
||||
|
||||
@@ -65,7 +109,12 @@ public class Affine{
|
||||
|
||||
logger.debug("Cleaned key2 {}", key2);
|
||||
}
|
||||
//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 must not be null");
|
||||
@@ -97,7 +146,9 @@ public class Affine{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string using the affine cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -138,7 +189,9 @@ public class Affine{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string using the affine cipher.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -194,13 +247,24 @@ public class Affine{
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Affine} instance with default settings:
|
||||
* capitals, symbols, and whitespace are not preserved.
|
||||
*/
|
||||
public Affine(){
|
||||
preserveCapitals = false;
|
||||
preserveSymbols = false;
|
||||
preserveWhitespace = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Affine} instance with specified settings for preserving capitals, symbols, and whitespace.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public Affine(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
@@ -208,7 +272,16 @@ public class Affine{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using key1 and key2 and returns the result
|
||||
/**
|
||||
* Encodes the provided input string using the specified keys and returns the encoded result.
|
||||
*
|
||||
* @param key1 the multiplicative key (must be relatively prime to 26)
|
||||
* @param key2 the additive key
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if key1 is not relatively prime to 26
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
@@ -216,7 +289,16 @@ public class Affine{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using key1 and key2 and returns the result
|
||||
/**
|
||||
* Decodes the provided input string using the specified keys and returns the decoded result.
|
||||
*
|
||||
* @param key1 the multiplicative key (must be relatively prime to 26)
|
||||
* @param key2 the additive key
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if key1 is not relatively prime to 26
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
@@ -225,20 +307,42 @@ public class Affine{
|
||||
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 multiplicative key.
|
||||
*
|
||||
* @return the multiplicative key
|
||||
*/
|
||||
public int getKey1(){
|
||||
return key1;
|
||||
}
|
||||
/**
|
||||
* Returns the current additive key.
|
||||
*
|
||||
* @return the additive key
|
||||
*/
|
||||
public int getKey2(){
|
||||
return key2;
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user