001: package com.sun.portal.app.blog.password;
002:
003: import com.sun.portal.app.blog.BlogPortletException;
004: import com.sun.portal.app.blog.CommandLineOptions;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileNotFoundException;
008: import java.io.FileOutputStream;
009: import java.io.IOException;
010: import java.io.InputStream;
011: import java.io.ObjectInputStream;
012: import java.io.ObjectOutputStream;
013: import java.io.UnsupportedEncodingException;
014: import java.security.InvalidKeyException;
015: import java.security.NoSuchAlgorithmException;
016: import javax.crypto.BadPaddingException;
017: import javax.crypto.Cipher;
018: import javax.crypto.IllegalBlockSizeException;
019: import javax.crypto.KeyGenerator;
020: import javax.crypto.NoSuchPaddingException;
021: import javax.crypto.SecretKey;
022: import org.apache.commons.codec.binary.Base64;
023:
024: public class Password {
025: public SecretKey generateKey() throws BlogPortletException {
026: try {
027: KeyGenerator kg = KeyGenerator.getInstance("DES");
028: kg.init(56);
029: SecretKey sk = kg.generateKey();
030:
031: return sk;
032: } catch (NoSuchAlgorithmException nsae) {
033: throw new BlogPortletException(nsae);
034: }
035: }
036:
037: public void writeKey(File f) throws BlogPortletException {
038: SecretKey sk = generateKey();
039:
040: try {
041: FileOutputStream fos = new FileOutputStream(f);
042: ObjectOutputStream oos = new ObjectOutputStream(fos);
043: oos.writeObject(sk);
044: oos.close();
045: fos.close();
046: } catch (IOException ioe) {
047: throw new BlogPortletException(ioe);
048: }
049: }
050:
051: public SecretKey readKey(File f) throws BlogPortletException {
052: if (!f.canRead()) {
053: throw new BlogPortletException("cannot read key file: " + f);
054: }
055: FileInputStream fis;
056: try {
057: fis = new FileInputStream(f);
058: } catch (FileNotFoundException fnfe) {
059: throw new BlogPortletException(fnfe);
060: }
061:
062: return readKey(fis);
063: }
064:
065: public SecretKey readKey(InputStream is)
066: throws BlogPortletException {
067: try {
068: ObjectInputStream ois = new ObjectInputStream(is);
069: SecretKey sk = (SecretKey) ois.readObject();
070: ois.close();
071:
072: return sk;
073: } catch (ClassNotFoundException cnfe) {
074: throw new BlogPortletException(cnfe);
075: } catch (IOException ioe) {
076: throw new BlogPortletException(ioe);
077: }
078: }
079:
080: public String decrypt(String s, SecretKey sk)
081: throws BlogPortletException {
082: try {
083: s = new String(Base64.decodeBase64(s.getBytes()),
084: "ISO-8859-1");
085: //s = URLDecoder.decode(s, "UTF-8");
086:
087: Cipher dciph = Cipher.getInstance("DES");
088: dciph.init(Cipher.DECRYPT_MODE, sk);
089:
090: byte[] sb = s.getBytes("ISO-8859-1");
091: byte[] db = dciph.doFinal(sb);
092: String pw = new String(db, "ISO-8859-1");
093:
094: return pw;
095: } catch (NoSuchAlgorithmException nsae) {
096: throw new BlogPortletException(nsae);
097: } catch (NoSuchPaddingException nspe) {
098: throw new BlogPortletException(nspe);
099: } catch (InvalidKeyException ike) {
100: throw new BlogPortletException(ike);
101: } catch (UnsupportedEncodingException uee) {
102: throw new BlogPortletException(uee);
103: } catch (IllegalBlockSizeException ibse) {
104: throw new BlogPortletException(ibse);
105: } catch (BadPaddingException bpe) {
106: throw new BlogPortletException(bpe);
107: }
108: }
109:
110: public String encrypt(String s, SecretKey sk)
111: throws BlogPortletException {
112: try {
113: Cipher eciph = Cipher.getInstance("DES");
114: eciph.init(Cipher.ENCRYPT_MODE, sk);
115:
116: byte[] sb = s.getBytes("ISO-8859-1");
117: byte[] eb = eciph.doFinal(sb);
118: String epw = new String(Base64.encodeBase64(eb));
119: //String epw = URLEncoder.encode(new String(eb, "ISO-8859-1"), "UTF-8");
120:
121: return epw;
122: } catch (NoSuchAlgorithmException nsae) {
123: throw new BlogPortletException(nsae);
124: } catch (NoSuchPaddingException nspe) {
125: throw new BlogPortletException(nspe);
126: } catch (InvalidKeyException ike) {
127: throw new BlogPortletException(ike);
128: } catch (UnsupportedEncodingException uee) {
129: throw new BlogPortletException(uee);
130: } catch (IllegalBlockSizeException ibse) {
131: throw new BlogPortletException(ibse);
132: } catch (BadPaddingException bpe) {
133: throw new BlogPortletException(bpe);
134: }
135: }
136:
137: public static void main(String[] args) {
138: try {
139: CommandLineOptions clops = new CommandLineOptions(args);
140: System.out.println(clops);
141: String cmd = clops.get("c");
142: Password p = new Password();
143:
144: if (cmd.equals("genkey")) {
145: File keyFile = new File(clops.get("k"));
146: p.writeKey(keyFile);
147: } else if (cmd.equals("decrypt")) {
148: File keyFile = readKeyFile(clops);
149: SecretKey sk = p.readKey(keyFile);
150: String epw = clops.get("e");
151: String dpw = p.decrypt(epw, sk);
152: System.out.println(dpw);
153: } else if (cmd.equals("encrypt")) {
154: File keyFile = readKeyFile(clops);
155: SecretKey sk = p.readKey(keyFile);
156: String dpw = clops.get("d");
157: String epw = p.encrypt(dpw, sk);
158: System.out.println(epw);
159: } else if (cmd.equals("test")) {
160: File keyFile = readKeyFile(clops);
161: SecretKey sk = p.readKey(keyFile);
162: String epw = p.encrypt("test", sk);
163: String dpw = p.decrypt(epw, sk);
164: System.out.println(dpw);
165: }
166: } catch (Throwable t) {
167: t.printStackTrace();
168: }
169: }
170:
171: private static File readKeyFile(CommandLineOptions clops) {
172: String f = clops.get("k");
173: File keyFile;
174: if (f == null) {
175: keyFile = new File("key.tmp");
176: } else {
177: keyFile = new File(f);
178: }
179:
180: return keyFile;
181: }
182: }
|