001: /*
002: * WbDesCipher.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.util.StringTokenizer;
015:
016: import javax.crypto.Cipher;
017: import javax.crypto.spec.SecretKeySpec;
018:
019: import workbench.log.LogMgr;
020:
021: /**
022: * @author support@sql-workbench.net
023: */
024: public class WbDesCipher implements WbCipher {
025: private static final byte[] KEY_DATA = { -108, -50, -5, -75, -98,
026: 28, -116, 107 };
027: private static final SecretKeySpec KEY = new SecretKeySpec(
028: KEY_DATA, "DES");
029: private Cipher cipher;
030:
031: private static WbCipher instance;
032:
033: public static WbCipher getInstance() {
034: synchronized (KEY_DATA) {
035: if (instance == null) {
036: WbDesCipher wb = new WbDesCipher();
037: if (wb.cipher == null) {
038: LogMgr
039: .logWarning("WbDesCipher.getInstance()",
040: "Could not create cipher. Using NullCipher!");
041: instance = new WbNullCipher();
042: } else {
043: LogMgr.logDebug("WbDesCipher.getInstance()",
044: "WbDesCipher created");
045: instance = wb;
046: }
047: }
048: return instance;
049: }
050: }
051:
052: private WbDesCipher() {
053: try {
054: cipher = Cipher.getInstance("DES");
055: } catch (Exception e) {
056: LogMgr.logWarning("WbDesCipher.init()",
057: "No encryption available!");
058: cipher = null;
059: }
060: }
061:
062: public String decryptString(String aValue) {
063: if (aValue == null)
064: return aValue;
065: try {
066: cipher.init(Cipher.DECRYPT_MODE, KEY);
067:
068: byte[] encrypted = this .makeArray(aValue);
069: byte[] decrypted = cipher.doFinal(encrypted);
070: String result = new String(decrypted);
071: return result;
072: } catch (Exception e) {
073: e.printStackTrace();
074: return aValue;
075: }
076: }
077:
078: public String encryptString(String aValue) {
079: if (aValue == null)
080: return null;
081: if (cipher == null)
082: return aValue;
083: try {
084: cipher.init(Cipher.ENCRYPT_MODE, KEY);
085: byte[] values = aValue.getBytes();
086: byte[] encrypted = cipher.doFinal(values);
087: return this .makeString(encrypted);
088: } catch (Exception e) {
089: e.printStackTrace();
090: return aValue;
091: }
092: }
093:
094: /**
095: * Creates a String from the given array
096: * which can be used to store the array
097: * in a text file (e.g. XML)
098: *
099: * @see #makeArray(String)
100: */
101: private String makeString(byte[] values) {
102: StringBuilder buff = new StringBuilder(values.length * 3);
103: for (int i = 0; i < values.length; i++) {
104: buff.append('#');
105: buff.append(values[i]);
106: }
107: return buff.toString();
108: }
109:
110: /**
111: * Internal method which converts an "Array String" into
112: * a byte array which can be used for decoding
113: *
114: * @see #makeString(byte[])
115: */
116: private byte[] makeArray(String values) {
117: StringTokenizer tok = new StringTokenizer(values, "#");
118: byte[] result = new byte[tok.countTokens()];
119: byte b;
120: String c;
121: int i = 0;
122: while (tok.hasMoreTokens()) {
123: c = tok.nextToken();
124: try {
125: b = Byte.parseByte(c);
126: result[i] = b;
127: i++;
128: } catch (NumberFormatException e) {
129: return new byte[1];
130: }
131: }
132: return result;
133: }
134:
135: }
|