Added logging
This commit is contained in:
@@ -1,47 +1,65 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/Autokey.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 07-03-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;
|
||||
|
||||
|
||||
public class Autokey extends Vigenere{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Autokey.class);
|
||||
|
||||
//Special rules for setting the strings for encoding
|
||||
private void encodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Setting fields for encoding");
|
||||
|
||||
//Set the input
|
||||
setInputString(inputString);
|
||||
|
||||
StringBuilder newKey = new StringBuilder();
|
||||
//Remove all unneccessary elements from the key
|
||||
logger.debug("Setting keyword");
|
||||
setKeyword(keyword);
|
||||
newKey.append(keyword);
|
||||
//Remove all unneccessary elements from the input
|
||||
logger.debug("Adding input to keyword");
|
||||
setKeyword(inputString);
|
||||
newKey.append(getKeyword());
|
||||
|
||||
//Make sure the key is not any longer than the input
|
||||
logger.debug("Removing last letters in the keyword");
|
||||
keyword = newKey.substring(0, getKeyword().length());
|
||||
|
||||
//Set the new keyword
|
||||
setKeyword(keyword);
|
||||
|
||||
//Make sure to update the offset
|
||||
offset.clear();
|
||||
setOffset();
|
||||
}
|
||||
//Setting the strings for decoding
|
||||
private void decodeSet(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
logger.debug("Setting fields for decoding");
|
||||
|
||||
//Remove all unneccessary elements from the key
|
||||
logger.debug("Setting keyword");
|
||||
setKeyword(keyword);
|
||||
|
||||
//Remove all unneccessary elements from the input
|
||||
logger.debug("Setting input string");
|
||||
setInputString(inputString);
|
||||
}
|
||||
//Decodes the inputString
|
||||
@Override
|
||||
protected String decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Decode what the key will allow, add that to the key and continue
|
||||
StringBuilder currentOutput = new StringBuilder();
|
||||
StringBuilder fullOutput = new StringBuilder();
|
||||
@@ -51,36 +69,55 @@ public class Autokey extends Vigenere{
|
||||
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
|
||||
//If we have reached the end of the keyword add what we have to it and continue
|
||||
if(offsetCnt == keyword.length()){
|
||||
logger.debug("Appending partial output to keyword");
|
||||
|
||||
setKeyword(keyword + currentOutput.toString());
|
||||
fullOutput.append(currentOutput);
|
||||
currentOutput = new StringBuilder();
|
||||
}
|
||||
|
||||
char letter = inputString.charAt(letterCnt);
|
||||
logger.debug("Working character {}", letter);
|
||||
|
||||
if(Character.isUpperCase(letter)){
|
||||
logger.debug("Appending uppercase");
|
||||
|
||||
letter -= offset.get((offsetCnt++) % offset.size());
|
||||
if(letter < 'A'){
|
||||
logger.debug("Wrapping around to Z");
|
||||
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
logger.debug("Wrapping around to A");
|
||||
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
else if(Character.isLowerCase(letter)){
|
||||
logger.debug("Appending lowercase");
|
||||
|
||||
letter -= offset.get((offsetCnt++) % offset.size());
|
||||
if(letter < 'a'){
|
||||
logger.debug("Wrapping around to z");
|
||||
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'z'){
|
||||
logger.debug("Wrapping around to a");
|
||||
|
||||
letter -= 26;
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Decoded letter {}", letter);
|
||||
currentOutput.append(letter);
|
||||
}
|
||||
//Empty the last character that were decoded
|
||||
fullOutput.append(currentOutput);
|
||||
|
||||
//Save and return the results
|
||||
logger.debug("Saving output string '{}'", fullOutput);
|
||||
outputString = fullOutput.toString();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user