Implementation of Vigenere Cipher

// CSci 4509 Vigenere Cipher implementation
// Elena Machkasova, 1/31/05

public class Vigenere {
    public static void main(String[] args) {

	if (args.length < 3) {
	    System.out.println("Usage: java Vigenere mode key message");
	    System.out.println("mode: e for encrypt. d for decrypt");
	    System.out.println("key: a sequence of letters, no spaces, etc.");
	    System.out.println("message: the message to encrypt or decrypt");
	    System.exit(0);
	}


	if (args[0].equals("e") || args[0].equals("E")) {
	    // encrypting
	    System.out.println(encrypt(args[2], args[1]));
	} else if (args[0].equals("d") || args[0].equals("D")) {
	    // decrypting
	    System.out.println(decrypt(args[2], args[1]));
	} else {
	    System.out.println("the first argument must be e or d");
	    System.exit(0);
	}
	
    }

    // computes n mod m defined as the smallest
    // non-negative integer r such that n = k * m + r, 
    // where k is an integer 
    // Assume that m > 0
    public static int mod(int n, int m) {
	int r = n % m;
	if (r < 0) r = r + m;
	return r;
    }

    // converts an upper or lower case letter
    // into an integer 0 <= n < = 25 which 
    // corresponds to the position of the letter in 
    // English alphabet
    public static int letterToNum(char letter) {
	int position = 0;
	if (Character.isUpperCase(letter)) {
		position = letter - 'A';
	    }
	    else if (Character.isLowerCase(letter)) {
		position = letter - 'a';
	    } else {
		System.out.println("Illegal character " + letter);
		System.exit(0);
	    }
	return position;
    }


    public static String encrypt(String plaintext, String key) {
	int messageLength = plaintext.length();
	int keyLength = key.length();
	StringBuffer ciphertext = new StringBuffer();
	int j = 0;
	for (int i = 0; i < messageLength; ++i) {
	    // convert the letter to a number 0 to 25
	    char letter = plaintext.charAt(i);
	    int position = letterToNum(letter);

	    // convert the next key letter to a number 0 to 25
	    char keyLetter = key.charAt(j);
	    int shift = letterToNum(keyLetter);
	    
	    // encrypt the letter
	    ciphertext.append((char) ('A' + mod(position + shift,26)));

	    // index of the next key letter (wraps around)
	    j = mod(j + 1, keyLength);
	}
	return ciphertext.toString();
    }

    public static String decrypt(String ciphertext, String key) {
	int messageLength = ciphertext.length();
	StringBuffer plaintext = new StringBuffer();

	int keyLength = key.length();
	int j = 0;

	for (int i = 0; i < messageLength; ++i) {
	    // convert the letter to a number 0 to 25
	    char letter = ciphertext.charAt(i);
	    int position = letterToNum(letter);

	    // convert the next key letter to a number 0 to 25
	    char keyLetter = key.charAt(j);
	    int shift = letterToNum(keyLetter);
	    
	    // encrypt the letter
	    plaintext.append((char) ('A' + mod(position - shift,26)));

	    // index of the next key letter (wraps around)
	    j = mod(j + 1, keyLength);
	}

	return plaintext.toString();
    }
}

Try encrypting this
idonotlikegreeneggsandhamidonotlikethemsamiam
using the key KEY and the key GREEN
The course main page.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.