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/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");

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Atbash.java
//Mattrixwv
// 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;
@@ -11,18 +27,34 @@ import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
/**
* Implements the Atbash cipher, a simple substitution cipher where each letter of the alphabet is mapped to its reverse.
* For example, 'A' is mapped to 'Z', 'B' to 'Y', and so on.
* This class provides methods to encode and decode strings using the Atbash cipher.
*
* <p>
* The Atbash cipher is symmetric, meaning that encoding and decoding are the same operation.
* </p>
*/
public class Atbash{
private static final Logger logger = LoggerFactory.getLogger(Atbash.class);
//Fields
protected String inputString; //Holds the string that needs encoded or decoded
protected String outputString; //The encoded/decoded 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
/** Holds the string that needs encoded or decoded */
protected String inputString;
/** The encoded/decoded string */
protected String outputString;
//?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;
//Encodes inputString and stores in outputString
/**
* Encodes the input string using the Atbash cipher.
*/
protected void encode(){
logger.debug("Encoding");
StringBuilder output = new StringBuilder();
@@ -57,7 +89,12 @@ public class Atbash{
outputString = output.toString();
logger.debug("Saving output string '{}'", outputString);
}
//Removes all invalid characters and sets inputString
/**
* 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");
@@ -95,13 +132,24 @@ public class Atbash{
}
//Constructor
//?Constructor
/**
* Constructs a new {@code Atbash} instance with default settings:
* capitals, symbols, and whitespace are not preserved.
*/
public Atbash(){
reset();
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
}
/**
* Constructs a new {@code Atbash} 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
*/
public Atbash(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
reset();
this.preserveCapitals = preserveCapitals;
@@ -109,7 +157,13 @@ public class Atbash{
this.preserveSymbols = preserveSymbols;
}
//Encodes inputString and returns the result
/**
* Encodes the provided input string using the Atbash cipher and returns the encoded result.
*
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidInputException if the input string is invalid
*/
public String encode(String inputString) throws InvalidInputException{
//Make sure everything is empty before you begin
reset();
@@ -117,19 +171,38 @@ public class Atbash{
encode();
return outputString;
}
//Decodes inputString and returns the result
/**
* Decodes the provided input string using the Atbash cipher and returns the decoded result.
* Since the Atbash cipher is symmetric, this method performs the same operation as encoding.
*
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidInputException if the input string is invalid
*/
public String decode(String inputString) throws InvalidInputException{
return encode(inputString);
}
//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;
}
//Makes sure all variables are empty
/**
* Resets all fields to their default values.
*/
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
//Mattrixwv
// 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;
@@ -12,11 +28,28 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
/**
* Implements the Autokey cipher, an extension of the Vigenère cipher that uses a keyword combined with the plaintext itself as the key.
* The Autokey cipher adds the plaintext to the end of the keyword to create a longer key, which helps to make the encryption stronger.
* This class inherits from the {@code Vigenere} class and overrides methods to handle encoding and decoding with the Autokey cipher.
*
* <p>
* The Autokey cipher is symmetric, meaning that encoding and decoding are essentially the same process, but with a different key setup for decoding.
* </p>
*/
public class Autokey extends Vigenere{
private static final Logger logger = LoggerFactory.getLogger(Autokey.class);
//Special rules for setting the strings for encoding
/**
* Sets up the keyword and input string for encoding, generating a longer key that includes the plaintext.
* This method is used internally to prepare the cipher for encoding.
*
* @param keyword the keyword used for encoding
* @param inputString the string to be encoded
* @throws InvalidKeywordException if the keyword is invalid
* @throws InvalidInputException if the input string is invalid
*/
protected void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
logger.debug("Setting fields for encoding");
@@ -44,7 +77,15 @@ public class Autokey extends Vigenere{
offset.clear();
setOffset();
}
//Setting the strings for decoding
/**
* Sets up the keyword and input string for decoding. The keyword is used to decode the input string.
* This method is used internally to prepare the cipher for decoding.
*
* @param keyword the keyword used for decoding
* @param inputString the string to be decoded
* @throws InvalidKeywordException if the keyword is invalid
* @throws InvalidInputException if the input string is invalid
*/
protected void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
logger.debug("Setting fields for decoding");
@@ -56,7 +97,10 @@ public class Autokey extends Vigenere{
logger.debug("Setting input string");
setInputString(inputString);
}
//Decodes the inputString
/**
* Decodes the input string using the Autokey cipher.
* This method is overridden to handle decoding with the Autokey cipher, which involves using the key and updating it with decoded characters.
*/
@Override
protected void decode(){
logger.debug("Decoding");
@@ -113,14 +157,32 @@ public class Autokey extends Vigenere{
}
//Constructor
//?Constructor
/**
* Constructs a new {@code Autokey} instance with default settings for preserving capitals, whitespace, and symbols.
*/
public Autokey(){
super();
}
/**
* Constructs a new {@code Autokey} 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
*/
public Autokey(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
super(preserveCapitals, preserveWhitespace, preserveSymbols);
}
//Encodes inputString using the Autokey cipher
/**
* Encodes the input string using the Autokey cipher with the provided keyword.
*
* @param keyword the keyword used 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
*/
@Override
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();
@@ -128,7 +190,15 @@ public class Autokey extends Vigenere{
encode();
return outputString;
}
//Decodes inputString using the Autokey cipher
/**
* Decodes the input string using the Autokey cipher with the provided keyword.
*
* @param keyword the keyword used 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
*/
@Override
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
reset();

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 04-19-24
//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;
@@ -16,20 +32,38 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
/**
* Implements the Baconian cipher, a method of steganography where each letter of the alphabet is represented by a unique sequence of five characters ('a' or 'b').
* The Baconian cipher is a simple substitution cipher that can encode and decode text based on these sequences.
*
* <p>
* The cipher uses a predefined list of five-character strings to represent each letter of the alphabet (A-Z). The input string is converted to these sequences for encoding,
* and sequences are converted back to letters for decoding.
* </p>
*/
public class Baconian{
private static final Logger logger = LoggerFactory.getLogger(Baconian.class);
//Conversions
//?Conversions
/** Predefined code for Baconian cipher (5-character strings for A-Z) */
protected static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
));
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected boolean preserveCapitals; //Persist capitals in the output string
/** The string that needs encoded/decoded */
protected String inputString;
/** The encoded/decoded string */
protected String outputString;
/** Persist capitals in the output string */
protected boolean preserveCapitals;
//Sets the input string
/**
* Sets the input string for encoding, removing whitespace and symbols, and handling capitalization based on the flag.
*
* @param inputString the string to be encoded
* @throws InvalidInputException if the input string is null, empty, or invalid
*/
protected void setInputStringEncode(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -53,6 +87,13 @@ public class Baconian{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
/**
* Sets the input string for decoding, ensuring it contains only valid Baconian characters (a's and b's) with a length of 5.
*
* @param inputString the string to be decoded
* @throws InvalidCharacterException if the input string contains invalid Baconian characters
* @throws InvalidInputException if the input string is null or empty
*/
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -91,7 +132,10 @@ public class Baconian{
logger.debug("Cleaned input string '{}'", inputString);
}
//Encodes the inputString and stores the result in outputString
/**
* Encodes the input string using the Baconian cipher.
* Each character in the input string is converted to its corresponding Baconian sequence.
*/
protected void encode(){
logger.debug("Encoding");
StringJoiner output = new StringJoiner(" ");
@@ -121,7 +165,10 @@ public class Baconian{
outputString = output.toString();
logger.debug("Saving output string '{}'", outputString);
}
//Decodes the inputString and stores the result in outputString
/**
* Decodes the input string using the Baconian cipher.
* Each Baconian sequence in the input string is converted back to its corresponding character.
*/
protected void decode(){
logger.debug("Decoding");
@@ -157,24 +204,45 @@ public class Baconian{
logger.debug("Saving output string '{}'", outputString);
}
//Constructor
//?Constructor
/**
* Constructs a new {@code Baconian} instance with default settings for preserving capitals.
*/
public Baconian(){
reset();
preserveCapitals = false;
}
/**
* Constructs a new {@code Baconian} instance with a specified setting for preserving capitals.
*
* @param preserveCapitals whether to preserve capital letters in the output
*/
public Baconian(boolean preserveCapitals){
reset();
this.preserveCapitals = preserveCapitals;
}
//Sets the inputString and encodes the message
/**
* Encodes the input string using the Baconian cipher.
*
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidInputException if the input string is invalid
*/
public String encode(String inputString) throws InvalidInputException{
reset();
setInputStringEncode(inputString);
encode();
return outputString;
}
//Sets the inputString and decodes the message
/**
* Decodes the input string using the Baconian cipher.
*
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidCharacterException if the input string contains invalid Baconian characters
* @throws InvalidInputException if the input string is invalid
*/
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setInputStringDecode(inputString);
@@ -182,14 +250,26 @@ public class Baconian{
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;
}
//Makes sure all variables are empty
/**
* Resets the input and output strings to empty.
*/
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/BaseX.java
//Mattrixwv
// Created: 01-08-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;
@@ -15,16 +31,35 @@ import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
/**
* A class for encoding and decoding strings using a specified numerical base.
* The BaseX class allows encoding and decoding of strings where characters are represented in a given base.
* The base can be set to any value between Character.MIN_RADIX and Character.MAX_RADIX.
*
* <p>
* This class supports encoding and decoding of ASCII characters into their base-X representations,
* where X is the base provided by the user. It ensures that input strings are valid and within the acceptable range
* for the specified base.
* </p>
*/
public class BaseX{
private static final Logger logger = LoggerFactory.getLogger(BaseX.class);
//Fields
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
//Settings
protected int base; //The base that the number will be encoded at
//?Fields
/** The string that needs encoded/decoded */
protected String inputString;
/** The encoded/decoded string */
protected String outputString;
//?Settings
/** The base that the number will be encoded at */
protected int base;
//Sets the input string
/**
* Sets the input string for encoding, ensuring it is not null and contains at least one letter.
*
* @param inputString the string to be encoded
* @throws InvalidInputException if the input string is null or blank
*/
protected void setInputStringEncode(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -38,6 +73,13 @@ public class BaseX{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
/**
* Sets the input string for decoding, ensuring it is not null, does not contain invalid characters, and is properly formatted.
*
* @param inputString the string to be decoded
* @throws InvalidCharacterException if the input string contains invalid characters
* @throws InvalidInputException if the input string is null or blank
*/
protected void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -68,7 +110,12 @@ public class BaseX{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Sets the numeric base
/**
* Sets the base for encoding and decoding, ensuring it is within valid range.
*
* @param base the base to be set
* @throws InvalidBaseException if the base is less than Character.MIN_RADIX or greater than Character.MAX_RADIX
*/
protected void setBase(int base) throws InvalidBaseException{
if(base < Character.MIN_RADIX){
throw new InvalidBaseException("Base cannot be less than " + Character.MIN_RADIX);
@@ -81,7 +128,9 @@ public class BaseX{
this.base = base;
}
//Encode inputString, store it in outputString, and return it
/**
* Encodes the input string using the specified base.
*/
protected void encode(){
logger.debug("Encoding");
@@ -102,7 +151,11 @@ public class BaseX{
outputString = output.toString().toUpperCase();
logger.debug("Saving output string '{}'", outputString);
}
//Decode inputString, store it in outputString, and return it
/**
* Decodes the input string from the specified base.
*
* @throws InvalidCharacterException if the input string contains invalid characters for the base
*/
protected void decode() throws InvalidCharacterException{
logger.debug("Decoding");
@@ -129,23 +182,49 @@ public class BaseX{
logger.debug("Saving output string '{}'", outputString);
}
//Constructor
//?Constructor
/**
* Constructs a new {@code BaseX} instance with the default base of 2.
*
* @throws InvalidBaseException if the default base is invalid
*/
public BaseX() throws InvalidBaseException{
reset();
setBase(2);
}
/**
* Constructs a new {@code BaseX} instance with the specified base.
*
* @param base the base to be used for encoding and decoding
* @throws InvalidBaseException if the base is invalid
*/
public BaseX(int base) throws InvalidBaseException{
reset();
setBase(base);
}
//Sets the inputString and encodes the message
/**
* Encodes the given input string using the current base.
*
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidInputException if the input string is invalid
*/
public String encode(String inputString) throws InvalidInputException{
reset();
setInputStringEncode(inputString);
encode();
return outputString;
}
/**
* Encodes the given input string using the specified base.
*
* @param base the base to use for encoding
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidBaseException if the base is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String encode(int base, String inputString) throws InvalidBaseException, InvalidInputException{
reset();
setBase(base);
@@ -153,13 +232,30 @@ public class BaseX{
encode();
return outputString;
}
//Sets the inputString and decodes the message
/**
* Decodes the given input string using the current base.
*
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidCharacterException if the input string contains invalid characters
* @throws InvalidInputException if the input string is invalid
*/
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setInputStringDecode(inputString);
decode();
return outputString;
}
/**
* Decodes the given input string using the specified base.
*
* @param base the base to use for decoding
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidBaseException if the base is invalid
* @throws InvalidCharacterException if the input string contains invalid characters
* @throws InvalidInputException if the input string is invalid
*/
public String decode(int base, String inputString) throws InvalidBaseException, InvalidCharacterException, InvalidInputException{
reset();
setBase(base);
@@ -168,17 +264,34 @@ public class BaseX{
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 base.
*
* @return the base
*/
public int getBase(){
return base;
}
//Makes sure all variables are empty
/**
* Resets the input and output strings to empty.
*/
public void reset(){
logger.debug("Resetting fields");

View File

@@ -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");

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.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;
@@ -11,19 +27,39 @@ import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
/**
* A class for encoding and decoding strings using the Caesar cipher.
*
* <p>
* The Caesar cipher is a substitution cipher where each letter in the
* plaintext is shifted a fixed number of places down or up the alphabet.
* This class allows you to encode and decode strings with options to preserve
* capitalization, whitespace, and symbols.
* </p>
*/
public class Caesar{
private static final Logger logger = LoggerFactory.getLogger(Caesar.class);
//Fields
protected String inputString; //The string that needs encoded/decoded
protected String outputString; //The encoded/decoded string
protected int shift; //The amount that you need to shift each letter
//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 amount that you need to shift each letter */
protected int shift;
//?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;
//Sets shift and makes sure it is within the propper bounds
/**
* Sets the shift amount and ensures it is within the proper bounds (0-25).
*
* @param shiftAmount the amount to shift each letter
*/
protected void setShift(int shiftAmount){
logger.debug("Setting shift {}", shiftAmount);
@@ -32,7 +68,13 @@ public class Caesar{
logger.debug("Cleaned shift {}", shift);
}
//Sets the input string
/**
* 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
*/
protected void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -63,7 +105,9 @@ public class Caesar{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Encodes the inputString and stores the result in outputString
/**
* Encodes the input string by shifting letters according to the Caesar cipher.
*/
protected void encode(){
logger.debug("Encoding");
@@ -111,7 +155,9 @@ public class Caesar{
outputString = output.toString();
logger.debug("Saving encoded string '{}'", outputString);
}
//Decodes the inputString and stores the result in outputString
/**
* Decodes the input string by reversing the shift applied during encoding.
*/
protected void decode(){
logger.debug("Decoding");
@@ -164,13 +210,23 @@ public class Caesar{
logger.debug("Saving decoded string '{}'", outputString);
}
//Constructor
//?Constructor
/**
* Constructs a new {@code Caesar} instance with default settings.
*/
public Caesar(){
reset();
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
}
/**
* Constructs a new {@code Caesar} 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 Caesar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
reset();
this.preserveCapitals = preserveCapitals;
@@ -178,7 +234,14 @@ public class Caesar{
this.preserveSymbols = preserveSymbols;
}
//Sets the shift and inputString and encodes the message
/**
* Encodes the input string with the specified shift amount.
*
* @param shiftAmount the amount to shift each letter
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidInputException if the input string is invalid
*/
public String encode(int shiftAmount, String inputString) throws InvalidInputException{
reset();
setShift(shiftAmount);
@@ -186,7 +249,14 @@ public class Caesar{
encode();
return outputString;
}
//Sets the shift and inputString and decodes the message
/**
* Decodes the input string with the specified shift amount.
*
* @param shiftAmount the amount to shift each letter
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidInputException if the input string is invalid
*/
public String decode(int shiftAmount, String inputString) throws InvalidInputException{
reset();
setShift(shiftAmount);
@@ -195,17 +265,34 @@ public class Caesar{
return outputString;
}
//Getters
//?Getters
/**
* Gets the current input string.
*
* @return the input string
*/
public String getInputString(){
return inputString;
}
/**
* Gets the current shift amount.
*
* @return the shift amount
*/
public int getShift(){
return shift;
}
/**
* Gets the current output string.
*
* @return the output string
*/
public String getOutputString(){
return outputString;
}
//Makes sure all variables are empty
/**
* Resets the internal fields to their default values.
*/
public void reset(){
logger.debug("Resetting fields");

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/OneTimePad.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,21 +28,51 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
/**
* A class for encoding and decoding strings using the One-Time Pad cipher,
* which is a special case of the Vigenère cipher where the key is as long as
* the input message and used only once.
*
* <p>
* The One-Time Pad cipher provides perfect secrecy when the key is truly random,
* as long as the key length is equal to or greater than the length of the message
* and the key is used only once.
* </p>
*/
public class OneTimePad extends Vigenere{
private static final Logger logger = LoggerFactory.getLogger(OneTimePad.class);
//?Add some kind of entropy calculator?
//Constructor
//?Constructor
/**
* Constructs a new {@code OneTimePad} instance with default settings.
*/
public OneTimePad(){
super();
}
/**
* Constructs a new {@code OneTimePad} 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 OneTimePad(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
super(preserveCapitals, preserveWhitespace, preserveSymbols);
}
//Encodes input using key and returns the result
/**
* Encodes the input string using the One-Time Pad cipher with the provided key.
* The key must be at least as long as the input string.
*
* @param keyword the key to use for encoding
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidKeywordException if the key is shorter than the input string
* @throws InvalidInputException if the input string is invalid
*/
@Override
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
if(keyword.length() < inputString.length()){
@@ -37,7 +83,16 @@ public class OneTimePad extends Vigenere{
return super.encode(keyword, inputString);
}
//Decodes input using key and returns the result
/**
* Decodes the input string using the One-Time Pad cipher with the provided key.
* The key must be at least as long as the input string.
*
* @param keyword the key to use for decoding
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidKeywordException if the key is shorter than the input string
* @throws InvalidInputException if the input string is invalid
*/
@Override
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
if(keyword.length() < inputString.length()){

View File

@@ -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");

View File

@@ -1,7 +1,23 @@
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Substitution.java
//Mattrixwv
// Created: 02-22-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,19 +28,41 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
/**
* A class for encoding and decoding strings using a substitution cipher.
* This cipher can handle both alphabetic and alphanumeric keys and supports
* preserving or removing capitalization, whitespace, and symbols.
*
* <p>
* The substitution cipher replaces each letter or digit in the input string
* with a corresponding character from a key. The key must contain all letters
* of the alphabet and optionally digits, without duplicates.
* </p>
*/
public class Substitution{
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
//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/decode the input
//Getters
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/decode the input */
protected String keyword;
//?Getters
/** 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;
//Ensures key constraints are followed
/**
* Ensures all key constraints are followed.
*
* @param key the key to be used for encoding/decoding
* @throws InvalidKeywordException if the key is null, contains duplicates, or has invalid length
*/
protected void setKeyword(String key) throws InvalidKeywordException{
if(key == null){
throw new InvalidKeywordException("Key cannot be null");
@@ -71,7 +109,12 @@ public class Substitution{
logger.debug("Cleaned key '{}'", key);
this.keyword = key;
}
//Ensure intput 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{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
@@ -105,7 +148,9 @@ public class Substitution{
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
//Encodes the inputString and stores the result in outputString
/**
* Encodes the inputString using the provided key and stores the result in outputString.
*/
protected void encode(){
logger.debug("Encoding");
@@ -137,7 +182,9 @@ public class Substitution{
this.outputString = output.toString();
logger.debug("Encoded message '{}'", outputString);
}
//Decodes the inputString and stores the result in outputString
/**
* Decodes the inputString using the provided key and stores the result in outputString.
*/
protected void decode(){
logger.debug("Decoding");
@@ -173,13 +220,23 @@ public class Substitution{
logger.debug("Decoded message '{}'", outputString);
}
//Constructors
//?Constructors
/**
* Constructs a new {@code Substitution} instance with default settings.
*/
public Substitution(){
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
reset();
}
/**
* Constructs a new {@code Substitution} 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 Substitution(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
@@ -187,14 +244,30 @@ public class Substitution{
reset();
}
//Encodes inputString using the provided key
/**
* Encodes the inputString using the provided key.
*
* @param key the key to use for encoding
* @param inputString the string to be encoded
* @return the encoded string
* @throws InvalidKeywordException if the key is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKeyword(key);
setInputString(inputString);
encode();
return outputString;
}
//Decodes inputString using the provided key
/**
* Decodes the inputString using the provided key.
*
* @param key the key to use for decoding
* @param inputString the string to be decoded
* @return the decoded string
* @throws InvalidKeywordException if the key is invalid
* @throws InvalidInputException if the input string is invalid
*/
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKeyword(key);
setInputString(inputString);
@@ -202,17 +275,34 @@ public class Substitution{
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 all fields to their default values.
*/
public void reset(){
logger.debug("Resetting fields");

View File

@@ -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");