01: ///////////////////////////////////////////////////////////////////////////////
02: //
03: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
04: //
05: // All Rights Reserved
06: //
07: // This program is free software; you can redistribute it and/or modify
08: // it under the terms of the GNU General Public License and GNU Library
09: // General Public License as published by the Free Software Foundation;
10: // either version 2, or (at your option) any later version.
11: //
12: // This program is distributed in the hope that it will be useful,
13: // but WITHOUT ANY WARRANTY; without even the implied warranty of
14: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: // GNU General Public License and GNU Library General Public License
16: // for more details.
17: //
18: // You should have received a copy of the GNU General Public License
19: // and GNU Library General Public License along with this program; if
20: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21: // MA 02139, USA.
22: //
23: ///////////////////////////////////////////////////////////////////////////////
24: package org.myoodb.core;
25:
26: import java.io.*;
27: import javax.crypto.*;
28: import javax.crypto.spec.*;
29: import java.security.spec.*;
30:
31: public class Crypto {
32: private static volatile String s_passPhrase = null;
33:
34: public static void setPassPhrase(String passPhrase) {
35: s_passPhrase = passPhrase;
36: }
37:
38: public static String getPassPhrase() {
39: return s_passPhrase;
40: }
41:
42: private Cipher m_ecipher;
43: private Cipher m_dcipher;
44:
45: private byte[] m_salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8,
46: (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3,
47: (byte) 0x03 };
48:
49: private int m_iterationCount = 19;
50:
51: public Crypto(String passPhrase) throws Exception {
52: KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(),
53: m_salt, m_iterationCount);
54: SecretKey key = SecretKeyFactory
55: .getInstance("PBEWithMD5AndDES")
56: .generateSecret(keySpec);
57: m_ecipher = Cipher.getInstance(key.getAlgorithm());
58: m_dcipher = Cipher.getInstance(key.getAlgorithm());
59:
60: AlgorithmParameterSpec paramSpec = new PBEParameterSpec(m_salt,
61: m_iterationCount);
62: m_ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
63: m_dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
64: }
65:
66: public String encrypt(String str) throws Exception {
67: byte[] utf8 = str.getBytes("UTF8");
68: byte[] enc = m_ecipher.doFinal(utf8);
69:
70: org.myoodb.util.MimeBase64 mimeBase64 = new org.myoodb.util.MimeBase64();
71: mimeBase64.translate(enc);
72:
73: return new String(mimeBase64.getCharArray());
74: }
75:
76: public String decrypt(String str) throws Exception {
77: org.myoodb.util.MimeBase64 mimeBase64 = new org.myoodb.util.MimeBase64();
78: mimeBase64.translate(str.toCharArray());
79:
80: byte[] dec = mimeBase64.getByteArray();
81: byte[] utf8 = m_dcipher.doFinal(dec);
82:
83: return new String(utf8, "UTF8");
84: }
85: }
|