Updated test coverage
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Affine.java
|
||||
//Mattrixwv
|
||||
// Created: 01-26-22
|
||||
//Modified: 07-09-22
|
||||
//Modified: 04-15-23
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -15,24 +15,26 @@ import com.mattrixwv.NumberAlgorithms;
|
||||
|
||||
|
||||
public class Affine{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Affine.class);
|
||||
protected static 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
|
||||
protected boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||
protected boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
protected boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
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
|
||||
|
||||
//Ensures key1 constraints
|
||||
private void setKey1(int key1) throws InvalidKeywordException{
|
||||
protected void setKey1(int key1) throws InvalidKeywordException{
|
||||
logger.debug("Setting key1 {}", key1);
|
||||
|
||||
//Mod 26 to ensure no overflow
|
||||
key1 %= 26;
|
||||
|
||||
//If the key is negative change it to possitive
|
||||
if(key1 < 0){
|
||||
key1 %= 26;
|
||||
key1 += 26;
|
||||
}
|
||||
|
||||
@@ -47,12 +49,14 @@ public class Affine{
|
||||
logger.debug("Cleaned key1 {}", key1);
|
||||
}
|
||||
//Ensures key2 constraints
|
||||
private void setKey2(int key2){
|
||||
protected void setKey2(int key2){
|
||||
logger.debug("Setting key2 {}", key2);
|
||||
|
||||
//Mod 26 to ensure no overflow
|
||||
key2 %= 26;
|
||||
|
||||
//If the key is negative change it to possitive
|
||||
if(key2 < 0){
|
||||
key2 %= 26;
|
||||
key2 += 26;
|
||||
}
|
||||
|
||||
@@ -62,7 +66,7 @@ public class Affine{
|
||||
logger.debug("Cleaned key2 {}", key2);
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
@@ -94,7 +98,7 @@ public class Affine{
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
private String encode(){
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Step through every character in the input and encode it if needed
|
||||
@@ -108,7 +112,7 @@ public class Affine{
|
||||
//Encode the number
|
||||
letter = ((key1 * letter) + key2) % 26;
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 65);
|
||||
char newChar = (char)(letter + 'A');
|
||||
output.append(newChar);
|
||||
|
||||
logger.debug("Encoded char {}", newChar);
|
||||
@@ -119,7 +123,7 @@ public class Affine{
|
||||
//Encode the number
|
||||
letter = ((key1 * letter) + key2) % 26;
|
||||
//Change the new number back to a character and append it to the output
|
||||
char newChar = (char)(letter + 97);
|
||||
char newChar = (char)(letter + 'a');
|
||||
output.append(newChar);
|
||||
|
||||
logger.debug("Encoded char {}", newChar);
|
||||
@@ -131,12 +135,11 @@ public class Affine{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Saving output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
private String decode(){
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Find the multiplicative inverse of key1
|
||||
@@ -186,9 +189,8 @@ public class Affine{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Saving output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
logger.debug("Saving output string '{}'", outputString);
|
||||
}
|
||||
|
||||
|
||||
@@ -211,14 +213,16 @@ public class Affine{
|
||||
setKey1(key1);
|
||||
setKey2(key2);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//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);
|
||||
setInputString(inputString);
|
||||
return decode();
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Returns the cleaned inputString
|
||||
|
||||
Reference in New Issue
Block a user