Implementation of Shift Cipher

// CSci 4509 Shift Cipher implementation
// Elena Machkasova, 1/19/05

public class ShiftCipher {
    public static void main(String[] args) {
	// testing mod
	/*
	System.out.println(mod(7, 3));
	System.out.println(mod(-7, 3));
	System.out.println(mod(-101, 7));
	*/

	if (args.length < 3) {
	    System.out.println("Usage: java ShiftCipher mode key message");
	    System.out.println("mode: e for encrypt. d for decrypt");
	    System.out.println("key: an integer key <= 25");
	    System.out.println("message: the message to encrypt or decrypt");
	    System.exit(0);
	}

	int key = (new Integer(args[1])).intValue();
	if (key < 0 || key > 25) {
	    System.out.println("Illegal key value " + key);
	    System.exit(0);
	}
	
	if (args[0].equals("e") || args[0].equals("E")) {
	    // encrypting
	    System.out.println(encrypt(args[2],key));
	} else if (args[0].equals("d") || args[0].equals("D")) {
	    // decrypting
	    System.out.println(decrypt(args[2],key));
	} 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;
    }

    // assume that the key is valid
    public static String encrypt(String plaintext, int key) {
	int length = plaintext.length();
	StringBuffer ciphertext = new StringBuffer();
	for (int i = 0; i < length; ++i) {
	    char letter = plaintext.charAt(i);
	    if (!Character.isLetter(letter)) {
		System.out.println("Illegal character " + letter);
		System.exit(0);
	    }
	    int position = -1;
	    if (Character.isUpperCase(letter)) {
		position = letter - 'A';
	    }
	    else if (Character.isLowerCase(letter)) {
		position = letter - 'a';
	    }
	    int cipherposition = mod(position + key, 26);
	    //System.out.println(cipherposition);
	    ciphertext.append((char) ('A' + cipherposition));
	}
	return ciphertext.toString();
    }

        // assume that the key is valid
    public static String decrypt(String ciphertext, int key) {
	int length = ciphertext.length();
	StringBuffer plaintext = new StringBuffer();
	for (int i = 0; i < length; ++i) {
	    char letter = ciphertext.charAt(i);
	    if (!Character.isLetter(letter)) {
		System.out.println("Illegal character " + letter);
		System.exit(0);
	    }
	    int position = -1;
	    if (Character.isUpperCase(letter)) {
		position = letter - 'A';
	    }
	    else if (Character.isLowerCase(letter)) {
		position = letter - 'a';
	    }
	    int plainposition = mod(position - key, 26);
	    //System.out.println(plainposition);
	    plaintext.append((char) ('A' + plainposition));
	}
	return plaintext.toString();
    }
}

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.