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