001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.base.crypto;
019:
020: import java.security.NoSuchAlgorithmException;
021: import java.security.InvalidKeyException;
022: import java.security.InvalidAlgorithmParameterException;
023: import java.security.spec.InvalidKeySpecException;
024: import javax.crypto.Cipher;
025: import javax.crypto.IllegalBlockSizeException;
026: import javax.crypto.BadPaddingException;
027: import javax.crypto.SecretKey;
028: import javax.crypto.NoSuchPaddingException;
029: import javax.crypto.KeyGenerator;
030: import javax.crypto.SecretKeyFactory;
031: import javax.crypto.spec.IvParameterSpec;
032: import javax.crypto.spec.DESedeKeySpec;
033:
034: import org.ofbiz.base.util.GeneralException;
035:
036: /**
037: * Utility class for doing DESded (3DES) Two-Way Encryption
038: *
039: */
040: public class DesCrypt {
041:
042: public static final String module = DesCrypt.class.getName();
043:
044: public static SecretKey generateKey()
045: throws NoSuchAlgorithmException {
046: KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
047:
048: // generate the DES3 key
049: return keyGen.generateKey();
050: }
051:
052: public static byte[] encrypt(SecretKey key, byte[] bytes)
053: throws GeneralException {
054: Cipher cipher = DesCrypt.getCipher(key, Cipher.ENCRYPT_MODE);
055: byte[] encBytes = null;
056: try {
057: encBytes = cipher.doFinal(bytes);
058: } catch (IllegalStateException e) {
059: throw new GeneralException(e);
060: } catch (IllegalBlockSizeException e) {
061: throw new GeneralException(e);
062: } catch (BadPaddingException e) {
063: throw new GeneralException(e);
064: }
065: return encBytes;
066: }
067:
068: public static byte[] decrypt(SecretKey key, byte[] bytes)
069: throws GeneralException {
070: Cipher cipher = DesCrypt.getCipher(key, Cipher.DECRYPT_MODE);
071: byte[] decBytes = null;
072: try {
073: decBytes = cipher.doFinal(bytes);
074: } catch (IllegalStateException e) {
075: throw new GeneralException(e);
076: } catch (IllegalBlockSizeException e) {
077: throw new GeneralException(e);
078: } catch (BadPaddingException e) {
079: throw new GeneralException(e);
080: }
081: return decBytes;
082: }
083:
084: public static SecretKey getDesKey(byte[] rawKey)
085: throws GeneralException {
086: SecretKeyFactory skf = null;
087: try {
088: skf = SecretKeyFactory.getInstance("DESede");
089: } catch (NoSuchAlgorithmException e) {
090: throw new GeneralException(e);
091: }
092:
093: // load the raw key
094: if (rawKey.length > 0) {
095: DESedeKeySpec desedeSpec1 = null;
096: try {
097: desedeSpec1 = new DESedeKeySpec(rawKey);
098: } catch (InvalidKeyException e) {
099: throw new GeneralException(e);
100: }
101:
102: // create the SecretKey Object
103: SecretKey key = null;
104: try {
105: key = skf.generateSecret(desedeSpec1);
106: } catch (InvalidKeySpecException e) {
107: throw new GeneralException(e);
108: }
109: return key;
110: } else {
111: throw new GeneralException("Not a valid DESede key!");
112: }
113: }
114:
115: // return a cipher for a key - DESede/CBC/PKCS5Padding IV = 0
116: protected static Cipher getCipher(SecretKey key, int mode)
117: throws GeneralException {
118: byte[] zeros = { 0, 0, 0, 0, 0, 0, 0, 0 };
119: IvParameterSpec iv = new IvParameterSpec(zeros);
120:
121: // create the Cipher - DESede/CBC/NoPadding
122: Cipher encCipher = null;
123: try {
124: encCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
125: } catch (NoSuchAlgorithmException e) {
126: throw new GeneralException(e);
127: } catch (NoSuchPaddingException e) {
128: throw new GeneralException(e);
129: }
130: try {
131: encCipher.init(mode, key, iv);
132: } catch (InvalidKeyException e) {
133: throw new GeneralException(e);
134: } catch (InvalidAlgorithmParameterException e) {
135: throw new GeneralException(e);
136: }
137: return encCipher;
138: }
139: }
|