Added logging
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-22
|
||||
//Modified: 02-17-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
@@ -12,15 +15,21 @@ import mattrixwv.NumberAlgorithms;
|
||||
|
||||
|
||||
public class Affine{
|
||||
private boolean preserveCapitals;
|
||||
private boolean preserveSymbols;
|
||||
private boolean preserveWhitespace;
|
||||
private String inputString;
|
||||
private String outputString;
|
||||
private int key1; //Key1 must be relatively prime to 26
|
||||
private int key2;
|
||||
private static final Logger logger = LoggerFactory.getLogger(Affine.class);
|
||||
|
||||
//Fields
|
||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The string that is output after encoding/decoding
|
||||
private int key1; //The multiplicative key. Key1 must be relatively prime to 26
|
||||
private int key2; //The additive key
|
||||
|
||||
//Ensures key1 constraints
|
||||
private void setKey1(int key1) throws InvalidKeywordException{
|
||||
logger.debug("Setting key1 {}", key1);
|
||||
|
||||
//If the key is negative change it to possitive
|
||||
if(key1 < 0){
|
||||
key1 %= 26;
|
||||
@@ -34,8 +43,13 @@ public class Affine{
|
||||
|
||||
//Save the key
|
||||
this.key1 = key1;
|
||||
|
||||
logger.debug("Cleaned key1 {}", key1);
|
||||
}
|
||||
//Ensures key2 constraints
|
||||
private void setKey2(int key2){
|
||||
logger.debug("Setting key2 {}", key2);
|
||||
|
||||
//If the key is negative change it to possitive
|
||||
if(key2 < 0){
|
||||
key2 %= 26;
|
||||
@@ -44,32 +58,49 @@ public class Affine{
|
||||
|
||||
//Save the key
|
||||
this.key2 = key2;
|
||||
|
||||
logger.debug("Cleaned key2 {}", key2);
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string '{}'", inputString);
|
||||
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing case");
|
||||
|
||||
inputString = inputString.toLowerCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
this.inputString = inputString;
|
||||
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
private String encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Step through every character in the input and encode it if needed
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(char ch : inputString.toCharArray()){
|
||||
logger.debug("Current char {}", ch);
|
||||
//Encode all letters
|
||||
if(Character.isUpperCase(ch)){
|
||||
//Change the character to a number
|
||||
@@ -79,6 +110,8 @@ public class Affine{
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 65);
|
||||
output.append(newChar);
|
||||
|
||||
logger.debug("Encoded char {}", newChar);
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
//Change the character to a number
|
||||
@@ -88,6 +121,8 @@ public class Affine{
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 97);
|
||||
output.append(newChar);
|
||||
|
||||
logger.debug("Encoded char {}", newChar);
|
||||
}
|
||||
//Pass all other characters through
|
||||
else{
|
||||
@@ -96,19 +131,25 @@ public class Affine{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Saving output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
private String decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Find the multiplicative inverse of key1
|
||||
int key1I = 1;
|
||||
while(((key1 * key1I) % 26) != 1){
|
||||
++key1I;
|
||||
}
|
||||
logger.debug("Key1 inverse {}", key1I);
|
||||
|
||||
//Step through every character in the input and decode it if needed
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(char ch : inputString.toCharArray()){
|
||||
logger.debug("Current char {}", ch);
|
||||
//Encode all letters
|
||||
if(Character.isUpperCase(ch)){
|
||||
//Change the letter to a number
|
||||
@@ -121,6 +162,8 @@ public class Affine{
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 65);
|
||||
output.append(newChar);
|
||||
|
||||
logger.debug("Decoded char {}", newChar);
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
//Change the letter to a number
|
||||
@@ -133,6 +176,8 @@ public class Affine{
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 97);
|
||||
output.append(newChar);
|
||||
|
||||
logger.debug("Decoded char {}", newChar);
|
||||
}
|
||||
//Pass all other characters through
|
||||
else{
|
||||
@@ -141,10 +186,13 @@ public class Affine{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Saving output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Affine(){
|
||||
preserveCapitals = false;
|
||||
preserveSymbols = false;
|
||||
@@ -157,12 +205,15 @@ public class Affine{
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using key1 and key2 and returns the result
|
||||
public String encode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Decodes inputString using key1 and key2 and returns the result
|
||||
public String decode(int key1, int key2, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
@@ -170,22 +221,29 @@ public class Affine{
|
||||
return decode();
|
||||
}
|
||||
|
||||
//Returns the cleaned inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns the outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Returns the cleaned key1
|
||||
public int getKey1(){
|
||||
return key1;
|
||||
}
|
||||
//Returns the cleaned key2
|
||||
public int getKey2(){
|
||||
return key2;
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
key1 = 0;
|
||||
key2 = 0;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public int getKey1(){
|
||||
return key1;
|
||||
}
|
||||
public int getKey2(){
|
||||
return key2;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user