01: import java.io.IOException;
02: 
03: /**
04:    A program to run the Caesar cipher encryptor with
05:    command line arguments.
06: */
07: public class Crypt
08: {  
09:    public static void main(String[] args)
10:    {  
11:       boolean decrypt = false;
12:       int key = DEFAULT_KEY;
13:       String inFile = null;
14:       String outFile = null;
15: 
16:       if (args.length < 2 || args.length > 4) usage();
17: 
18:       try
19:       {  
20:          for (int i = 0; i < args.length; i++)
21:          {  
22:             if (args[i].charAt(0) == '-')
23:             {  
24:                // It is a command line option
25:                char option = args[i].charAt(1);
26:                if (option == 'd')
27:                   decrypt = true;
28:                else if (option == 'k')
29:                   key = Integer.parseInt(args[i].substring(2));
30:             }
31:             else
32:             {  
33:                // It is a file name
34:                if (inFile == null)
35:                   inFile = args[i];
36:                else if (outFile == null)
37:                   outFile = args[i];
38:                else usage();
39:             }
40:          }
41:          if (decrypt) key = -key;
42:          Encryptor crypt = new Encryptor(key);
43:          crypt.encryptFile(inFile, outFile);
44:       }
45:       catch (IOException exception)
46:       {  
47:          System.out.println("Error processing file: " + exception);
48:       }
49:    }
50: 
51:    /**
52:       Prints a message describing proper usage and exits.
53:    */
54:    public static void usage()
55:    {  
56:       System.out.println
57:             ("Usage: java Crypt [-d] [-kn] infile outfile");
58:       System.exit(1);
59:    }
60: 
61:    public static final int DEFAULT_KEY = 3;
62: }