Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
Created March 22, 2017 04:39
Show Gist options
  • Save M-ZubairAhmed/85fda5a1e403f2d1844c911400fef503 to your computer and use it in GitHub Desktop.
Save M-ZubairAhmed/85fda5a1e403f2d1844c911400fef503 to your computer and use it in GitHub Desktop.
Algorithm to encrypt text with Vigenere method
public class Main {
public static void main(String[] args) {
//Entering given text for process.
System.out.println("Enter the text to be encrypted with Vigenere method");
Scanner scanInputString = new Scanner(System.in);
String inputString = scanInputString.nextLine().toLowerCase();
//Entering key word/s.
System.out.println("Enter the encryption key");
Scanner scanInputKey = new Scanner(System.in);
String inputKey_Unfiltered = scanInputKey.nextLine().toLowerCase();
//Displaying input texts.
System.out.println("The Input text is: \t"+inputString);
System.out.println("The input key is: \t"+inputKey_Unfiltered);
scanInputString.close();
scanInputKey.close();
//Returning int array of key.
int[] keyToInt = keyChangeInt(inputKey_Unfiltered);
// Initializing int array of input text storing to ASCI from alpha.
int[] textToInt = new int[inputString.length()];
//Initializing char array for catching encrypted characters.
char[] encryptTextArr = new char[inputString.length()];
//Initializing int variable for looping through key indices.
int j = 0;
for (int i = 0 ; i<inputString.length(); i++){
//converting input text to ASCII int array.
textToInt[i] = (int)inputString.charAt(i);
if ((textToInt[i]>=97)&&(textToInt[i]<=122)){
textToInt[i] = textToInt[i] + keyToInt[j];
if (textToInt[i]>122){
textToInt[i] = textToInt[i] - 122 + 97;
}
j = keyIndexer(j,keyToInt.length);
}
//Converting encrypted ascii int array to char array of same form.
encryptTextArr[i] = (char)textToInt[i];
}
String encryptText = new String(encryptTextArr);
System.out.println("Encrypted Text is:\t" + encryptText);
}
/**
* This method converts the key to int array. After converting to Asci values
* the individual key values are brought back to range of 0 - 26 for ease.
* @param inputKey_Unfiltered The key which is input by the user.
* @return After the process (explained above), it returns the key in array form.
*/
private static int[] keyChangeInt(String inputKey_Unfiltered){
String inputKey_filtered = inputKey_Unfiltered.replaceAll("\\s|\\d","");
int[] keyToInt = new int[inputKey_filtered.length()];
for(int i=0;i<inputKey_filtered.length();i++){
keyToInt[i] = ((int)inputKey_filtered.toUpperCase().charAt(i))- 65;
}
return keyToInt;
}
/**
* This method checks and iterates the key index, so that key is always
* included for increasing the input string.
* eg. key: ab
* this method iterates as "ababababab..." upto length of the input text.
* @param keyIndex current index of the key value.
* @param keyLength total length of the key variable.
* @return It returns the appropriate index value of key.
*/
private static int keyIndexer(int keyIndex, int keyLength){
if (keyIndex == keyLength-1){
return 0;
}
else {
keyIndex++;
return keyIndex;
}
}
}
@M-ZubairAhmed
Copy link
Author

Vigenere Encryption

This method is based on encrypting alphabetic text by using a series of interwoven Caesar ciphers based on the letters of a keyword.

read full wiki article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment