Completed RailFence cipher

This commit is contained in:
2022-03-22 16:07:34 +00:00
parent 9621b2178e
commit c9633d33fe

View File

@@ -1,7 +1,7 @@
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/RailFence.java //CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/RailFence.java
//Mattrixwv //Mattrixwv
// Created: 03-21-22 // Created: 03-21-22
//Modified: 03-21-22 //Modified: 03-22-22
package com.mattrixwv.CipherStreamJava.polySubstitution; package com.mattrixwv.CipherStreamJava.polySubstitution;
@@ -13,13 +13,14 @@ import com.mattrixwv.CipherStreamJava.exceptions.InvalidInputException;
public class RailFence{ public class RailFence{
//Variables //Variables
private String inputString; private String inputString; //The message that needs to be encoded/decoded
private String outputString; private String outputString; //The encoded/decoded message
private StringBuilder[] fence; private StringBuilder[] fence; //The fence used for encoding/decoding
private boolean preserveCapitals; private boolean preserveCapitals; //Persist capitals in the output string
private boolean preserveWhitespace; private boolean preserveWhitespace; //Persist whitespace in the output string
private boolean preserveSymbols; private boolean preserveSymbols; //Persist symbols in the output string
//Strips invalid characters from the string that needs encoded/decoded
private void setInputString(String inputString) throws InvalidInputException{ private void setInputString(String inputString) throws InvalidInputException{
//Ensure the input string isn't null //Ensure the input string isn't null
if(inputString == null){ if(inputString == null){
@@ -45,9 +46,10 @@ public class RailFence{
throw new InvalidInputException("Input must contain at least 1 letter"); throw new InvalidInputException("Input must contain at least 1 letter");
} }
} }
//Ensures the number of rails is valid and sets up the fence
private void setNumRails(int numRails) throws InvalidBaseException{ private void setNumRails(int numRails) throws InvalidBaseException{
if(numRails <= 0){ if(numRails < 2){
throw new InvalidBaseException(); throw new InvalidBaseException("You must use at least 2 rails");
} }
fence = new StringBuilder[numRails]; fence = new StringBuilder[numRails];
@@ -55,9 +57,11 @@ public class RailFence{
fence[cnt] = new StringBuilder(); fence[cnt] = new StringBuilder();
} }
} }
//Strip the inputString of all non-letter characters
private String getCleanInputString(){ private String getCleanInputString(){
return inputString.replaceAll("[^a-zA-Z]", ""); return inputString.replaceAll("[^a-zA-Z]", "");
} }
//Ensures capitals, lowercase, and symbols are displayed in the output string
private void formatOutput(String outputString){ private void formatOutput(String outputString){
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
int outputLoc = 0; int outputLoc = 0;
@@ -75,6 +79,7 @@ public class RailFence{
this.outputString = output.toString(); this.outputString = output.toString();
} }
//Returns the decoded string found in the fence after all characters are placed correctly
private String getDecodedStringFromFence(){ private String getDecodedStringFromFence(){
boolean down = true; boolean down = true;
int rail = 0; int rail = 0;
@@ -82,6 +87,7 @@ public class RailFence{
int insideCol = -1; int insideCol = -1;
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
while(true){ while(true){
//Get the next character based on what rail you are currently usinig
if(rail == 0){ if(rail == 0){
if(outsideCol >= fence[rail].length()){ if(outsideCol >= fence[rail].length()){
break; break;
@@ -103,6 +109,7 @@ public class RailFence{
output.append(fence[rail].charAt(insideCol)); output.append(fence[rail].charAt(insideCol));
} }
//Make sure you're still in bounds
if(down){ if(down){
++rail; ++rail;
} }
@@ -121,6 +128,7 @@ public class RailFence{
return output.toString(); return output.toString();
} }
//Encodes inputString using the RailFence cipher and stores the result in outputString
private void encode(){ private void encode(){
boolean up = true; boolean up = true;
int rail = 0; int rail = 0;
@@ -151,7 +159,9 @@ public class RailFence{
formatOutput(output.toString()); formatOutput(output.toString());
} }
//Decodes inputString using the RailFence cipher and stores the result in outputString
private void decode(){ private void decode(){
//Determine the number of characters on each rail
String cleanInputString = getCleanInputString(); String cleanInputString = getCleanInputString();
int cycleLength = 2 * (fence.length - 1); int cycleLength = 2 * (fence.length - 1);
BigDecimal k = new BigDecimal(cleanInputString.length()).divide(new BigDecimal(cycleLength)); BigDecimal k = new BigDecimal(cleanInputString.length()).divide(new BigDecimal(cycleLength));
@@ -172,6 +182,7 @@ public class RailFence{
middleNum = (cycleLength - (k.remainder(BigDecimal.ONE).multiply(new BigDecimal(cycleLength))).intValue()); middleNum = (cycleLength - (k.remainder(BigDecimal.ONE).multiply(new BigDecimal(cycleLength))).intValue());
} }
//Add the correct number of characters to each rail
fence[0].append(cleanInputString.substring(0, numInTopRail)); fence[0].append(cleanInputString.substring(0, numInTopRail));
int start = numInTopRail; int start = numInTopRail;
int end = numInTopRail + numInMiddleRails; int end = numInTopRail + numInMiddleRails;
@@ -183,8 +194,10 @@ public class RailFence{
start = end; start = end;
end += numInMiddleRails; end += numInMiddleRails;
} }
fence[fence.length - 1].append(cleanInputString.substring(start)); end = start + numInBottomRail;
fence[fence.length - 1].append(cleanInputString.substring(start, end));
//Get the decoded string from the constructed fence
String output = getDecodedStringFromFence(); String output = getDecodedStringFromFence();
formatOutput(output); formatOutput(output);
} }