Created Bifid cipher

This commit is contained in:
2022-03-03 17:15:54 +00:00
parent e171923426
commit 9ed19039ca

View File

@@ -1,7 +1,7 @@
//MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java
//Mattrixwv
// Created: 01-16-22
//Modified: 02-17-22
//Modified: 03-03-22
package com.mattrixwv.CipherStreamJava.polySubstitution;
@@ -15,17 +15,18 @@ import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
public class Columnar{
private boolean preserveCapitals; //Whether to respect capitals in the output string
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
private boolean preserveSymbols; //Whether to respect symbols in the output string
private boolean removePadding; //Whether to respect the padding letters added to the cipher
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 String keyword; //The keyword used to create the grid
private char characterToAdd; //The character that is added to the end of a string to bring it to the correct length
private int charsAdded; //The number of characters that were added to the end of the message
private int charsAdded; //The number of characters that were added to the end of the message
private ArrayList<ArrayList<Character>> grid; //The grid used to encode/decode the message
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
private boolean removePadding; //Remove the padding letters added to the cipher
//Strip the inputString of all non-letter characters and change them to capitals
private String getCleanInputString(){
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
}
@@ -75,15 +76,17 @@ public class Columnar{
}
//Strips invalid characters from the string that needs encoded/decoded
private void setInputStringEncode(String inputString) throws InvalidInputException{
//Ensure the input isn't null
if(inputString == null){
throw new InvalidInputException("Input must not be null");
}
//Apply removal options
if(!preserveCapitals){
inputString = inputString.toUpperCase();
}
if(!preserveWhitespace){
inputString = inputString.replaceAll("[\\s]", "");
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
@@ -104,17 +107,21 @@ public class Columnar{
charsAdded = charsToAdd;
inputString = inputStringBuilder.toString();
//Save the string
this.inputString = inputString;
//Ensure the string isn't blank
if(this.inputString.isBlank() || getCleanInputString().isBlank()){
throw new InvalidInputException("Input cannot be blank");
}
}
private void setInputStringDecode(String inputString) throws InvalidInputException{
//Ensure the input isn't null
if(inputString == null){
throw new InvalidInputException("Input must not be null");
}
//Apply removal options
if(!preserveCapitals){
inputString = inputString.toUpperCase();
}
@@ -177,12 +184,13 @@ public class Columnar{
//Save the input
this.inputString = inputString;
//Ensure the string isn't blank
if(this.inputString.isBlank() || getCleanInputString().isBlank()){
throw new InvalidInputException("Input cannot be blank");
}
}
//Creates the output string from the grid
private String createOutputStringFromColumns(){
private void createOutputStringFromColumns(){
//Get the current rows of any characters that you added
ArrayList<Integer> colsAddedTo = new ArrayList<Integer>();
if(removePadding){
@@ -224,9 +232,8 @@ public class Columnar{
//Save and return the output
outputString = output.toString();
return outputString;
}
private String createOutputStringFromRows(){
private void createOutputStringFromRows(){
//Turn the grid into a string
StringBuilder gridOutput = new StringBuilder();
for(int row = 1;row < grid.size();++row){
@@ -282,14 +289,15 @@ public class Columnar{
//Save and return the output
outputString = output.toString();
return outputString;
}
//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 NullPointerException("Keyword cannot be null");
}
//Strip all non-letter characters and change them to uppercase
this.keyword = keyword.toUpperCase().replaceAll("[^A-Z]", "");
//Make sure a valid keyword is present
@@ -361,27 +369,28 @@ public class Columnar{
grid = newGrid;
}
//Encodes inputString using the Columnar cipher and stores the result in outputString
private String encode(){
private void encode(){
//Create the grid
createGridEncode();
//Figure out the new column order
ArrayList<Integer> orderedLocations = getKeywordAlphaLocations();
//Rearange the grid to the new order
rearangeGrid(orderedLocations);
//Create and return the output
return createOutputStringFromColumns();
//Create the output
createOutputStringFromColumns();
}
//Decodes inputString using the Columnar cipher and stores the result in outputString
private String decode(){
private void decode(){
//Create the grid
createGridDecode();
ArrayList<Integer> originalOrder = getKeywordOriginalLocations();
//Rearange the grid to the original order
rearangeGrid(originalOrder);
//Create and return the output
return createOutputStringFromRows();
//Create the output
createOutputStringFromRows();
}
//Constructor
public Columnar() throws InvalidCharacterException{
preserveCapitals = false;
preserveWhitespace = false;
@@ -406,17 +415,27 @@ public class Columnar{
setCharacterToAdd(characterToAdd);
reset();
}
//Sets the keyword, grid, and inputString and encodes the message
//Encodes inputString using keyword and returns the result
public String encode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
reset();
setKeyword(keyword);
setInputStringEncode(inputString);
return encode();
//Encode and return the message
encode();
return outputString;
}
//Sets the keyword, grid, and inputString and decodes the message
//Encodes inputString using keyword and returns the result
public String decode(String keyword, String inputString) throws InvalidKeywordException, InvalidInputException{
//Set the parameters
reset();
setKeyword(keyword);
setInputStringDecode(inputString);
return decode();
//Decode and return the message
decode();
return outputString;
}
//Makes sure all variables are empty