Created Substitution cipher

This commit is contained in:
2022-02-22 22:12:51 +00:00
parent 88bdef0dc4
commit 517c580182

View File

@@ -0,0 +1,161 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Substitution.java
//Mattrixwv
// Created: 02-22-22
//Modified: 02-22-22
package com.mattrixwv.CipherStreamJava.monoSubstitution;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidKeywordException;
public class Substitution{
private String inputString;
private String outputString;
private String key;
private boolean preserveCapitals;
private boolean preserveWhitespace;
private boolean preserveSymbols;
private void setKey(String key) throws InvalidKeywordException{
if(key == null){
throw new InvalidKeywordException("Key cannot be null");
}
//Transform all letters to uppercase
key = key.toUpperCase();
//Make sure the key contains no duplicate mappings
String tempKey = key.replaceAll("(.)\\1{2}", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key cannot contain duplicate mappings");
}
//Make sure the key is a valid length
if(key.length() == 26){
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters");
}
}
else if(key.length() == 36){
//Make sure the key contains all valid characters
tempKey = key.replaceAll("[^A-Z0-9]", "");
if(!tempKey.equals(key)){
throw new InvalidKeywordException("The key must contain all letters and can contain all numbers");
}
}
else{
throw new InvalidKeywordException("The key must contain all letters and can contain all numbers");
}
//Save the key
this.key = key;
}
private void setInputString(String inputString) throws InvalidInputException{
if(inputString == null){
throw new InvalidInputException("Input cannot be null");
}
//Remove any data that should not be preserved
if(!preserveCapitals){
inputString = inputString.toUpperCase();
}
if(!preserveWhitespace){
inputString = inputString.replaceAll("\\s", "");
}
if(!preserveSymbols){
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
}
//Save the inputString
this.inputString = inputString;
//Make sure there is still input
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
private void encode(){
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and convert it
for(char ch : inputString.toCharArray()){
if(Character.isUpperCase(ch)){
output.append(Character.toUpperCase(key.charAt(ch - 'A')));
}
else if(Character.isLowerCase(ch)){
output.append(Character.toLowerCase(key.charAt(ch - 'a')));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
output.append(key.charAt('Z' - 'A' + Integer.valueOf(Character.toString(ch)) + 1));
}
else{
output.append(ch);
}
}
this.outputString = output.toString();
}
private void decode(){
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and convert it
for(char ch : inputString.toCharArray()){
if(Character.isUpperCase(ch)){
output.append((char)('A' + key.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isLowerCase(ch)){
output.append((char)('a' + key.indexOf(Character.toUpperCase(ch))));
}
else if(Character.isDigit(ch) && (key.length() == 36)){
output.append((char)('0' + (key.indexOf(Character.toUpperCase(ch)) - 26)));
}
else{
output.append(ch);
}
}
this.outputString = output.toString();
}
public Substitution(){
preserveCapitals = false;
preserveWhitespace = false;
preserveSymbols = false;
reset();
}
public Substitution(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
this.preserveCapitals = preserveCapitals;
this.preserveWhitespace = preserveWhitespace;
this.preserveSymbols = preserveSymbols;
reset();
}
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
public String getKeyword(){
return key;
}
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setInputString(inputString);
encode();
return outputString;
}
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
setKey(key);
setInputString(inputString);
decode();
return outputString;
}
public void reset(){
inputString = "";
outputString = "";
key = "";
}
}