1 /*
2  * @(#) $Id: Base64Tool.java,v 1.2 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.pem;
11
12import java.util.*;
13import java.io.*;
14import java.util.logging.Logger;
15import java.security.*;
16
17
18import org.jstk.*;
19
20
21public class Base64Tool extends JSTKAbstractTool {
22    static class EncodeCommand extends JSTKCommandAdapter {
23
24        public String briefDescription(){
25            String briefDesc = "encodes input data to base64 format";
26            return briefDesc;
27        }
28
29        public String optionsDescription(){
30            String optionsDesc =
31                "  -infile <infile>  : File to be base64 encoded.\n" +
32                "  -intext <text>    : Text to be base64 encoded.\n" +
33                "  -outfile <outfile>: Output file to store base64 encoded data.\n";
34            return optionsDesc;
35        }
36        public String[] useForms(){
37            String[] useForms = {
38                "(-infile <infile> | -intext <text>) [-outfile <outfile>]"
39            };
40            return useForms;
41        }
42        public String[] sampleUses(){
43            String[] sampleUses = {
44                "-infile test.cer",
45                "-infile test.cer -outfile test.pem",
46                "-intext \"Hello, World!\""
47            };
48            return sampleUses;
49        }
50        public Object execute(JSTKArgs args) throws JSTKException{
51            try {
52                String intext = null;
53                PEMData pemData = null;
54                String msg = null;
55
56                String infile = args.get("infile");
57                String outfile = args.get("outfile");
58                if (infile == null){
59                    intext = args.get("intext");
60                    if (intext == null)
61                        return new JSTKResult(null, false, "No input data. Specify -infile  or -intext option.");
62                    pemData = new PEMData(intext.getBytes());
63                } else {
64                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(infile));
65                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
66                    byte[] buf = new byte[1024];
67                    int n;
68                    while ((n = bis.read(buf, 0, buf.length)) > 0){
69                        baos.write(buf, 0, n);
70                    }
71                    pemData = new PEMData(baos.toByteArray());
72                }
73                String text = pemData.encode();
74                if (outfile != null){
75                    FileOutputStream fos = new FileOutputStream(outfile);
76                    fos.write(text.getBytes());
77                    fos.close();
78                    msg = "base64 encoded data written to file: " + outfile;
79                } else {
80                    msg = "base64 encoded data: " + text;
81                }
82                return new JSTKResult(null, true, msg);
83            } catch (Exception exc){
84                throw new JSTKException("EncodeCommand execution failed", exc);
85            }
86        }
87    }
88
89    static class DecodeCommand extends JSTKCommandAdapter {
90        public String briefDescription(){
91            String briefDesc = "decodes base64 input data";
92            return briefDesc;
93        }
94
95        public String optionsDescription(){
96            String optionsDesc =
97                "  -infile <infile>  : File hvaing base64 encoded data.\n" +
98                "  -intext <text>    : Text in base64 format.\n" +
99                "  -outfile <outfile>: Output file to store decoded data.\n";
00            return optionsDesc;
01        }
02        public String[] useForms(){
03            String[] useForms = {
04                "(-infile <infile> | -intext <text>) [-outfile <outfile>]"
05            };
06            return useForms;
07        }
08        public String[] sampleUses(){
09            String[] sampleUses = {
10                "-infile test.pem",
11                "-infile test.pem -outfile test.cer",
12                "-intext SGVsbG8sIFdvcmxkIQ=="
13            };
14            return sampleUses;
15        }
16        public Object execute(JSTKArgs args) throws JSTKException{
17            try {
18                String intext = null;
19                PEMData pemData = null;
20                String msg = null;
21
22                String infile = args.get("infile");
23                String outfile = args.get("outfile");
24                if (infile == null){
25                    intext = args.get("intext");
26                    if (intext == null)
27                        return new JSTKResult(null, false, "No input data. Specify -infile  or -intext option.");
28                    pemData = new PEMData(intext);
29                } else {
30                    BufferedReader reader = new BufferedReader(new FileReader(infile));
31                    pemData = new PEMData(reader);
32                }
33                byte[] raw = pemData.decode();
34                if (outfile != null){
35                    FileOutputStream fos = new FileOutputStream(outfile);
36                    fos.write(raw);
37                    fos.close();
38                    msg = "decoded data written to file: " + outfile;
39                } else {
40                    msg = "decoded data: " + new String(raw);
41                }
42                return new JSTKResult(null, true, msg);
43            } catch (Exception exc){
44                throw new JSTKException("DecodeCommand execution failed", exc);
45            }
46        }
47    }
48
49    public static final Logger logger = Logger.getLogger("org.jstk.pem");
50    static {
51        cmds.put("encode", new EncodeCommand());
52        cmds.put("decode", new DecodeCommand());
53    }
54    public String progName(){
55        String progName = System.getProperty("org.jstk.pem.progname");
56        if (progName == null)
57            progName = "java org.jstk.pem.Base64Tool";
58
59        return progName;
60    }
61    public String briefDescription(){
62        return "A base64 conversion tool";
63    }
64
65
66    public static void main(String[] args) throws Exception {
67        Base64Tool b64t = new Base64Tool();
68        System.exit(b64t.execute(args));
69    }
70}