Created autokey cipher

This commit is contained in:
2021-12-30 23:16:46 -05:00
parent d37b3fc35a
commit 4b3d9b3261
3 changed files with 630 additions and 1 deletions

View File

@@ -0,0 +1,106 @@
//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Autokey.java
//Matthew Ellison
// Created: 07-25-21
//Modified: 12-30--21
//This is the declaration of the Autokey class
package mattrixwv.CipherStreamJava;
public class Autokey extends Vigenere{
//Special rules for setting the strings for encoding
private void encodeSet(String key, String input) throws Exception{
//Set the input
setInputString(input);
StringBuilder newKey = new StringBuilder();
//Remove all unneccessary elements from the key
setKeyword(key);
newKey.append(keyword);
//Remove all unneccessary elements from the input
setKeyword(input);
newKey.append(getKeyword());
//Make sure the key is not any longer than the input
key = newKey.substring(0, getKeyword().length());
//Set the new keyword
setKeyword(key);
//Make sure to update the offset
offset.clear();
setOffset();
}
//Setting the strings for decoding
private void decodeSet(String key, String input) throws Exception{
//Remove all unneccessary elements from the key
setKeyword(key);
//Remove all unneccessary elements from the input
inputString = "";
setInputString(input);
}
//Decodes the inputString
protected String decode() throws Exception{
//Decode what the key will allow, add that to the key and continue
StringBuilder currentOutput = new StringBuilder();
StringBuilder fullOutput = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset
int offsetCnt = 0;
for(int letterCnt = 0;letterCnt < inputString.length();++letterCnt){
//If we have reached the end of the keyword add what we have to it and continue
if(offsetCnt == keyword.length()){
setKeyword(keyword + currentOutput.toString());
fullOutput.append(currentOutput);
currentOutput = new StringBuilder();
}
char letter = inputString.charAt(letterCnt);
if(Character.isUpperCase(letter)){
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'A'){
letter += 26;
}
else if(letter > 'Z'){
letter -= 26;
}
}
else if(Character.isLowerCase(letter)){
letter -= offset.get((offsetCnt++) % offset.size());
if(letter < 'a'){
letter += 26;
}
else if(letter > 'z'){
letter -= 26;
}
}
currentOutput.append(letter);
}
//Empty the last character that were decoded
fullOutput.append(currentOutput);
//Save and return the results
outputString = fullOutput.toString();
return outputString;
}
//Constructor
public Autokey(){
super();
}
public Autokey(boolean leaveCapitals, boolean leaveWhitespace, boolean leaveSymbols){
super(leaveCapitals, leaveWhitespace, leaveSymbols);
}
//Encodes inputString using the Autokey cipher
public String encode(String key, String input) throws Exception{
reset();
encodeSet(key, input);
return encode();
}
//Decodes inputString using the Autokey cipher
public String decode(String key, String input) throws Exception{
reset();
setInputString(input);
decodeSet(key, input);
return decode();
}
}

View File

@@ -92,7 +92,7 @@ public class Vigenere{
return outputString;
}
//Decodes inputString and stores the result in outputString
protected String decode(){
protected String decode() throws Exception{
StringBuilder output = new StringBuilder();
//Step through every character in the inputString and advance it the correct amount, according to offset