001: /*
002: This program is free software; you can redistribute it and/or modify
003: it under the terms of the latest version of the GNU Lesser General
004: Public License as published by the Free Software Foundation;
005:
006: This program is distributed in the hope that it will be useful,
007: but WITHOUT ANY WARRANTY; without even the implied warranty of
008: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
009: GNU Lesser General Public License for more details.
010:
011: You should have received a copy of the GNU Lesser General Public License
012: along with this program (gpl.txt); if not, write to the Free Software
013: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
014: */
015:
016: package vqwiki.utils;
017:
018: import javax.crypto.Cipher;
019: import javax.crypto.SecretKey;
020: import javax.crypto.SecretKeyFactory;
021: import javax.crypto.spec.DESKeySpec;
022: import org.apache.log4j.Logger;
023: import org.apache.commons.codec.binary.Base64;
024:
025: /**
026: * Provide capability for encrypting and decrypting values. Inspired by an
027: * example from http://www.devx.com/assets/sourcecode/10387.zip.
028: *
029: * @author Ryan Holliday
030: */
031: public class Encryption {
032:
033: private static Logger logger = Logger.getLogger(Encryption.class);
034: public static final String DES_ALGORITHM = "DES";
035: public static final String ENCRYPTION_KEY = "VQWiki Key 12345";
036:
037: /**
038: * Hide the constructor by making it private.
039: */
040: private Encryption() {
041: }
042:
043: /**
044: * Encrypt a String value using the DES encryption algorithm.
045: *
046: * @param unencryptedString The unencrypted String value that is to be encrypted.
047: * @return An encrypted version of the String that was passed to this method.
048: */
049: public static String encrypt(String unencryptedString)
050: throws Exception {
051: if (unencryptedString == null
052: || unencryptedString.trim().length() == 0) {
053: return unencryptedString;
054: }
055: try {
056: SecretKey key = createKey();
057: Cipher cipher = Cipher.getInstance(key.getAlgorithm());
058: cipher.init(Cipher.ENCRYPT_MODE, key);
059: byte[] unencryptedBytes = unencryptedString
060: .getBytes("UTF8");
061: byte[] encryptedBytes = Base64.encodeBase64(cipher
062: .doFinal(unencryptedBytes));
063: return bytes2String(encryptedBytes);
064: } catch (Exception e) {
065: logger.error("Encryption error while processing value '"
066: + unencryptedString + "'", e);
067: throw e;
068: }
069: }
070:
071: /**
072: * Unencrypt a String value using the DES encryption algorithm.
073: *
074: * @param encryptedString The encrypted String value that is to be unencrypted.
075: * @return An unencrypted version of the String that was passed to this method.
076: */
077: public static String decrypt(String encryptedString) {
078: if (encryptedString == null
079: || encryptedString.trim().length() <= 0) {
080: return encryptedString;
081: }
082: try {
083: SecretKey key = createKey();
084: Cipher cipher = Cipher.getInstance(key.getAlgorithm());
085: cipher.init(Cipher.DECRYPT_MODE, key);
086: byte[] encryptedBytes = encryptedString.getBytes("UTF8");
087: byte[] unencryptedBytes = cipher.doFinal(Base64
088: .decodeBase64(encryptedBytes));
089: return bytes2String(unencryptedBytes);
090: } catch (Exception e) {
091: logger.error("Decryption error while processing value '"
092: + encryptedString + "'", e);
093: // FIXME - should this throw the exception - caues issues upstream.
094: return null;
095: }
096: }
097:
098: /**
099: * Convert a byte array to a String value.
100: *
101: * @param bytes The byte array that is to be converted.
102: * @return A String value created from the byte array that was passed to this method.
103: */
104: private static String bytes2String(byte[] bytes) {
105: StringBuffer buffer = new StringBuffer();
106: for (int i = 0; i < bytes.length; i++) {
107: buffer.append((char) bytes[i]);
108: }
109: return buffer.toString();
110: }
111:
112: /**
113: * Create the encryption key value.
114: *
115: * @return An encryption key value implementing the DES encryption algorithm.
116: */
117: private static SecretKey createKey() throws Exception {
118: byte[] bytes = ENCRYPTION_KEY.getBytes("UTF8");
119: DESKeySpec spec = new DESKeySpec(bytes);
120: SecretKeyFactory keyFactory = SecretKeyFactory
121: .getInstance(DES_ALGORITHM);
122: return keyFactory.generateSecret(spec);
123: }
124: }
|