KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > Crypto


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// 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 {
33     private static String JavaDoc m_passPhrase = null;
34
35     public static void setPassPhrase(String JavaDoc passPhrase)
36     {
37         m_passPhrase = passPhrase;
38     }
39
40     public static String JavaDoc getPassPhrase()
41     {
42         return m_passPhrase;
43     }
44
45     private Cipher m_ecipher;
46     private Cipher m_dcipher;
47     
48     private byte[] m_salt =
49     {
50         (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
51         (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
52     };
53     
54     private int m_iterationCount = 19;
55
56     public Crypto(String JavaDoc passPhrase) throws Exception JavaDoc
57     {
58         KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), m_salt, m_iterationCount);
59         SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
60         m_ecipher = Cipher.getInstance(key.getAlgorithm());
61         m_dcipher = Cipher.getInstance(key.getAlgorithm());
62         
63         AlgorithmParameterSpec paramSpec = new PBEParameterSpec(m_salt, m_iterationCount);
64         m_ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
65         m_dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
66     }
67     
68     public String JavaDoc encrypt(String JavaDoc str) throws Exception JavaDoc
69     {
70         byte[] utf8 = str.getBytes("UTF8");
71         byte[] enc = m_ecipher.doFinal(utf8);
72         return new sun.misc.BASE64Encoder().encode(enc);
73     }
74     
75     public String JavaDoc decrypt(String JavaDoc str) throws Exception JavaDoc
76     {
77         byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
78         byte[] utf8 = m_dcipher.doFinal(dec);
79         return new String JavaDoc(utf8, "UTF8");
80     }
81 }
82
Popular Tags