Changed binary to use any base

This commit is contained in:
2022-01-08 15:01:26 -05:00
parent f4d4585bc6
commit 21fbfd4152
3 changed files with 326 additions and 93 deletions

View File

@@ -1,4 +1,4 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Binary.java
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/BaseX.java
//Mattrixwv
// Created: 01-08-22
//Modified: 01-08-22
@@ -10,19 +10,33 @@ import java.util.StringJoiner;
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
public class Binary{
public class BaseX{
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private int base; //The base that the number will be encoded at
//Sets the input string
private void setInputStringEncode(String inputString){
this.inputString = inputString;
}
private void setInputStringDecode(String inputString) throws InvalidCharacterException{
this.inputString = inputString.replaceAll("[^01\\s]", "");
if(this.inputString != inputString){
throw new InvalidCharacterException("inputString cannot contain anything except 0's, 1's, and whitespace");
StringBuilder validNumbers = new StringBuilder();
for(int cnt = 0;cnt < base;++cnt){
validNumbers.append(Integer.toString(cnt, base).toUpperCase());
}
this.inputString = inputString.replaceAll("[^" + validNumbers.toString() + "\\s]", "");
if(this.inputString != inputString){
throw new InvalidCharacterException("inputString cannot contain anything except numbers 0-" + Integer.toString(base - 1, base) + ", and whitespace");
}
}
//Sets the numeric base
private void setBase(int base) throws Exception{
if(base <= 0){
//TODO: Change this to a custom exception. InvalidParameter?
throw new Exception("Base cannot be a negative number");
}
this.base = base;
}
//Encode inputString, store it in outputString, and return it
private String encode(){
@@ -32,11 +46,11 @@ public class Binary{
//Get the next character
char ch = inputString.charAt(cnt);
//Encode the character to binary and add it to the output
output.add(Integer.toString(ch, 2));
output.add(Integer.toString(ch, base));
}
//Save the output
outputString = output.toString();
outputString = output.toString().toUpperCase();
//Return the output
return outputString;
@@ -45,12 +59,12 @@ public class Binary{
private String decode() throws InvalidCharacterException{
//Decode every binary number in the string
StringBuilder output = new StringBuilder();
for(String binary : inputString.split(" ")){
for(String baseXString : inputString.split(" ")){
//Decode the current binary number
int num = Integer.valueOf(binary, 2);
int num = Integer.valueOf(baseXString, base);
//Make sure it is in a valid range
if((num < 0) && (num > 255)){
throw new InvalidCharacterException("The binary string '" + binary + "' is not a valid character");
throw new InvalidCharacterException("The base" + base + " string '" + baseXString + "' is not a valid character");
}
//Convert the int to a char and save it
@@ -65,8 +79,13 @@ public class Binary{
}
//Constructor
public Binary(){
public BaseX() throws Exception{
reset();
setBase(2);
}
public BaseX(int base) throws Exception{
reset();
setBase(base);
}
//Returns the inputString
public String getInputString(){
@@ -76,18 +95,34 @@ public class Binary{
public String getOutputString(){
return outputString;
}
//Returns the base
public int getBase(){
return base;
}
//Sets the inputString and encodes the message
public String encode(String inputString){
reset();
setInputStringEncode(inputString);
return encode();
}
public String encode(int base, String inputString) throws Exception{
reset();
setBase(base);
setInputStringEncode(inputString);
return encode();
}
//Sets the inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException{
reset();
setInputStringDecode(inputString);
return decode();
}
public String decode(int base, String inputString) throws Exception{
reset();
setBase(base);
setInputStringDecode(inputString);
return decode();
}
//Makes sure all of the variables are empty
public void reset(){
inputString = "";