Added Vigenere cipher
This commit is contained in:
135
src/main/java/mattrixwv/CipherStreamJava/Vigenere.java
Normal file
135
src/main/java/mattrixwv/CipherStreamJava/Vigenere.java
Normal file
@@ -0,0 +1,135 @@
|
||||
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Vigenere.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 07-25-21
|
||||
//This is the declaration of the Vigenere class
|
||||
package mattrixwv.CipherStreamJava;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Vigenere{
|
||||
public static final String version = "1.0"; //The current library's version number
|
||||
private String inputString; //This is the string that needs encoded/decoded
|
||||
private String outputString; //This is the string that is output after encoding/decoding
|
||||
private String keyword; //This is the keyword that is resposible for determining the offsets that you change each character by
|
||||
private ArrayList<Integer> offset; //Holds the offsets coputed from each character in the keyword
|
||||
|
||||
//Uses keyword to calculate the offset for the Caesar cipher for each character
|
||||
private void setOffset(){
|
||||
//Reserve the correct size to increase speed later
|
||||
offset.ensureCapacity(keyword.length());
|
||||
|
||||
//Loop through every letter in keyword and get the offset from A
|
||||
for(int cnt = 0;cnt < keyword.length();++cnt){
|
||||
char letter = keyword.charAt(cnt);
|
||||
offset.add((letter - 'A') % 26);
|
||||
}
|
||||
}
|
||||
//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;
|
||||
}
|
||||
//Sets keyword
|
||||
private void setKeyword(String key) throws Exception{
|
||||
//Convert all letters to uppercase
|
||||
key = key.toUpperCase();
|
||||
//Remove all characters except capital letters
|
||||
key = key.replaceAll("[^A-Z]", "");
|
||||
//Save the string
|
||||
keyword = key;
|
||||
|
||||
//Make sure offset is empty before adding to it
|
||||
offset.clear();
|
||||
setOffset();
|
||||
//If after all the eliminating of unusable characters the keyword is empty throw an exception
|
||||
if(keyword == ""){
|
||||
throw new Exception("The keyword cannot be empty");
|
||||
}
|
||||
}
|
||||
//Encodes inputString and stores the result in outputString
|
||||
private String encode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
//Step through every character in the inputString and advance it the correct amount, according to offset
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char letter = (char)(inputString.charAt(cnt) + offset.get(cnt % offset.size()));
|
||||
//Make sure the character is still a letter, if not, wrap around
|
||||
if(letter < 'A'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
letter -= 26;
|
||||
}
|
||||
output.append(letter);
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString and stores the result in outputString
|
||||
private String decode(){
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
//Step through every character in the inputString and advance it the correct amount, according to offset
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char letter = (char)(inputString.charAt(cnt) - offset.get(cnt % offset.size()));
|
||||
if(letter < 'A'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
letter -= 26;
|
||||
}
|
||||
output.append(letter);
|
||||
}
|
||||
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Vigenere(){
|
||||
offset = new ArrayList<Integer>();
|
||||
reset();
|
||||
}
|
||||
//Returns the current inputString
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns the current outputString
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Returns the current keyword
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Returns the current offsets (Used mostly in bug fixing)
|
||||
public ArrayList<Integer> getOffsets(){
|
||||
return offset;
|
||||
}
|
||||
//Encodes input using key and returns the result
|
||||
public String encode(String key, String input) throws Exception{
|
||||
reset();
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
return encode();
|
||||
}
|
||||
//Decodes input using key and returns the result
|
||||
public String decode(String key, String input) throws Exception{
|
||||
reset();
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
return decode();
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = outputString = keyword = "";
|
||||
offset.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user