Added binary encoding
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Binary.java
|
||||
//Mattrixwv
|
||||
// Created: 01-08-22
|
||||
//Modified: 01-08-22
|
||||
package com.mattrixwv.CipherStreamJava.monoSubstitution;
|
||||
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.CipherStreamJava.exceptions.InvalidCharacterException;
|
||||
|
||||
|
||||
public class Binary{
|
||||
private String inputString; //The string that needs encoded/decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
|
||||
//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");
|
||||
}
|
||||
}
|
||||
//Encode inputString, store it in outputString, and return it
|
||||
private String encode(){
|
||||
//Encode every character in inputString
|
||||
StringJoiner output = new StringJoiner(" ");
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
//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));
|
||||
}
|
||||
|
||||
//Save the output
|
||||
outputString = output.toString();
|
||||
|
||||
//Return the output
|
||||
return outputString;
|
||||
}
|
||||
//Decode inputString, store it in outputString, and return it
|
||||
private String decode() throws InvalidCharacterException{
|
||||
//Decode every binary number in the string
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(String binary : inputString.split(" ")){
|
||||
//Decode the current binary number
|
||||
int num = Integer.valueOf(binary, 2);
|
||||
//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");
|
||||
}
|
||||
|
||||
//Convert the int to a char and save it
|
||||
output.append((char)num);
|
||||
}
|
||||
|
||||
//Save the output
|
||||
outputString = output.toString();
|
||||
|
||||
//Return the output
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Constructor
|
||||
public Binary(){
|
||||
reset();
|
||||
}
|
||||
//Returns the inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns the outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Sets the inputString and encodes the message
|
||||
public String encode(String inputString){
|
||||
reset();
|
||||
setInputStringEncode(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Sets the inputString and decodes the message
|
||||
public String decode(String inputString) throws InvalidCharacterException{
|
||||
reset();
|
||||
setInputStringDecode(inputString);
|
||||
return decode();
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//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