Updated more tests
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Hill.java
|
||||
//Mattrixwv
|
||||
// Created: 01-31-22
|
||||
//Modified: 02-17-22
|
||||
//Modified: 04-27-23
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeyException;
|
||||
@@ -16,28 +19,34 @@ import com.mattrixwv.matrix.exceptions.InvalidScalarException;
|
||||
|
||||
|
||||
public class Hill{
|
||||
private boolean preserveCapitals;
|
||||
private boolean preserveWhitespace;
|
||||
private boolean preserveSymbols;
|
||||
private boolean removePadding;
|
||||
private String inputString;
|
||||
private String outputString;
|
||||
private char characterToAdd;
|
||||
private int charsAdded;
|
||||
private ModMatrix key;
|
||||
protected static Logger logger = LoggerFactory.getLogger(Hill.class);
|
||||
|
||||
|
||||
protected boolean preserveCapitals;
|
||||
protected boolean preserveWhitespace;
|
||||
protected boolean preserveSymbols;
|
||||
protected String inputString;
|
||||
protected String outputString;
|
||||
protected char characterToAdd;
|
||||
protected ModMatrix key;
|
||||
|
||||
protected void setKey(ModMatrix key) throws InvalidKeyException{
|
||||
logger.debug("Setting key");
|
||||
|
||||
private void setKey(ModMatrix key) throws InvalidKeyException{
|
||||
//Make sure the mod is correct
|
||||
logger.debug("Testing mod");
|
||||
if(key.getMod() != 26){
|
||||
throw new InvalidKeyException("This algorithm uses the english alphabet, so the mod for the key must be 26");
|
||||
}
|
||||
|
||||
//Make sure the matrix is square
|
||||
logger.debug("Testing square");
|
||||
if(!key.isSquare()){
|
||||
throw new InvalidKeyException("The key must be a square matrix");
|
||||
}
|
||||
|
||||
//Make sure the matrix is invertable
|
||||
logger.debug("Testing invertable");
|
||||
try{
|
||||
key.inverse();
|
||||
}
|
||||
@@ -46,25 +55,37 @@ public class Hill{
|
||||
}
|
||||
|
||||
//Set the key
|
||||
logger.debug("key = {}", key);
|
||||
this.key = new ModMatrix(key);
|
||||
}
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
//Remove anything that needs removed
|
||||
protected void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for encoding");
|
||||
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string '{}'", inputString);
|
||||
|
||||
//Remove anything that needs removed
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing capitals");
|
||||
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
inputString = inputString.replaceAll("[\\s]", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Make sure the input is correct length
|
||||
logger.debug("Checking length");
|
||||
this.inputString = inputString;
|
||||
int cleanLength = getCleanInputString().length();
|
||||
int charsToAdd = (cleanLength % key.getNumRows());
|
||||
@@ -73,12 +94,13 @@ public class Hill{
|
||||
if(charsToAdd != 0){
|
||||
charsToAdd = key.getNumRows() - charsToAdd;
|
||||
}
|
||||
logger.debug("Adding {} characters", charsToAdd);
|
||||
for(int cnt = 0;cnt < charsToAdd;++cnt){
|
||||
inputStringBuilder.append(characterToAdd);
|
||||
}
|
||||
charsAdded = charsToAdd;
|
||||
inputString = inputStringBuilder.toString();
|
||||
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
//Make sure the input isn't blank
|
||||
@@ -86,10 +108,51 @@ public class Hill{
|
||||
throw new InvalidInputException("Input cannot be blank");
|
||||
}
|
||||
}
|
||||
private String getCleanInputString(){
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
protected void setInputStringDecode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for decoding");
|
||||
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string '{}'", inputString);
|
||||
|
||||
//Remove anything that needs removed
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing capitals");
|
||||
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
inputString = inputString.replaceAll("[\\s]", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
logger.debug("Checking length");
|
||||
if(getCleanInputString().isBlank() || ((getCleanInputString().length() % key.getNumRows()) != 0)){
|
||||
throw new InvalidInputException("Length of input string must be a multiple of the number of rows in the key");
|
||||
}
|
||||
}
|
||||
private void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
|
||||
protected String getCleanInputString(){
|
||||
logger.debug("Cleaning inputString");
|
||||
|
||||
String cleanInputString = inputString.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
logger.debug("Clean input string '{}'", cleanInputString);
|
||||
|
||||
return cleanInputString;
|
||||
}
|
||||
protected void setCharacterToAdd(char characterToAdd) throws InvalidCharacterException{
|
||||
logger.debug("Setting character to add {}", characterToAdd);
|
||||
|
||||
//Make sure the character is a letter
|
||||
if(!Character.isAlphabetic(characterToAdd)){
|
||||
throw new InvalidCharacterException("Character to add must be a letter");
|
||||
@@ -97,37 +160,45 @@ public class Hill{
|
||||
|
||||
//Save the characterToAdd
|
||||
if(!preserveCapitals){
|
||||
this.characterToAdd = Character.toUpperCase(characterToAdd);
|
||||
}
|
||||
else{
|
||||
this.characterToAdd = characterToAdd;
|
||||
logger.debug("Removing capitals");
|
||||
|
||||
characterToAdd = Character.toUpperCase(characterToAdd);
|
||||
}
|
||||
|
||||
logger.debug("Cleaned character {}", characterToAdd);
|
||||
this.characterToAdd = characterToAdd;
|
||||
}
|
||||
private String polishOutputString(){
|
||||
protected String polishOutputString(){
|
||||
logger.debug("Polishing output string");
|
||||
|
||||
//Add the extra characters back to the output and remove the added characters
|
||||
int outputCnt = 0;
|
||||
StringBuilder outputBuilder = new StringBuilder();
|
||||
for(char ch : inputString.toCharArray()){
|
||||
logger.debug("Current char {}", ch);
|
||||
if(Character.isUpperCase(ch)){
|
||||
logger.debug("Uppercase");
|
||||
|
||||
outputBuilder.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
logger.debug("Lowercase");
|
||||
|
||||
outputBuilder.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
logger.debug("Symbol");
|
||||
|
||||
outputBuilder.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
if(removePadding){
|
||||
String tempString = outputBuilder.substring(0, outputBuilder.length() - charsAdded);
|
||||
outputBuilder = new StringBuilder();
|
||||
outputBuilder.append(tempString);
|
||||
}
|
||||
|
||||
logger.debug("Polished string '{}'", outputBuilder.toString());
|
||||
return outputBuilder.toString();
|
||||
}
|
||||
private ArrayList<ModMatrix> getInputVectors(){
|
||||
protected ArrayList<ModMatrix> getInputVectors(){
|
||||
logger.debug("Generating input vectors");
|
||||
|
||||
//Get the number of columns in the key
|
||||
int numCols = key.getNumCols();
|
||||
|
||||
@@ -139,6 +210,7 @@ public class Hill{
|
||||
for(int cnt = 0;cnt < cleanInput.length();cnt += numCols){
|
||||
String subString = cleanInput.substring(cnt, cnt + numCols);
|
||||
int[] grid = new int[numCols];
|
||||
logger.debug("Current substring '{}'", subString);
|
||||
|
||||
//Subtract 65 from each character so that A=0, B=1, ...
|
||||
for(int subCnt = 0;subCnt < subString.length();++subCnt){
|
||||
@@ -148,6 +220,7 @@ public class Hill{
|
||||
//Create a vector from the new values
|
||||
ModMatrix vector = new ModMatrix(26);
|
||||
vector.addCol(grid);
|
||||
logger.debug("Current vector {}", vector);
|
||||
|
||||
//Add the vector to the array
|
||||
vectors.add(vector);
|
||||
@@ -156,10 +229,14 @@ public class Hill{
|
||||
//Return the array of vectors
|
||||
return vectors;
|
||||
}
|
||||
private String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){
|
||||
protected String getOutputFromVectors(ArrayList<ModMatrix> outputVectors){
|
||||
logger.debug("Turning vectors into a string");
|
||||
|
||||
//Go through each element in the vector
|
||||
StringBuilder outputBuilder = new StringBuilder();
|
||||
for(ModMatrix vector : outputVectors){
|
||||
logger.debug("Current vector {}", vector);
|
||||
|
||||
//Add 65 to each element and add it to the string
|
||||
for(int cnt = 0;cnt < vector.getNumRows();++cnt){
|
||||
outputBuilder.append((char)(vector.get(cnt, 0) + 65));
|
||||
@@ -167,16 +244,24 @@ public class Hill{
|
||||
}
|
||||
|
||||
//Return the new string
|
||||
return outputBuilder.toString();
|
||||
String convertedString = outputBuilder.toString();
|
||||
logger.debug("Converted string '{}'", convertedString);
|
||||
return convertedString;
|
||||
}
|
||||
private String encode(){
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Get an array of vectors that we are going to encode
|
||||
ArrayList<ModMatrix> inputVectors = getInputVectors();
|
||||
|
||||
//Multiply the key by each vector and add the result to a new vector
|
||||
logger.debug("Multiplying vectors");
|
||||
ArrayList<ModMatrix> outputVectors = new ArrayList<>();
|
||||
for(ModMatrix inputVector : inputVectors){
|
||||
logger.debug("Current input vector {}", inputVector);
|
||||
ModMatrix outputVector = key.multiply(inputVector);
|
||||
|
||||
logger.debug("Multiplied vector {}", outputVector);
|
||||
outputVectors.add(outputVector);
|
||||
}
|
||||
|
||||
@@ -185,19 +270,23 @@ public class Hill{
|
||||
|
||||
//Add the extra characters back to the output and remove the added characters
|
||||
outputString = polishOutputString();
|
||||
|
||||
//Save the output
|
||||
return outputString;
|
||||
}
|
||||
private String decode(){
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Get the array of vectors that we are going to decode
|
||||
ArrayList<ModMatrix> inputVectors = getInputVectors();
|
||||
|
||||
//Multiply the inverse of the key by each vector and add the result to a new vector
|
||||
logger.debug("Getting inverse of key");
|
||||
ModMatrix inverseKey = key.inverse();
|
||||
logger.debug("Inverse of key {}", inverseKey);
|
||||
ArrayList<ModMatrix> outputVectors = new ArrayList<>();
|
||||
for(ModMatrix inputVector : inputVectors){
|
||||
logger.debug("Current input vector {}", inputVector);
|
||||
ModMatrix outputVector = inverseKey.multiply(inputVector);
|
||||
|
||||
logger.debug("Multiplied vector {}", outputVector);
|
||||
outputVectors.add(outputVector);
|
||||
}
|
||||
|
||||
@@ -206,32 +295,26 @@ public class Hill{
|
||||
|
||||
//Add the extra characters back to the output and remove the added characters
|
||||
outputString = polishOutputString();
|
||||
|
||||
//Save the output
|
||||
return outputString;
|
||||
}
|
||||
|
||||
public Hill() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
removePadding = false;
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding) throws InvalidCharacterException{
|
||||
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
this.removePadding = removePadding;
|
||||
setCharacterToAdd('x');
|
||||
reset();
|
||||
}
|
||||
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, boolean removePadding, char characterToAdd) throws InvalidCharacterException{
|
||||
public Hill(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char characterToAdd) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
this.removePadding = removePadding;
|
||||
setCharacterToAdd(characterToAdd);
|
||||
reset();
|
||||
}
|
||||
@@ -241,22 +324,25 @@ public class Hill{
|
||||
}
|
||||
public String encode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
|
||||
setKey(key);
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
setInputStringEncode(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
public String decode(int[][] key, String inputString) throws InvalidKeyException, InvalidInputException{
|
||||
return decode(new ModMatrix(key, 26), inputString);
|
||||
}
|
||||
public String decode(ModMatrix key, String inputString) throws InvalidKeyException, InvalidInputException{
|
||||
setKey(key);
|
||||
setInputString(inputString);
|
||||
return decode();
|
||||
setInputStringDecode(inputString);
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
key = new ModMatrix(26);
|
||||
charsAdded = 0;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
|
||||
Reference in New Issue
Block a user