Atbash cipher implemented

This commit is contained in:
2021-07-25 16:59:48 -04:00
parent 23c58a6fdc
commit 300a3acd06
2 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Atbash.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//This is the declaration of the Atbash class
package mattrixwv.CipherStreamJava;
public class Atbash{
public static final String version = "1.0";
private String inputString; //Holds the string that needs encoded or decoded
private String outputString; //Holds teh current version of the library
//Decodes inputString and stores in outputString
private String decode(){
//Stop through every element in the inputString and shift it the correct amount
for(int cnt = 0;cnt < inputString.length();++cnt){
outputString += (char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A')));
}
return outputString;
}
//Encodes inputString and stores in outputString
private String encode(){
//Step through every element in the inputString and shift it the correct amount
for(int cnt = 0;cnt < inputString.length();++cnt){
outputString += (char)(inputString.charAt(cnt) + 25 - (2 * (inputString.charAt(cnt) - 'A')));
}
return outputString;
}
//Removes all invalid characters and sets inputString
private void setInputString(String input){
//Convert all letters to uppercase
input = input.toUpperCase();
//Remove all characters except capital letters
input = input.replaceAll("[^A-Z]", "");
//Save the string
inputString = input;
}
public String getInputString(){
return inputString;
}
public String getOutputString(){
return outputString;
}
public String encode(String input){
//Make sure everything is empty before you begin
reset();
setInputString(input);
return encode();
}
public String decode(String input){
//Make sure everything is empty before you begin
reset();
setInputString(input);
return decode();
}
public void reset(){
inputString = outputString = "";
}
}

View File

@@ -0,0 +1,47 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestAtbash.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 07-25-21
//These are the tests for the Atbash class
package mattrixwv.CipherStreamJava;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestAtbash{
@Test
public void TestDecode(){
Atbash cipher = new Atbash();
//Test 1
String input = "zyx";
String correctOutput = "ABC";
String output = cipher.decode(input);
assertEquals("Atbash Decoding failed the first test", correctOutput, output);
//Test 2
input = "GSV JFRXP YILDM ULC QFNKH LEVI - GSV OZAB WLT";
correctOutput = "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG";
output = cipher.decode(input);
assertEquals("Atbash Decoding failed the second test", correctOutput, output);
}
@Test
public void TestEncode(){
Atbash cipher = new Atbash();
//Test 1
String input = "abc";
String correctOutput = "ZYX";
String output = cipher.encode(input);
assertEquals("Atbash Encoding failed the first test", correctOutput, output);
//Test 2
input = "The quick brown fox jumps over - the lazy dog";
correctOutput = "GSVJFRXPYILDMULCQFNKHLEVIGSVOZABWLT";
output = cipher.encode(input);
assertEquals("Atbash Encoding failed the second test", correctOutput, output);
}
}