KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > crypto > DesCrypt


1 /*
2  * $Id: DesCrypt.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.crypto;
26
27 import java.security.NoSuchAlgorithmException JavaDoc;
28 import java.security.InvalidKeyException JavaDoc;
29 import java.security.InvalidAlgorithmParameterException JavaDoc;
30 import java.security.spec.InvalidKeySpecException JavaDoc;
31 import javax.crypto.Cipher;
32 import javax.crypto.IllegalBlockSizeException;
33 import javax.crypto.BadPaddingException;
34 import javax.crypto.SecretKey;
35 import javax.crypto.NoSuchPaddingException;
36 import javax.crypto.KeyGenerator;
37 import javax.crypto.SecretKeyFactory;
38 import javax.crypto.spec.IvParameterSpec;
39 import javax.crypto.spec.DESedeKeySpec;
40
41 import org.ofbiz.base.util.GeneralException;
42
43 /**
44  * Utility class for doing DESded (3DES) Two-Way Encryption
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 3.2
49  */

50 public class DesCrypt {
51
52     public static final String JavaDoc module = DesCrypt.class.getName();
53
54     public static SecretKey generateKey() throws NoSuchAlgorithmException JavaDoc {
55         KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
56
57         // generate the DES3 key
58
return keyGen.generateKey();
59     }
60
61     public static byte[] encrypt(SecretKey key, byte[] bytes) throws GeneralException {
62         Cipher cipher = DesCrypt.getCipher(key, Cipher.ENCRYPT_MODE);
63         byte[] encBytes = null;
64         try {
65             encBytes = cipher.doFinal(bytes);
66         } catch (IllegalStateException JavaDoc e) {
67             throw new GeneralException(e);
68         } catch (IllegalBlockSizeException e) {
69             throw new GeneralException(e);
70         } catch (BadPaddingException e) {
71             throw new GeneralException(e);
72         }
73         return encBytes;
74     }
75
76     public static byte[] decrypt(SecretKey key, byte[] bytes) throws GeneralException {
77         Cipher cipher = DesCrypt.getCipher(key, Cipher.DECRYPT_MODE);
78         byte[] decBytes = null;
79         try {
80             decBytes = cipher.doFinal(bytes);
81         } catch (IllegalStateException JavaDoc e) {
82             throw new GeneralException(e);
83         } catch (IllegalBlockSizeException e) {
84             throw new GeneralException(e);
85         } catch (BadPaddingException e) {
86             throw new GeneralException(e);
87         }
88         return decBytes;
89     }
90
91     public static SecretKey getDesKey(byte[] rawKey) throws GeneralException {
92         SecretKeyFactory skf = null;
93         try {
94             skf = SecretKeyFactory.getInstance("DESede");
95         } catch (NoSuchAlgorithmException JavaDoc e) {
96             throw new GeneralException(e);
97         }
98
99         // load the raw key
100
if (rawKey.length > 0) {
101             DESedeKeySpec desedeSpec1 = null;
102             try {
103                 desedeSpec1 = new DESedeKeySpec(rawKey);
104             } catch (InvalidKeyException JavaDoc e) {
105                 throw new GeneralException(e);
106             }
107
108             // create the SecretKey Object
109
SecretKey key = null;
110             try {
111                 key = skf.generateSecret(desedeSpec1);
112             } catch (InvalidKeySpecException JavaDoc e) {
113                 throw new GeneralException(e);
114             }
115             return key;
116         } else {
117             throw new GeneralException("Not a valid DESede key!");
118         }
119     }
120
121     // return a cipher for a key - DESede/CBC/PKCS5Padding IV = 0
122
protected static Cipher getCipher(SecretKey key, int mode) throws GeneralException {
123         byte[] zeros = { 0, 0, 0, 0, 0, 0, 0, 0 };
124         IvParameterSpec iv = new IvParameterSpec(zeros);
125
126         // create the Cipher - DESede/CBC/NoPadding
127
Cipher encCipher = null;
128         try {
129             encCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
130         } catch (NoSuchAlgorithmException JavaDoc e) {
131             throw new GeneralException(e);
132         } catch (NoSuchPaddingException e) {
133             throw new GeneralException(e);
134         }
135         try {
136             encCipher.init(mode, key, iv);
137         } catch (InvalidKeyException JavaDoc e) {
138             throw new GeneralException(e);
139         } catch (InvalidAlgorithmParameterException JavaDoc e) {
140             throw new GeneralException(e);
141         }
142         return encCipher;
143     }
144 }
145
Popular Tags