Merge branch 'Matthew-Ellison/created-bifid-cipher-1646327833453' into develop
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Bifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 03-03-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||
|
||||
public class Bifid{
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
private PolybiusSquare polybiusSquare; //Used to encode the message to numbers then back into letters
|
||||
private boolean preserveCapitals; //Persist capitals in the output string
|
||||
private boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
private boolean preserveSymbols; //Persist symbols in the output string
|
||||
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Save the key for polybius to deal with
|
||||
this.keyword = keyword;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
private void formatOutput(String outputString){
|
||||
//Keep track of where you are in the output
|
||||
int outputCnt = 0;
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Check every character in the input and apply the correct rules to the output
|
||||
for(char ch : inputString.toCharArray()){
|
||||
if(Character.isUpperCase(ch)){
|
||||
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
//Save the output
|
||||
this.outputString = output.toString();
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
private void encode() throws InvalidCharacterException, InvalidInputException{
|
||||
//Get the encoded numbers from a polybius square
|
||||
String numberResult = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
|
||||
keyword = polybiusSquare.getKeyword(); //Save the cleaned keyword
|
||||
|
||||
//Split the numbers into 2 rows and rejoin the rows to create a new string
|
||||
StringBuilder row0 = new StringBuilder();
|
||||
StringBuilder row1 = new StringBuilder();
|
||||
boolean firstNum = true;
|
||||
for(char ch : numberResult.toCharArray()){
|
||||
if(firstNum){
|
||||
row0.append(ch);
|
||||
}
|
||||
else{
|
||||
row1.append(ch);
|
||||
}
|
||||
firstNum = !firstNum;
|
||||
}
|
||||
String shuffledResult = row0.toString() + row1.toString();
|
||||
|
||||
//Take the new string and decode the numbers using polybius
|
||||
String letterResult = polybiusSquare.decode(keyword, shuffledResult);
|
||||
|
||||
//Format the output
|
||||
formatOutput(letterResult);
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
private void decode() throws InvalidCharacterException, InvalidInputException{
|
||||
//Get the decoded number from a polybius square
|
||||
String numberResult = polybiusSquare.encode(keyword, inputString).replaceAll("\\s", "");
|
||||
keyword = polybiusSquare.getKeyword();
|
||||
|
||||
//Split the numbers into to rows and rejoin the rows to create a new string
|
||||
String row0 = numberResult.substring(0, numberResult.length() / 2);
|
||||
String row1 = numberResult.substring(numberResult.length() / 2);
|
||||
StringBuilder unshuffledResult = new StringBuilder();
|
||||
for(int cnt = 0;cnt < row0.length();++cnt){
|
||||
unshuffledResult.append(row0.charAt(cnt));
|
||||
unshuffledResult.append(row1.charAt(cnt));
|
||||
}
|
||||
|
||||
//Take the new string and decode the numbers using polybius
|
||||
String letterResult = polybiusSquare.decode(keyword, unshuffledResult.toString());
|
||||
|
||||
//Format the outputString
|
||||
formatOutput(letterResult);
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Bifid() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
polybiusSquare = new PolybiusSquare(false, false);
|
||||
reset();
|
||||
}
|
||||
public Bifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
polybiusSquare = new PolybiusSquare(false, false);
|
||||
reset();
|
||||
}
|
||||
//Encodes inputString using keyword and returns the result
|
||||
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Encode and return the message
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and returns the result
|
||||
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setInputString(inputString);
|
||||
|
||||
//Decode and return the message
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
}
|
||||
//Gets
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Trifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 03-03-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidBaseException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
public class Trifid{
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
private int y;
|
||||
private int z;
|
||||
public CharLocation(int x, int y, int z){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
public int getX(){
|
||||
return x;
|
||||
}
|
||||
public int getY(){
|
||||
return y;
|
||||
}
|
||||
public int getZ(){
|
||||
return z;
|
||||
}
|
||||
public String toString(){
|
||||
return "[" + (z + 1) + ", " + (x + 1) + ", " + (y + 1) + "]";
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
private int groupSize; //The size of the groups used to break up the input
|
||||
private char[][][] grid; //The grid used to encode/decode the message
|
||||
private char fillIn; //The character added to the alphabet to meet the 27 character requirement
|
||||
private boolean preserveCapitals; //Persist capitals in the output string
|
||||
private boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
private boolean preserveSymbols; //Persist symbols in the output string
|
||||
|
||||
//Makes sure the fillIn is a valid character
|
||||
private void setFillIn(char fillIn) throws InvalidCharacterException{
|
||||
//Make sure the character is a printing character
|
||||
if((fillIn < ' ') || (fillIn > '~')){
|
||||
throw new InvalidCharacterException("Fill in character must be a printing character");
|
||||
}
|
||||
//Make sure the character is not a letter
|
||||
if(Character.isAlphabetic(fillIn)){
|
||||
throw new InvalidCharacterException("Fill in must not be a letter");
|
||||
}
|
||||
|
||||
//Save the fillIn character
|
||||
this.fillIn = fillIn;
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
private void setKeyword(String keyword) throws InvalidKeywordException{
|
||||
//Ensure the keyword isn't null
|
||||
if(keyword == null){
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
//Change everything to uppercase
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Remove everything except capital letters
|
||||
keyword = keyword.replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
|
||||
//Add all letters in the alphabet and fillIn to the key
|
||||
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn;
|
||||
|
||||
//Remove all duplicate characters
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Creates the grid from the keyword
|
||||
private void createGrid(){
|
||||
for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){
|
||||
for(int rowCnt = 0;rowCnt < grid[layerCnt].length;++rowCnt){
|
||||
for(int colCnt = 0;colCnt < grid[layerCnt][rowCnt].length;++colCnt){
|
||||
int loc = 0;
|
||||
loc = colCnt + (rowCnt * grid[layerCnt][rowCnt].length) + (layerCnt * grid[layerCnt].length * grid[layerCnt][rowCnt].length);
|
||||
grid[layerCnt][rowCnt][colCnt] = keyword.charAt(loc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//Ensures groupSize constraints
|
||||
private void setGroupSize(int groupSize) throws InvalidBaseException{
|
||||
if(groupSize <= 0){
|
||||
throw new InvalidBaseException("Group size must be > 0");
|
||||
}
|
||||
|
||||
this.groupSize = groupSize;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
private void setInputString(String inputString) throws InvalidInputException{
|
||||
//Ensure the input string isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
if(Character.isWhitespace(fillIn)){
|
||||
throw new InvalidInputException("If fillIn is whitespace, whitespace must be preserved");
|
||||
}
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
inputString = inputString.replaceAll("[^a-zA-Z" + fillIn + "\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Returns the inputString with only letters
|
||||
private String getCleanInputString(){
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws InvalidCharacterException{
|
||||
for(int layer = 0;layer < grid.length;++layer){
|
||||
for(int row = 0;row < grid[layer].length;++row){
|
||||
for(int col = 0;col < grid[layer][row].length;++col){
|
||||
if(grid[layer][row][col] == letter){
|
||||
return new CharLocation(row, col, layer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If it was not found something went wrong
|
||||
throw new InvalidCharacterException("The character '" + letter + "' was not found in the grid");
|
||||
}
|
||||
//Return the character from the location provided
|
||||
private char getChar(CharLocation location) throws InvalidCharacterException{
|
||||
if(location.getX() > 2){
|
||||
throw new InvalidCharacterException("x cannot be larget than 2");
|
||||
}
|
||||
if(location.getY() > 2){
|
||||
throw new InvalidCharacterException("y cannot be larget than 2");
|
||||
}
|
||||
if(location.getZ() > 2){
|
||||
throw new InvalidCharacterException("z cannot be larget than 2");
|
||||
}
|
||||
|
||||
return grid[location.getZ()][location.getX()][location.getY()];
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
private void formatOutput(String outputString){
|
||||
//Keep track of where you are in the output
|
||||
int outputCnt = 0;
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Check every character in the input and apply the correct rules to the output
|
||||
for(char ch : inputString.toCharArray()){
|
||||
if(Character.isUpperCase(ch)){
|
||||
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
//Save the output
|
||||
this.outputString = output.toString();
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
private void encode() throws InvalidCharacterException{
|
||||
//Step through every element in the sanitized inputString encoding the letters
|
||||
ArrayList<CharLocation> locations = new ArrayList<CharLocation>();
|
||||
for(char ch : getCleanInputString().toCharArray()){
|
||||
//Get the location of the char in the grid
|
||||
CharLocation location = findChar(ch);
|
||||
locations.add(location);
|
||||
}
|
||||
|
||||
//Take the rows and split the numbers up by group
|
||||
int numGroups = inputString.length() / groupSize;
|
||||
if(numGroups == 0){
|
||||
numGroups = 1;
|
||||
}
|
||||
ArrayList<ArrayList<CharLocation>> groups = new ArrayList<ArrayList<CharLocation>>(numGroups);
|
||||
for(int cnt = 0;cnt < numGroups;++cnt){
|
||||
groups.add(new ArrayList<CharLocation>());
|
||||
}
|
||||
int groupCnt = -1;
|
||||
for(int locCnt = 0;locCnt < locations.size();++locCnt){
|
||||
if((locCnt % groupSize) == 0){
|
||||
++groupCnt;
|
||||
}
|
||||
groups.get(groupCnt).add(locations.get(locCnt));
|
||||
}
|
||||
|
||||
//Creation locations from the groups
|
||||
ArrayList<Integer> coordinates = new ArrayList<Integer>(locations.size() * 3);
|
||||
for(ArrayList<CharLocation> group : groups){
|
||||
ArrayList<Integer> layers = new ArrayList<Integer>(group.size());
|
||||
ArrayList<Integer> rows = new ArrayList<Integer>(group.size());
|
||||
ArrayList<Integer> cols = new ArrayList<Integer>(group.size());
|
||||
for(CharLocation loc : group){
|
||||
layers.add(loc.getZ());
|
||||
rows.add(loc.getX());
|
||||
cols.add(loc.getY());
|
||||
}
|
||||
coordinates.addAll(layers);
|
||||
coordinates.addAll(rows);
|
||||
coordinates.addAll(cols);
|
||||
}
|
||||
ArrayList<CharLocation> newLocations = new ArrayList<CharLocation>(locations.size());
|
||||
for(int cnt = 0;cnt < coordinates.size();){
|
||||
int z = coordinates.get(cnt++);
|
||||
int x = coordinates.get(cnt++);
|
||||
int y = coordinates.get(cnt++);
|
||||
newLocations.add(new CharLocation(x, y, z));
|
||||
}
|
||||
|
||||
//Get the new letters from the grid
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(CharLocation loc : newLocations){
|
||||
output.append(getChar(loc));
|
||||
}
|
||||
|
||||
//Format the output
|
||||
formatOutput(output.toString());
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
private void decode() throws InvalidCharacterException{
|
||||
//TODO:
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Trifid() throws InvalidCharacterException{
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
setFillIn('+');
|
||||
}
|
||||
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
setFillIn('+');
|
||||
}
|
||||
public Trifid(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols, char fillIn) throws InvalidCharacterException{
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
setFillIn(fillIn);
|
||||
}
|
||||
|
||||
//Encodes inputString using keyword and groupSize and returns the result
|
||||
public String encode(String keyword, String inputString) throws InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
return encode(keyword, inputString.length(), inputString);
|
||||
}
|
||||
public String encode(String keyword, int groupSize, String inputString) throws InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setGroupSize(groupSize);
|
||||
setInputString(inputString);
|
||||
|
||||
//Encode and return the message
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using keyword and groupSize and returns the result
|
||||
public String decode(String keyword, String inputString) throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
return decode(keyword, inputString.length(), inputString);
|
||||
}
|
||||
public String decode(String keyword, int groupSize, String inputString) throws InvalidBaseException, InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
//Set the parameters
|
||||
reset();
|
||||
setKeyword(keyword);
|
||||
setGroupSize(groupSize);
|
||||
setInputString(inputString);
|
||||
|
||||
//Decode and return the message
|
||||
decode();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
groupSize = Integer.MAX_VALUE;
|
||||
grid = new char[3][3][3];
|
||||
}
|
||||
//Gets
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
public int getGroupSize(){
|
||||
return groupSize;
|
||||
}
|
||||
public char getFillIn(){
|
||||
return fillIn;
|
||||
}
|
||||
public String getGrid(){
|
||||
StringJoiner layers = new StringJoiner("\n\n");
|
||||
|
||||
for(char[][] layer : grid){
|
||||
StringJoiner gridJoiner = new StringJoiner("\n");
|
||||
for(char[] row : layer){
|
||||
StringJoiner rowJoiner = new StringJoiner(" ", "[", "]");
|
||||
for(char ch : row){
|
||||
rowJoiner.add(Character.toString(ch));
|
||||
}
|
||||
gridJoiner.add(rowJoiner.toString());
|
||||
}
|
||||
layers.add(gridJoiner.toString());
|
||||
}
|
||||
|
||||
return layers.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestBifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 03-03-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestBifid{
|
||||
@Test
|
||||
public void testEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne kc vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne*kc+vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaokne kc^vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, true, true);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MQAOKNEKCVDODZD";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE KC VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE*KC+VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNE KC^VDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoWhitespaceEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, false, true);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaoknekcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne*kc+vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaoknekc^vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "mqaoknekcvdodzd";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaokne kc vdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "mqaoknekcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Mqaokne kcvdodzd";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceSymbolEncode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MQAOKNEKCVDODZD";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to+encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MQAOKNEKCVDODZD";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message to encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message*to+encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Message to^encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, true, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MESSAGETOENCODE";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE TO ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE*TO+ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGE TO^ENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no capital mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoWhitespaceDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, false, true);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "messagetoencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message*to+encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Messageto^encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no whitespace mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(true, true, false);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "messagetoencode";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "message to encode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "messagetoencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Message toencode";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed no symbol mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoCapitalWhitespaceSymbolDecode() throws InvalidKeywordException, InvalidInputException, InvalidCharacterException{
|
||||
Bifid cipher = new Bifid(false, false, false);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "mqaoknekcvdodzd";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "MESSAGETOENCODE";
|
||||
String output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "MQAOKNEKCVDODZD";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "mqaokne kc vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "mqaokne*kc+vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol decoding
|
||||
inputString = "Mqaokne kc^vdodzd";
|
||||
keyword = "keyword";
|
||||
correctOutput = "MESSAGETOENCODE";
|
||||
output = cipher.decode(keyword, inputString);
|
||||
assertEquals("Bifid failed secure mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/polySubstitution/TestTrifid.java
|
||||
//Mattrixwv
|
||||
// Created: 03-03-22
|
||||
//Modified: 03-03-22
|
||||
package com.mattrixwv.CipherStreamJava.polySubstitution;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidBaseException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestTrifid{
|
||||
@Test
|
||||
public void testEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{
|
||||
Trifid cipher = new Trifid(true, true, true, '+');
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "messagetoencode";
|
||||
String keyword = "keyword";
|
||||
String correctOutput = "gqdokpdodljvflf";
|
||||
String output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Trifid failed lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "MESSAGETOENCODE";
|
||||
keyword = "keyword";
|
||||
correctOutput = "GQDOKPDODLJVFLF";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Trifid failed uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test groupLength
|
||||
inputString = "messagetoencode";
|
||||
keyword = "keyword";
|
||||
int groupLength = 3;
|
||||
correctOutput = "gpjqdvdofodlklf";
|
||||
output = cipher.encode(keyword, groupLength, inputString);
|
||||
assertEquals("Trifid failed groupLength encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "message to encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "gqdokpd od ljvflf";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Trifid failed whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "message*to-encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "gqdokpd*od-ljvflf";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Trifid failed symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, symbol encoding
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
correctOutput = "Gqdokpd od^ljvflf";
|
||||
output = cipher.encode(keyword, inputString);
|
||||
assertEquals("Trifid failed mixed case, whitespace, symbol encoding.", correctOutput, output);
|
||||
//Throw in groupLength for good measure
|
||||
inputString = "Message to^encode";
|
||||
keyword = "keyword";
|
||||
groupLength = 3;
|
||||
correctOutput = "Gpjqdvd of^odlklf";
|
||||
output = cipher.encode(keyword, groupLength, inputString);
|
||||
assertEquals("Trifid failed mixed case, whitespace, symbol, groupLength encoding.", correctOutput, output);
|
||||
}
|
||||
|
||||
public void testNoCapitalEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{
|
||||
//TODO:
|
||||
}
|
||||
|
||||
public void testNoWhitespaceEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{
|
||||
//TODO:
|
||||
}
|
||||
|
||||
public void testNoSymbolEncode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{
|
||||
//TODO:
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecode() throws InvalidCharacterException, InvalidBaseException, InvalidKeywordException, InvalidInputException{
|
||||
//TODO:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user