Changed binary to use any base
This commit is contained in:
@@ -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 = "";
|
||||
@@ -0,0 +1,280 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBaseX.java
|
||||
//Mattrixwv
|
||||
// Created: 01-08-22
|
||||
//Modified: 01-08-22
|
||||
package com.mattrixwv.CipherStreamJava.monoSubstitution;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestBaseX{
|
||||
@Test
|
||||
public void testBinaryDecode() throws InvalidCharacterException, Exception{
|
||||
BaseX cipher = new BaseX();
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "1100001";
|
||||
String correctOutput = "a";
|
||||
String output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed binary lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "1000001";
|
||||
correctOutput = "A";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed binary uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "1000001 100000 1000010 1001 1000011 1010";
|
||||
correctOutput = "A B\tC\n";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed binary whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "1000001 1000000 1000010 101101 1000011 101011";
|
||||
correctOutput = "A@B-C+";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed binary symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol decoding
|
||||
inputString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
||||
correctOutput = "A+B@C d\te\nf";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed binary mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testOctalDecode() throws InvalidCharacterException, Exception{
|
||||
BaseX cipher = new BaseX(8);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "141";
|
||||
String correctOutput = "a";
|
||||
String output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed octal lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "101";
|
||||
correctOutput = "A";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed octal uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "101 40 102 11 103 12";
|
||||
correctOutput = "A B\tC\n";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed octal whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "101 100 102 55 103 53";
|
||||
correctOutput = "A@B-C+";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed octal symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol decoding
|
||||
inputString = "101 53 102 100 103 40 144 11 145 12 146";
|
||||
correctOutput = "A+B@C d\te\nf";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed octal mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testDecimalDecode() throws InvalidCharacterException, Exception{
|
||||
BaseX cipher = new BaseX(10);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "97";
|
||||
String correctOutput = "a";
|
||||
String output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed decimal lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "65";
|
||||
correctOutput = "A";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed decimal uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "65 32 66 9 67 10";
|
||||
correctOutput = "A B\tC\n";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed decimal whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "65 64 66 45 67 43";
|
||||
correctOutput = "A@B-C+";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed decimal symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol decoding
|
||||
inputString = "65 43 66 64 67 32 100 9 101 10 102";
|
||||
correctOutput = "A+B@C d\te\nf";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed decimal mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testHexDecode() throws InvalidCharacterException, Exception{
|
||||
BaseX cipher = new BaseX(16);
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "61";
|
||||
String correctOutput = "a";
|
||||
String output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed hex lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "41";
|
||||
correctOutput = "A";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed hex uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "41 20 42 9 43 A";
|
||||
correctOutput = "A B\tC\n";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed hex whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "41 40 42 2D 43 2B";
|
||||
correctOutput = "A@B-C+";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed hex symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol decoding
|
||||
inputString = "41 2B 42 40 43 20 64 9 65 A 66";
|
||||
correctOutput = "A+B@C d\te\nf";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed hex mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testBinaryEncode() throws Exception{
|
||||
BaseX cipher = new BaseX();
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "a";
|
||||
String correctOutput = "1100001";
|
||||
String output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed binary lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "A";
|
||||
correctOutput = "1000001";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed binary uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "A B\tC\n";
|
||||
correctOutput = "1000001 100000 1000010 1001 1000011 1010";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed binary whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "A@B-C+";
|
||||
correctOutput = "1000001 1000000 1000010 101101 1000011 101011";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed binary symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol encoding
|
||||
inputString = "A+B@C d\te\nf";
|
||||
correctOutput = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed binary mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testOctalEncode() throws Exception{
|
||||
BaseX cipher = new BaseX(8);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "a";
|
||||
String correctOutput = "141";
|
||||
String output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed octal lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "A";
|
||||
correctOutput = "101";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed octal uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "A B\tC\n";
|
||||
correctOutput = "101 40 102 11 103 12";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed octal whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "A@B-C+";
|
||||
correctOutput = "101 100 102 55 103 53";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed octal symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol encoding
|
||||
inputString = "A+B@C d\te\nf";
|
||||
correctOutput = "101 53 102 100 103 40 144 11 145 12 146";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed octal mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testDecimalEncode() throws Exception{
|
||||
BaseX cipher = new BaseX(10);
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "a";
|
||||
String correctOutput = "97";
|
||||
String output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed decimal lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "A";
|
||||
correctOutput = "65";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed decimal uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "A B\tC\n";
|
||||
correctOutput = "65 32 66 9 67 10";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed decimal whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "A@B-C+";
|
||||
correctOutput = "65 64 66 45 67 43";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed decimal symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol encoding
|
||||
inputString = "A+B@C d\te\nf";
|
||||
correctOutput = "65 43 66 64 67 32 100 9 101 10 102";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed decimal mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testHexEncode() throws Exception{
|
||||
BaseX cipher = new BaseX(16);
|
||||
|
||||
//Test lowercase encoding
|
||||
String correctOutput = "61";
|
||||
String inputString = "a";
|
||||
String output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed hex lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "A";
|
||||
correctOutput = "41";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed hex uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "A B\tC\n";
|
||||
correctOutput = "41 20 42 9 43 A";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed hex whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "A@B-C+";
|
||||
correctOutput = "41 40 42 2D 43 2B";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed hex symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol encoding
|
||||
inputString = "A+B@C d\te\nf";
|
||||
correctOutput = "41 2B 42 40 43 20 64 9 65 A 66";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed hex mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/monoSubstitution/TestBinary.java
|
||||
//Mattrixwv
|
||||
// Created: 01-08-22
|
||||
//Modified: 01-08-22
|
||||
package com.mattrixwv.CipherStreamJava.monoSubstitution;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestBinary{
|
||||
@Test
|
||||
public void testDecode() throws InvalidCharacterException{
|
||||
Binary cipher = new Binary();
|
||||
|
||||
//Test lowercase decoding
|
||||
String inputString = "1100001";
|
||||
String correctOutput = "a";
|
||||
String output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed lowercase decoding.", correctOutput, output);
|
||||
//Test uppercase decoding
|
||||
inputString = "1000001";
|
||||
correctOutput = "A";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed uppercase decoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace decoding
|
||||
inputString = "1000001 100000 1000010 1001 1000011 1010";
|
||||
correctOutput = "A B\tC\n";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed whitespace decoding.", correctOutput, output);
|
||||
|
||||
//Test symbol decoding
|
||||
inputString = "1000001 1000000 1000010 101101 1000011 101011";
|
||||
correctOutput = "A@B-C+";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed symbol decoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol decoding
|
||||
inputString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
||||
correctOutput = "A+B@C d\te\nf";
|
||||
output = cipher.decode(inputString);
|
||||
assertEquals("Binary failed mixed case, whitespace, and symbol decoding.", correctOutput, output);
|
||||
}
|
||||
@Test
|
||||
public void testEncode(){
|
||||
Binary cipher = new Binary();
|
||||
|
||||
//Test lowercase encoding
|
||||
String inputString = "a";
|
||||
String correctOutput = "1100001";
|
||||
String output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed lowercase encoding.", correctOutput, output);
|
||||
//Test uppercase encoding
|
||||
inputString = "A";
|
||||
correctOutput = "1000001";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed uppercase encoding.", correctOutput, output);
|
||||
|
||||
//Test whitespace encoding
|
||||
inputString = "A B\tC\n";
|
||||
correctOutput = "1000001 100000 1000010 1001 1000011 1010";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed whitespace encoding.", correctOutput, output);
|
||||
|
||||
//Test symbol encoding
|
||||
inputString = "A@B-C+";
|
||||
correctOutput = "1000001 1000000 1000010 101101 1000011 101011";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed symbol encoding.", correctOutput, output);
|
||||
|
||||
//Test mixed case, whitespace, and symbol encoding
|
||||
inputString = "A+B@C d\te\nf";
|
||||
correctOutput = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
|
||||
output = cipher.encode(inputString);
|
||||
assertEquals("Binary failed mixed case, whitespace, and symbol encoding.", correctOutput, output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user