| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.util; |
| 16 | |
| 17 | import java.security.spec.KeySpec; |
| 18 | |
| 19 | import javax.crypto.Cipher; |
| 20 | import javax.crypto.SecretKey; |
| 21 | import javax.crypto.SecretKeyFactory; |
| 22 | import javax.crypto.spec.PBEKeySpec; |
| 23 | import javax.crypto.spec.PBEParameterSpec; |
| 24 | |
| 25 | import org.apache.commons.codec.binary.Base64; |
| 26 | import org.apache.commons.logging.Log; |
| 27 | import org.apache.commons.logging.LogFactory; |
| 28 | import org.apache.hivemind.ApplicationRuntimeException; |
| 29 | |
| 30 | public class SymmetricEncryptor |
| 31 | { |
| 32 | static final private Log _logger = LogFactory.getLog(SymmetricEncryptor.class); |
| 33 | |
| 34 | //CSOFF: IllegalCatchCheck |
| 35 | public SymmetricEncryptor(String passPhrase) |
| 36 | { |
| 37 | try |
| 38 | { |
| 39 | // Create the key |
| 40 | KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT); |
| 41 | SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES") |
| 42 | .generateSecret(keySpec); |
| 43 | _ecipher = Cipher.getInstance(key.getAlgorithm()); |
| 44 | _dcipher = Cipher.getInstance(key.getAlgorithm()); |
| 45 | |
| 46 | // Prepare the parameter to the ciphers |
| 47 | PBEParameterSpec paramSpec = new PBEParameterSpec(SALT, ITERATION_COUNT); |
| 48 | |
| 49 | // Create the ciphers |
| 50 | _ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); |
| 51 | _dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); |
| 52 | } |
| 53 | catch (Exception e) |
| 54 | { |
| 55 | _logger.error("<init>", e); |
| 56 | throw new ApplicationRuntimeException(e); |
| 57 | } |
| 58 | } |
| 59 | //CSON: IllegalCatchCheck |
| 60 | |
| 61 | //CSOFF: IllegalCatchCheck |
| 62 | public String encrypt(String str) |
| 63 | { |
| 64 | if (str == null) |
| 65 | { |
| 66 | return null; |
| 67 | } |
| 68 | try |
| 69 | { |
| 70 | // Encode the string into bytes using utf-8 |
| 71 | byte[] utf8 = str.getBytes("UTF8"); |
| 72 | |
| 73 | // Encrypt |
| 74 | byte[] enc = _ecipher.doFinal(utf8); |
| 75 | |
| 76 | // Encode bytes to base64 to get a string |
| 77 | byte[] b64str = Base64.encodeBase64(enc); |
| 78 | return new String(b64str, "UTF8"); |
| 79 | } |
| 80 | catch (Exception e) |
| 81 | { |
| 82 | _logger.error("encrypt()", e); |
| 83 | throw new ApplicationRuntimeException(e); |
| 84 | } |
| 85 | } |
| 86 | //CSON: IllegalCatchCheck |
| 87 | |
| 88 | //CSOFF: IllegalCatchCheck |
| 89 | public String decrypt(String str) |
| 90 | { |
| 91 | if (str == null) |
| 92 | { |
| 93 | return null; |
| 94 | } |
| 95 | try |
| 96 | { |
| 97 | // Encode the string into bytes using utf-8 |
| 98 | byte[] utf8 = str.getBytes("UTF8"); |
| 99 | |
| 100 | // Decode base64 to get bytes |
| 101 | byte[] dec = Base64.decodeBase64(utf8); |
| 102 | |
| 103 | // Decrypt |
| 104 | utf8 = _dcipher.doFinal(dec); |
| 105 | |
| 106 | // Decode using utf-8 |
| 107 | return new String(utf8, "UTF8"); |
| 108 | } |
| 109 | catch (Exception e) |
| 110 | { |
| 111 | _logger.error("decrypt()", e); |
| 112 | throw new ApplicationRuntimeException(e); |
| 113 | } |
| 114 | } |
| 115 | //CSON: IllegalCatchCheck |
| 116 | |
| 117 | final private Cipher _ecipher; |
| 118 | final private Cipher _dcipher; |
| 119 | |
| 120 | static final private byte[] SALT = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, |
| 121 | (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03}; |
| 122 | static final private int ITERATION_COUNT = 19; |
| 123 | } |