Skip to content

Instantly share code, notes, and snippets.

@azizulhakim
Created March 2, 2017 20:42
Show Gist options
  • Save azizulhakim/459a491694fddcc7b6c1b74a68849515 to your computer and use it in GitHub Desktop.
Save azizulhakim/459a491694fddcc7b6c1b74a68849515 to your computer and use it in GitHub Desktop.
Vignere Cipher
import java.util.Scanner;
/*
* IsonProjects
*/
public class Vignere {
private final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private char matrix[][];
public Vignere(){
matrix = new char[26][26];
int pos = 0;
for (int i = 0; i<alphabet.length(); i++){
for (int j = 0; j < alphabet.length(); j++){
int mov = (pos + j) % alphabet.length();
matrix[i][j] = alphabet.charAt(mov);
}
pos++;
}
}
public Vignere(String alphabet){
matrix = new char[alphabet.length()][alphabet.length()];
int pos = 0;
for (int i = 0; i<alphabet.length(); i++){
for (int j = 0; j < alphabet.length(); j++){
int mov = (pos + j) % alphabet.length();
matrix[i][j] = alphabet.charAt(mov);
}
pos++;
}
}
public String encode(String key, String input){
String out = "";
String strippedInput = input.replace(" ", ""); // remove spaces
key = key.toUpperCase();
strippedInput = strippedInput.toUpperCase();
int repeat = strippedInput.length() / key.length();
String keyStr = "";
for (int i = 0; i < repeat; i++){
keyStr += key;
}
int surplass = strippedInput.length() % key.length();
keyStr += key.substring(0, surplass);
int k = 0;
for (int i=0; i<input.length(); i++){
if(input.charAt(i) == ' '){
out += ' ';
}
else{
int row = keyStr.charAt(k) - 'A';
int col = strippedInput.charAt(k) - 'A';
out += matrix[row][col];
k++;
}
}
return out;
}
public String decode(String key, String input){
String out = "";
String strippedInput = input.replace(" ", ""); // remove spaces
key = key.toUpperCase();
strippedInput = strippedInput.toUpperCase();
int repeat = strippedInput.length() / key.length();
String keyStr = "";
for (int i = 0; i < repeat; i++){
keyStr += key;
}
int surplass = strippedInput.length() % key.length();
keyStr += key.substring(0, surplass);
int k = 0;
for (int i=0; i<input.length(); i++){
if (input.charAt(i) == ' '){
out += ' ';
}
else{
int row = keyStr.charAt(k) - 'A';
int col = 0;
for (int j=0; j<matrix[i].length; j++){
if (matrix[row][j] == strippedInput.charAt(k)){
col = j;
k++;
break;
}
}
out += (char)('A' + col);
}
}
return out;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Vignere vignere = new Vignere("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
System.out.print("Enter key(without space): ");
String key = scanner.nextLine();
while(true){
System.out.print("Enter choice (0. Exit 1. Encode 2. Decode): ");
int choice = Integer.parseInt(scanner.nextLine());
if (choice == 0){
break;
}else{
if (choice == 1){
System.out.print("Enter string to encode: ");
String input = scanner.nextLine();
System.out.println("Encoded output: " + vignere.encode(key, input));
}
else{
System.out.print("Enter string to encode: ");
String input = scanner.nextLine();
System.out.println("Decoded output: " + vignere.decode(key, input));
}
}
}
System.out.println("-----EXIT-----");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment