001: package org.jsqltool.utils;
002:
003: import java.util.*;
004: import javax.crypto.*;
005: import java.security.*;
006: import javax.crypto.*;
007: import javax.crypto.spec.*;
008: import java.security.*;
009: import javax.swing.JOptionPane;
010:
011: /**
012: * <p>Title: JSqlTool Project</p>
013: * <p>Description: This is a singleton class used to store application properties.
014: * </p>
015: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
016: *
017: * <p> This file is part of JSqlTool project.
018: * This library is free software; you can redistribute it and/or
019: * modify it under the terms of the (LGPL) Lesser General Public
020: * License as published by the Free Software Foundation;
021: *
022: * GNU LESSER GENERAL PUBLIC LICENSE
023: * Version 2.1, February 1999
024: *
025: * This library is distributed in the hope that it will be useful,
026: * but WITHOUT ANY WARRANTY; without even the implied warranty of
027: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
028: * Library General Public License for more details.
029: *
030: * You should have received a copy of the GNU Library General Public
031: * License along with this library; if not, write to the Free
032: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
033: *
034: * The author may be contacted at:
035: * maurocarniel@tin.it</p>
036: *
037: * @author Mauro Carniel
038: * @version 1.0
039: */
040: public class Options {
041:
042: /** unique instance of the class */
043: private static Options opt = null;
044:
045: /** date format */
046: private String dateFormat;
047:
048: /** Oracle PLAN table, used to execute query explain */
049: private String oracleExplainPlanTable;
050:
051: /** flag used to allow record updating when no pk is defined */
052: private boolean updateWhenNoPK;
053:
054: /** language id to be used inside the application */
055: private String language = null;
056:
057: /** contains internationalizaton settings */
058: private ResourceBundle resourceBundle = null;
059:
060: /** cipher object for password-based encryption */
061: private Cipher encCipher = null;
062:
063: /** cipher object for password-based decryption */
064: private Cipher decCipher = null;
065:
066: /** PBE parameter set */
067: private PBEParameterSpec pbeParamSpec = null;
068:
069: /** SecretKey object */
070: private SecretKey pbeKey = null;
071:
072: private Options() {
073: try {
074: // Salt
075: byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21,
076: (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
077: (byte) 0x99 };
078:
079: // Iteration count
080: int count = 20;
081:
082: // Create PBE parameter set
083: pbeParamSpec = new PBEParameterSpec(salt, count);
084:
085: // Prompt user for encryption password.
086: // Collect user password as char array (using the
087: // "readPasswd" method from above), and convert
088: // it into a SecretKey object, using a PBE key
089: // factory.
090: PBEKeySpec pbeKeySpec = new PBEKeySpec(new char[] { '2',
091: '1', '1', '7', '4' });
092: SecretKeyFactory keyFac = SecretKeyFactory
093: .getInstance("PBEWithMD5AndDES");
094: pbeKey = keyFac.generateSecret(pbeKeySpec);
095:
096: // get cipher object for password-based encryption
097: encCipher = Cipher.getInstance("PBEWithMD5AndDES");
098:
099: // get cipher object for password-based decryption
100: decCipher = Cipher.getInstance("PBEWithMD5AndDES");
101:
102: } catch (Throwable ex) {
103: ex.printStackTrace();
104: }
105: }
106:
107: /**
108: * @return unique instance of the class
109: */
110: public static Options getInstance() {
111: if (java.beans.Beans.isDesignTime())
112: return new Options();
113: if (opt == null)
114: opt = new Options();
115: return opt;
116: }
117:
118: /**
119: * @return date format
120: */
121: public String getDateFormat() {
122: return dateFormat;
123: }
124:
125: /**
126: * @return Oracle PLAN table, used to execute query explain
127: */
128: public final String getOracleExplainPlanTable() {
129: return oracleExplainPlanTable;
130: }
131:
132: /**
133: * @return allow record updating when no pk is defined
134: */
135: public final boolean isUpdateWhenNoPK() {
136: return updateWhenNoPK;
137: }
138:
139: /**
140: * Allow record updating when no pk is defined
141: * @param updateWhenNoPK allow record updating when no pk is defined
142: */
143: public final void setUpdateWhenNoPK(boolean updateWhenNoPK) {
144: this .updateWhenNoPK = updateWhenNoPK;
145: }
146:
147: /**
148: * Set Oracle PLAN table, used to execute query explain.
149: * @param oracleExplainPlanTable Oracle PLAN table, used to execute query explain
150: */
151: public final void setOracleExplainPlanTable(
152: String oracleExplainPlanTable) {
153: this .oracleExplainPlanTable = oracleExplainPlanTable;
154: }
155:
156: /**
157: * Set date format.
158: * @param dateFormat date format
159: */
160: public final void setDateFormat(String dateFormat) {
161: this .dateFormat = dateFormat;
162: }
163:
164: /**
165: * @return language id to be used inside the application
166: */
167: public final String getLanguage() {
168: return language;
169: }
170:
171: /**
172: * Set language id to be used inside the application.
173: * @param language language id to be used inside the application
174: */
175: public final void setLanguage(String language) {
176: this .language = language;
177: resourceBundle = ResourceBundle.getBundle(
178: "org.jsqltool.utils.Dictionary", new Locale(language));
179: }
180:
181: /**
182: * @param key key to translate
183: * @return key translation
184: */
185: public final String getResource(String key) {
186: if (java.beans.Beans.isDesignTime())
187: return key;
188: String value = null;
189: try {
190: value = resourceBundle.getString(key);
191: } catch (Exception ex) {
192: return key;
193: }
194: return value == null ? key : value;
195: }
196:
197: /**
198: * Encrypt text.
199: * @param text text to encrypt
200: * @return encrypted text
201: */
202: public final String encode(String text)
203: throws IllegalBlockSizeException, BadPaddingException,
204: InvalidKeyException, InvalidAlgorithmParameterException {
205: // initialize cipher for encryption, without supplying
206: // any parameters. Here, "myKey" is assumed to refer
207: // to an already-generated key.
208: encCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
209: byte[] cipherText = encCipher.doFinal(text.getBytes());
210: return new String(cipherText);
211: }
212:
213: /**
214: * Decrypt text.
215: * @param text text to decrypt
216: * @return decrypted text
217: */
218: public final String decode(String text)
219: throws IllegalBlockSizeException, BadPaddingException,
220: InvalidKeyException, InvalidAlgorithmParameterException {
221: // initialize cipher for decryption, without supplying
222: // any parameters. Here, "myKey" is assumed to refer
223: // to an already-generated key.
224: decCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
225: byte[] cipherText = decCipher.doFinal(text.getBytes());
226: return new String(cipherText);
227:
228: }
229:
230: /**
231: * Encrypt text.
232: * @param text text to encrypt
233: * @return encrypted text
234: */
235: public final byte[] encodeToBytes(String text)
236: throws IllegalBlockSizeException, BadPaddingException,
237: InvalidKeyException, InvalidAlgorithmParameterException {
238: encCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
239: byte[] cipherText = encCipher.doFinal(text.getBytes());
240: return cipherText;
241: }
242:
243: /**
244: * Decrypt text.
245: * @param text text to decrypt
246: * @return decrypted text
247: */
248: public final String decodeFromBytes(byte[] text)
249: throws IllegalBlockSizeException, BadPaddingException,
250: InvalidKeyException, InvalidAlgorithmParameterException {
251: // initialize cipher for decryption, without supplying
252: // any parameters. Here, "myKey" is assumed to refer
253: // to an already-generated key.
254: decCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
255: byte[] cipherText = decCipher.doFinal(text);
256: return new String(cipherText);
257: }
258:
259: }
|