Added encoding options

This commit is contained in:
2021-12-30 18:05:24 -05:00
parent feb5047d2a
commit b79e19e59b
2 changed files with 553 additions and 52 deletions

View File

@@ -1,18 +1,20 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//Modified: 12-30-21
//This is the declaration of the Vigenere class
package mattrixwv.CipherStreamJava;
import java.util.ArrayList;
public class Vigenere{
public static final String version = "1.0"; //The current library's version number
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
protected boolean leaveCapitals; //Whether to respect capitals in the output string
protected boolean leaveWhitespace; //Whether to respect whitespace in the output string
protected boolean leaveSymbols; //Whether to respect symbols in the output string
//Uses keyword to calculate the offset for the Caesar cipher for each character
protected void setOffset(){
@@ -26,13 +28,17 @@ public class Vigenere{
}
}
//Sets inputString
protected void setInputString(String input){
//Convert all letters to uppercase
input = input.toUpperCase();
//Remove all characters except capital letters
input = input.replaceAll("[^A-Z]", "");
//Save the string
inputString = input;
protected void setInputString(String inputString){
if(!leaveCapitals){
inputString = inputString.toLowerCase();
}
if(!leaveWhitespace){
inputString = inputString.replaceAll("\\s+", "");
}
if(!leaveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z0-9\\s+]", "");
}
this.inputString = inputString;
}
//Sets keyword
protected void setKeyword(String key) throws Exception{
@@ -56,14 +62,28 @@ public class Vigenere{
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
for(int cnt = 0;cnt < inputString.length();++cnt){
char letter = (char)(inputString.charAt(cnt) + offset.get(cnt % offset.size()));
//Make sure the character is still a letter, if not, wrap around
if(letter < 'A'){
letter += 26;
int offsetCnt = 0;
for(int inputCnt = 0;inputCnt < inputString.length();++inputCnt){
char letter = inputString.charAt(inputCnt);
if(Character.isUpperCase(letter)){
letter += offset.get((offsetCnt++) % offset.size());
//Make sure the character is still a letter, if not, wrap around
if(letter < 'A'){
letter += 26;
}
else if(letter > 'Z'){
letter -= 26;
}
}
else if(letter > 'Z'){
letter -= 26;
else if(Character.isLowerCase(letter)){
letter += offset.get((offsetCnt++) % offset.size());
//Make sure the character is still a letter, if not, wrap around
if(letter < 'a'){
letter += 26;
}
else if(letter > 'z'){
letter -= 26;
}
}
output.append(letter);
}
@@ -76,13 +96,26 @@ public class Vigenere{
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
for(int cnt = 0;cnt < inputString.length();++cnt){
char letter = (char)(inputString.charAt(cnt) - offset.get(cnt % offset.size()));
if(letter < 'A'){
letter += 26;
int offsetCnt = 0;
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
char letter = inputString.charAt(letterCnt);
if(Character.isUpperCase(letter)){
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'A'){
letter += 26;
}
else if(letter > 'Z'){
letter -= 26;
}
}
else if(letter > 'Z'){
letter -= 26;
else if(Character.isLowerCase(letter)){
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'a'){
letter += 26;
}
else if(letter > 'z'){
letter -= 26;
}
}
output.append(letter);
}
@@ -96,6 +129,16 @@ public class Vigenere{
public Vigenere(){
offset = new ArrayList<Integer>();
reset();
leaveCapitals = false;
leaveWhitespace = false;
leaveSymbols = false;
}
public Vigenere(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){
offset = new ArrayList<Integer>();
reset();
this.leaveCapitals = leaveCapitals;
this.leaveWhitespace = leaveWhitespace;
this.leaveSymbols = leaveSymbols;
}
//Returns the current inputString
public String getInputString(){