Vigenere Algorithm
The Vigenère cipher (French pronunciation: [viʒnɛːʁ ]) is a method of encryption alphabetic text by use a series of interwoven Caesar ciphers, based on the letters of a keyword. In 1863, Friedrich Kasiski was the first to publish a general method of deciphering Vigenère ciphers. The first well-documented description of a polyalphabetic cipher was by Leon Battista Alberti around 1467 and used a metal cipher disk to switch between cipher alphabets. The Confederate state of America, for example, used a brass cipher disk to implement the Vigenère cipher during the American Civil War. Gilbert Vernam try to repair the break cipher (make the Vernam – Vigenère cipher in 1918), but the technology he used was so cumbersome as to be impracticable.
package ciphers;
/**
* A Java implementation of Vigenere Cipher.
* @author straiffix
*/
public class Vigenere {
public static String encrypt(final String message, final String key)
{
String result = "";
for (int i = 0, j = 0; i < message.length(); i++) {
char c = message.charAt(i);
if (Character.isLetter(c)){
if(Character.isUpperCase(c)) {
result += (char) ((c + key.toUpperCase().charAt(j) - 2 * 'A') % 26 + 'A');
} else {
result += (char) ((c + key.toLowerCase().charAt(j) - 2 * 'a') % 26 + 'a');
}
} else {
result+=c;
}
j = ++j % key.length();
}
return result;
}
public static String decrypt( final String message, final String key)
{
String result ="";
for(int i = 0, j = 0; i < message.length(); i++){
char c = message.charAt(i);
if (Character.isLetter(c)){
if(Character.isUpperCase(c)) {
result += ((char)('Z'-(25-(c-key.toUpperCase().charAt(j)))%26));
} else {
result += ((char)('z'-(25-(c-key.toLowerCase().charAt(j)))%26));
}
} else {
result+=c;
}
j = ++j % key.length();
}
return result;
}
public static void main (String [] args){
String text="Hello World!";
String key="itsakey";
System.out.println(text);
String ciphertext=encrypt(text, key);
System.out.println(ciphertext);
System.out.println(decrypt(ciphertext, key));
}
}