001: package org.dbbrowser.security;
002:
003: import infrastructure.logging.Log;
004: import java.security.InvalidAlgorithmParameterException;
005: import java.security.InvalidKeyException;
006: import java.security.NoSuchAlgorithmException;
007: import java.security.spec.InvalidKeySpecException;
008: import javax.crypto.BadPaddingException;
009: import javax.crypto.Cipher;
010: import javax.crypto.IllegalBlockSizeException;
011: import javax.crypto.NoSuchPaddingException;
012: import javax.crypto.SecretKey;
013: import javax.crypto.SecretKeyFactory;
014: import javax.crypto.spec.PBEKeySpec;
015: import javax.crypto.spec.PBEParameterSpec;
016: import org.apache.axis.encoding.Base64;
017:
018: /**
019: * Class used to encrypt plaintext using asymmetric encryption
020: * @author amangat
021: *
022: */
023: public class AsymmetricEncryptionEngine {
024: /**
025: * Uses asymmetric encryption to encrypt clearText into cypherText. Base64 Encodes
026: * the cypherText and returns the base 64 encoded string
027: * @param clearText
028: * @param passPhrase
029: * @throws EncryptionEngineException - if there is an error during encryption
030: * @return
031: */
032: public static String encrypt(String clearText, String passPhrase)
033: throws EncryptionEngineException {
034: String base64EncodedCipherText = null;
035: Log.getInstance().debugMessage(
036: "Starting Asymmetric Encryption...",
037: AsymmetricEncryptionEngine.class.getName());
038: try {
039: //Use a password based encryption
040: PBEKeySpec keySpec = new PBEKeySpec(passPhrase
041: .toCharArray());
042:
043: //Get a secret key using the key spec and the secret key factory
044: //Generate a key for DES encryption and MD5 checksum using the key spec
045: SecretKeyFactory keyFactory = SecretKeyFactory
046: .getInstance("PBEWithMD5AndDES");
047: SecretKey pbeSecretKey = keyFactory.generateSecret(keySpec);
048:
049: Cipher c = Cipher.getInstance(pbeSecretKey.getAlgorithm()); //Symmetric DES encryption
050: //Init using the salt and iteration count
051: byte[] salt = new byte[] { (byte) 8, (byte) 8, (byte) 8,
052: (byte) 8, (byte) 8, (byte) 8, (byte) 8, (byte) 8 };//8 byte long salt
053: PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(
054: salt, 20);
055: c.init(Cipher.ENCRYPT_MODE, pbeSecretKey, pbeParameterSpec);
056:
057: byte[] cleartext = clearText.getBytes();
058:
059: // Encrypt the cleartext
060: byte[] ciphertextArray = c.doFinal(cleartext);
061:
062: //Print the encrypted text
063: Log.getInstance().debugMessage(
064: "Encrypted text: " + new String(ciphertextArray),
065: AsymmetricEncryptionEngine.class.getName());
066:
067: base64EncodedCipherText = Base64.encode(ciphertextArray);
068: Log.getInstance().debugMessage(
069: "base64EncodedString: " + base64EncodedCipherText,
070: AsymmetricEncryptionEngine.class.getName());
071: } catch (NoSuchAlgorithmException exc) {
072: Log.getInstance().debugMessage(exc.getMessage(),
073: AsymmetricEncryptionEngine.class.getName());
074: exc.printStackTrace();
075: throw new EncryptionEngineException(exc);
076: } catch (NoSuchPaddingException exc) {
077: Log.getInstance().debugMessage(exc.getMessage(),
078: AsymmetricEncryptionEngine.class.getName());
079: exc.printStackTrace();
080: throw new EncryptionEngineException(exc);
081: } catch (InvalidKeyException exc) {
082: Log.getInstance().debugMessage(exc.getMessage(),
083: AsymmetricEncryptionEngine.class.getName());
084: exc.printStackTrace();
085: throw new EncryptionEngineException(exc);
086: } catch (BadPaddingException exc) {
087: Log.getInstance().debugMessage(exc.getMessage(),
088: AsymmetricEncryptionEngine.class.getName());
089: exc.printStackTrace();
090: throw new EncryptionEngineException(exc);
091: } catch (IllegalBlockSizeException exc) {
092: Log.getInstance().debugMessage(exc.getMessage(),
093: AsymmetricEncryptionEngine.class.getName());
094: exc.printStackTrace();
095: throw new EncryptionEngineException(exc);
096: } catch (InvalidAlgorithmParameterException exc) {
097: Log.getInstance().debugMessage(exc.getMessage(),
098: AsymmetricEncryptionEngine.class.getName());
099: exc.printStackTrace();
100: throw new EncryptionEngineException(exc);
101: } catch (InvalidKeySpecException exc) {
102: Log.getInstance().debugMessage(exc.getMessage(),
103: AsymmetricEncryptionEngine.class.getName());
104: exc.printStackTrace();
105: throw new EncryptionEngineException(exc);
106: }
107:
108: return base64EncodedCipherText;
109: }
110:
111: /**
112: * Uses asymmetric encryption to decrypt cypherText into clearText.
113: * clearText is a base64 encoded string which is first decoded and then decrypted
114: * @param clearText
115: * @param passPhrase
116: * @throws EncryptionEngineException
117: * @return
118: */
119: public static String decrypt(String cypherTextInBase64Encoding,
120: String passPhrase) throws EncryptionEngineException {
121: String cleartext = null;
122: Log.getInstance().debugMessage(
123: "Starting Asymmetric Decryption...",
124: AsymmetricEncryptionEngine.class.getName());
125: try {
126: byte[] base64DecodedByteArray = Base64
127: .decode(cypherTextInBase64Encoding);
128: Log.getInstance().debugMessage(
129: "Base64 decoded string: "
130: + new String(base64DecodedByteArray),
131: AsymmetricEncryptionEngine.class.getName());
132:
133: PBEKeySpec keySpec = new PBEKeySpec(passPhrase
134: .toCharArray());
135:
136: //Get a secret key using the key spec and the secret key factory
137: //Generate a key for DES encryption and MD5 checksum using the key spec
138: SecretKeyFactory keyFactory = SecretKeyFactory
139: .getInstance("PBEWithMD5AndDES");
140: SecretKey pbeSecretKey = keyFactory.generateSecret(keySpec);
141: Cipher c = Cipher.getInstance(pbeSecretKey.getAlgorithm()); //Asymmetric DES encryption
142: byte[] salt = new byte[] { (byte) 8, (byte) 8, (byte) 8,
143: (byte) 8, (byte) 8, (byte) 8, (byte) 8, (byte) 8 };//8 byte long salt
144: PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(
145: salt, 20);
146:
147: c.init(Cipher.DECRYPT_MODE, pbeSecretKey, pbeParameterSpec);
148:
149: // Decrypt the ciphertext
150: byte[] recreatedClearText = c
151: .doFinal(base64DecodedByteArray);
152: cleartext = new String(recreatedClearText);
153: Log.getInstance().debugMessage(
154: "Finished recreating clearText",
155: AsymmetricEncryptionEngine.class.getName());
156: } catch (InvalidAlgorithmParameterException exc) {
157: Log.getInstance().debugMessage(exc.getMessage(),
158: AsymmetricEncryptionEngine.class.getName());
159: exc.printStackTrace();
160: throw new EncryptionEngineException(exc);
161: } catch (InvalidKeySpecException exc) {
162: Log.getInstance().debugMessage(exc.getMessage(),
163: AsymmetricEncryptionEngine.class.getName());
164: exc.printStackTrace();
165: throw new EncryptionEngineException(exc);
166: } catch (NoSuchAlgorithmException exc) {
167: Log.getInstance().debugMessage(exc.getMessage(),
168: AsymmetricEncryptionEngine.class.getName());
169: exc.printStackTrace();
170: throw new EncryptionEngineException(exc);
171: } catch (NoSuchPaddingException exc) {
172: Log.getInstance().debugMessage(exc.getMessage(),
173: AsymmetricEncryptionEngine.class.getName());
174: exc.printStackTrace();
175: throw new EncryptionEngineException(exc);
176: } catch (InvalidKeyException exc) {
177: Log.getInstance().debugMessage(exc.getMessage(),
178: AsymmetricEncryptionEngine.class.getName());
179: exc.printStackTrace();
180: throw new EncryptionEngineException(exc);
181: } catch (BadPaddingException exc) {
182: Log.getInstance().debugMessage(exc.getMessage(),
183: AsymmetricEncryptionEngine.class.getName());
184: exc.printStackTrace();
185: throw new EncryptionEngineException(exc);
186: } catch (IllegalBlockSizeException exc) {
187: Log.getInstance().debugMessage(exc.getMessage(),
188: AsymmetricEncryptionEngine.class.getName());
189: exc.printStackTrace();
190: throw new EncryptionEngineException(exc);
191: }
192:
193: return cleartext;
194: }
195: }
|