Last active
March 31, 2020 14:29
-
-
Save Amejonah1200/d744bbf844648de5dee80a2c014bb260 to your computer and use it in GitHub Desktop.
A VigenereCypher in Java, handling only with Alphabetical Letters and Spaces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
public class VigenereCypher { | |
private static List<Character> alphabet = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); | |
public static void main(String[] args) { | |
System.out.println(alphabet.indexOf('X') + " " + alphabet.indexOf('K') + " " +alphabet.indexOf('H')); | |
System.out.println(encodeString("X", "K")); | |
System.out.println(decodeString("H", "K")); | |
} | |
public static String encodeString(String input, String key) { | |
// --- Init --- | |
System.out.println("Encoding..."); | |
input = cleanUpString(input.toUpperCase()); | |
key = cleanUpString(key.toUpperCase()); | |
System.out.println("Input: " + input); // Print in console for debug | |
System.out.println("Key: " + key); | |
int indexInKey = 0; | |
String output = ""; | |
// --- Encode --- | |
for (Character inputChar : input.toCharArray()) { | |
output += encodedChar(inputChar, key.charAt(indexInKey)); | |
indexInKey++; | |
if (indexInKey >= key.length()) indexInKey = 0; | |
} | |
return output; | |
} | |
public static String decodeString(String input, String key) { | |
// --- Init --- | |
System.out.println("Decoding..."); | |
input = cleanUpString(input.toUpperCase()); | |
key = cleanUpString(key.toUpperCase()); | |
System.out.println("Input: " + input); // Print in console for debug | |
System.out.println("Key: " + key); | |
int indexInKey = 0; | |
String output = ""; | |
// --- Decode --- | |
for (Character inputChar : input.toCharArray()) { | |
output += decodedChar(inputChar, key.charAt(indexInKey)); | |
indexInKey++; | |
if (indexInKey >= key.length()) indexInKey = 0; | |
} | |
return output; | |
} | |
public static char decodeChar(Character inputChar, Character key) { | |
if (Character.isSpaceChar(inputChar)) return inputChar; | |
int index = alphabet.indexOf(inputChar) - alphabet.indexOf(key); | |
if (index < 0) index += 26; | |
return alphabet.get(index % 26); | |
} | |
public static char encodeChar(Character inputChar, Character key) { | |
return Character.isSpaceChar(inputChar) ? inputChar : alphabet.get(((alphabet.indexOf(inputChar) | |
+ alphabet.indexOf(key)) % 26)); | |
} | |
private static String cleanUpString(String string) { | |
String output = ""; | |
for (char c : string.toCharArray()) | |
if (Character.isLetter(c) || Character.isSpaceChar(c)) output += c; | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment