001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.crypto;
028:
029: /**
030: * Wrapper for all DES cipher classes.
031: */
032: public class DES extends Cipher {
033: Cipher cipher;
034:
035: /**
036: * Public by name constructor.
037: */
038: public DES() {
039: }
040:
041: /**
042: * Called by the factory method to set the mode and padding parameters.
043: * Need because Class.newInstance does not take args.
044: *
045: * @param mode the mode parsed from the transformation parameter of
046: * getInstance
047: * @param padding the paddinge parsed from the transformation parameter of
048: * getInstance
049: *
050: * @exception NoSuchPaddingException if <code>transformation</code>
051: * contains a padding scheme that is not available.
052: */
053: protected void setChainingModeAndPadding(String mode, String padding)
054: throws NoSuchPaddingException {
055: if (mode.equals("ECB") || mode.equals("")) {
056: cipher = new DES_ECB(false);
057: } else if (mode.equals("CBC")) {
058: cipher = new DES_CBC(false);
059: } else {
060: throw new IllegalArgumentException();
061: }
062:
063: cipher.setChainingModeAndPadding(mode, padding);
064: }
065:
066: /**
067: * Initializes this cipher with a key and a set of algorithm
068: * parameters.
069: *
070: * <p>The cipher is initialized for one of the following operations:
071: * encryption or decryption depending
072: * on the value of <code>opmode</code>.
073: *
074: * <p>If this cipher requires any algorithm parameters and
075: * <code>params</code> is null, the underlying cipher implementation is
076: * supposed to generate the required parameters itself (using
077: * provider-specific default or random values) if it is being
078: * initialized for encryption, and raise an
079: * <code>InvalidAlgorithmParameterException</code> if it is being
080: * initialized for decryption.
081: *
082: * <p>Note that when a Cipher object is initialized, it loses all
083: * previously-acquired state. In other words, initializing a Cipher is
084: * equivalent to creating a new instance of that Cipher and initializing
085: * it.
086: *
087: * @param opmode the operation mode of this cipher (this is one of the
088: * following:
089: * <code>ENCRYPT_MODE</code> or <code>DECRYPT_MODE</code>)
090: * @param key the encryption key
091: * @param params the algorithm parameters
092: *
093: * @exception InvalidKeyException if the given key is inappropriate for
094: * initializing this cipher, or its keysize exceeds the maximum allowable
095: * keysize.
096: * @exception InvalidAlgorithmParameterException if the given algorithm
097: * parameters are inappropriate for this cipher,
098: * or this cipher is being initialized for decryption and requires
099: * algorithm parameters and <code>params</code> is null, or the given
100: * algorithm parameters imply a cryptographic strength that would exceed
101: * the legal limits.
102: */
103: public void init(int opmode, Key key, CryptoParameter params)
104: throws InvalidKeyException,
105: InvalidAlgorithmParameterException {
106: cipher.init(opmode, key, params);
107: }
108:
109: /**
110: * Continues a multiple-part encryption or decryption operation
111: * (depending on how this cipher was initialized), processing another data
112: * part.
113: *
114: * <p>The first <code>inputLen</code> bytes in the <code>input</code>
115: * buffer, starting at <code>inputOffset</code> inclusive, are processed,
116: * and the result is stored in the <code>output</code> buffer, starting at
117: * <code>outputOffset</code> inclusive.
118: *
119: * <p>If the <code>output</code> buffer is too small to hold the result,
120: * a <code>ShortBufferException</code> is thrown. In this case, repeat this
121: * call with a larger output buffer.
122: *
123: * <p>If <code>inputLen</code> is zero, this method returns
124: * a length of zero.
125: *
126: * <p>Note: this method should be copy-safe, which means the
127: * <code>input</code> and <code>output</code> buffers can reference
128: * the same byte array and no unprocessed input data is overwritten
129: * when the result is copied into the output buffer.
130: *
131: * @param input the input buffer
132: * @param inputOffset the offset in <code>input</code> where the input
133: * starts
134: * @param inputLen the input length
135: * @param output the buffer for the result
136: * @param outputOffset the offset in <code>output</code> where the result
137: * is stored
138: *
139: * @return the number of bytes stored in <code>output</code>
140: *
141: * @exception IllegalStateException if this cipher is in a wrong state
142: * (e.g., has not been initialized)
143: * @exception ShortBufferException if the given output buffer is too small
144: * to hold the result
145: */
146: public int update(byte[] input, int inputOffset, int inputLen,
147: byte[] output, int outputOffset)
148: throws IllegalStateException, ShortBufferException {
149: return cipher.update(input, inputOffset, inputLen, output,
150: outputOffset);
151: }
152:
153: /**
154: * Encrypts or decrypts data in a single-part operation, or finishes a
155: * multiple-part operation. The data is encrypted or decrypted,
156: * depending on how this cipher was initialized.
157: *
158: * <p>The first <code>inputLen</code> bytes in the <code>input</code>
159: * buffer, starting at <code>inputOffset</code> inclusive, and any input
160: * bytes that may have been buffered during a previous
161: * <code>update</code> operation, are processed, with padding
162: * (if requested) being applied.
163: * The result is stored in the <code>output</code> buffer, starting at
164: * <code>outputOffset</code> inclusive.
165: *
166: * <p>If the <code>output</code> buffer is too small to hold the result,
167: * a <code>ShortBufferException</code> is thrown. In this case, repeat this
168: * call with a larger output buffer.
169: *
170: * <p>Upon finishing, this method resets this cipher object to the state
171: * it was in when previously initialized via a call to <code>init</code>.
172: * That is, the object is reset and available to encrypt or decrypt
173: * (depending on the operation mode that was specified in the call to
174: * <code>init</code>) more data.
175: *
176: * <p>Note: if any exception is thrown, this cipher object may need to
177: * be reset before it can be used again.
178: *
179: * <p>Note: this method should be copy-safe, which means the
180: * <code>input</code> and <code>output</code> buffers can reference
181: * the same byte array and no unprocessed input data is overwritten
182: * when the result is copied into the output buffer.
183: *
184: * @param input the input buffer
185: * @param inputOffset the offset in <code>input</code> where the input
186: * starts
187: * @param inputLen the input length
188: * @param output the buffer for the result
189: * @param outputOffset the offset in <code>output</code> where the result
190: * is stored
191: *
192: * @return the number of bytes stored in <code>output</code>
193: *
194: * @exception IllegalStateException if this cipher is in a wrong state
195: * (e.g., has not been initialized)
196: * @exception IllegalBlockSizeException if this cipher is a block cipher,
197: * no padding has been requested (only in encryption mode), and the total
198: * input length of the data processed by this cipher is not a multiple of
199: * block size
200: * @exception ShortBufferException if the given output buffer is too small
201: * to hold the result
202: * @exception BadPaddingException if this cipher is in decryption mode,
203: * and (un)padding has been requested, but the decrypted data is not
204: * bounded by the appropriate padding bytes
205: */
206: public int doFinal(byte[] input, int inputOffset, int inputLen,
207: byte[] output, int outputOffset)
208: throws IllegalStateException, ShortBufferException,
209: IllegalBlockSizeException, BadPaddingException {
210: return cipher.doFinal(input, inputOffset, inputLen, output,
211: outputOffset);
212: }
213:
214: /**
215: * Returns the initialization vector (IV) in a new buffer.
216: * This is useful in the case where a random IV was created.
217: * @return the initialization vector in a new buffer,
218: * or <code>null</code> if the underlying algorithm does
219: * not use an IV, or if the IV has not yet been set.
220: */
221: public byte[] getIV() {
222: return cipher.getIV();
223: }
224: }
|