gnu.javax.crypto.cipher

Class TripleDES

Implemented Interfaces:
Cloneable, IBlockCipher, gnu.javax.crypto.cipher.IBlockCipherSpi

public class TripleDES
extends BaseCipher

Triple-DES, 3DES, or DESede is a combined cipher that uses three iterations of the Data Encryption Standard cipher to theoretically improve the security of plain DES, at the cost of speed.

Triple-DES runs the DES algorithm three times with one, two or three independent 56-bit (DES) keys. When used with one DES key, the cipher behaves exactly like a (slower) DES.

To encrypt:

Ci = Ek3 ( Ek2-1 ( Ek1 ( Pi )))

And to decrypt:

Pi = Ek1-1 ( Ek2 ( Ek3-1 ( Ci )))

(The "ede" comes from the encryption operation, which runs Encrypt-Decrypt-Encrypt)

References:

  1. Bruce Schneier, Applied Cryptography: Protocols, Algorithms, and Source Code in C, Second Edition. (1996 John Wiley and Sons) ISBN 0-471-11709-9. Page 294--295.

Field Summary

static int
BLOCK_SIZE
Triple-DES only operates on 64 bit blocks.
static int
KEY_SIZE
By default, Triple-DES uses 168 bits of a parity-adjusted 192 bit key.

Fields inherited from class gnu.javax.crypto.cipher.BaseCipher

currentBlockSize, currentKey, defaultBlockSize, defaultKeySize, lock, name

Fields inherited from interface gnu.javax.crypto.cipher.IBlockCipher

CIPHER_BLOCK_SIZE, KEY_MATERIAL

Constructor Summary

TripleDES()
Default 0-arguments constructor.

Method Summary

static void
adjustParity(byte[] kb, int offset)
Convenience method which calls the method with same name and three arguments, passing 3 as the value of the first parameter.
static void
adjustParity(int keyCount, byte[] kb, int offset)
Adjusts, in-situ, the parity of the designated bytes, so they can be used as DES keys for a 3-DES 1-, 2- or 3-key cipher.
Iterator<E>
blockSizes()
Returns an Iterator over the supported block sizes.
Object
clone()
Returns a clone of this instance.
void
decrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
void
encrypt(byte[] in, int i, byte[] out, int o, Object K, int bs)
static boolean
isParityAdjusted(byte[] kb, int offset)
Convenience method which calls the method with same name and three arguments, passing 3 as the value of the first parameter.
static boolean
isParityAdjusted(int keyCount, byte[] kb, int offset)
Tests if enough bytes, expected to be used as DES keys for a 3-DES 1-, 2- or 3-key cipher, located in a designated byte array, has already been parity adjusted.
Iterator<E>
keySizes()
Returns an Iterator over the supported key sizes.
Object
makeKey(byte[] kb, int bs)

Methods inherited from class gnu.javax.crypto.cipher.BaseCipher

clone, currentBlockSize, decryptBlock, defaultBlockSize, defaultKeySize, encryptBlock, init, name, reset, selfTest, testKat, testKat

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

BLOCK_SIZE

public static final int BLOCK_SIZE
Triple-DES only operates on 64 bit blocks.
Field Value:
8

KEY_SIZE

public static final int KEY_SIZE
By default, Triple-DES uses 168 bits of a parity-adjusted 192 bit key.
Field Value:
24

Constructor Details

TripleDES

public TripleDES()
Default 0-arguments constructor.

Method Details

adjustParity

public static void adjustParity(byte[] kb,
                                int offset)
Convenience method which calls the method with same name and three arguments, passing 3 as the value of the first parameter.
Parameters:
kb - The key bytes to adjust.
offset - The starting offset into the key bytes.

adjustParity

public static void adjustParity(int keyCount,
                                byte[] kb,
                                int offset)
Adjusts, in-situ, the parity of the designated bytes, so they can be used as DES keys for a 3-DES 1-, 2- or 3-key cipher.
Parameters:
keyCount - the number of independent DES keys. Can be either 1, 2 or 3. Any other value will cause an IllegalArgumentException to be raised.
kb - the array containing the key bytes to adjust. MUST have at least 8 * keyCount bytes starting at offset position offset, otherwise an ArrayIndexOutOfBoundsException will be raised.
offset - the starting offset into the array.

blockSizes

public Iterator<E> blockSizes()
Returns an Iterator over the supported block sizes. Each element returned by this object is an Integer.
Specified by:
blockSizes in interface IBlockCipher
blockSizes in interface gnu.javax.crypto.cipher.IBlockCipherSpi
Returns:
an Iterator over the supported block sizes.

clone

public Object clone()
Returns a clone of this instance.
Specified by:
clone in interface IBlockCipher
Overrides:
clone in interface BaseCipher
Returns:
a clone copy of this instance.

decrypt

public void decrypt(byte[] in,
                    int i,
                    byte[] out,
                    int o,
                    Object K,
                    int bs)
Specified by:
decrypt in interface gnu.javax.crypto.cipher.IBlockCipherSpi

encrypt

public void encrypt(byte[] in,
                    int i,
                    byte[] out,
                    int o,
                    Object K,
                    int bs)
Specified by:
encrypt in interface gnu.javax.crypto.cipher.IBlockCipherSpi

isParityAdjusted

public static boolean isParityAdjusted(byte[] kb,
                                       int offset)
Convenience method which calls the method with same name and three arguments, passing 3 as the value of the first parameter.
Parameters:
kb - The key bytes to test.
offset - The starting offset into the key bytes.
Returns:
true if the bytes in kb starting at offset are parity adjusted.

isParityAdjusted

public static boolean isParityAdjusted(int keyCount,
                                       byte[] kb,
                                       int offset)
Tests if enough bytes, expected to be used as DES keys for a 3-DES 1-, 2- or 3-key cipher, located in a designated byte array, has already been parity adjusted.
Parameters:
keyCount - the number of independent DES keys. Can be either 1, 2 or 3. Any other value will cause an IllegalArgumentException to be raised.
kb - the array containing the key bytes to test. MUST have at least 8 * keyCount bytes starting at offset position offset, otherwise an ArrayIndexOutOfBoundsException will be raised.
offset - the starting offset into the array.
Returns:
true if the bytes in kb starting at offset are parity adjusted.

keySizes

public Iterator<E> keySizes()
Returns an Iterator over the supported key sizes. Each element returned by this object is an Integer.
Specified by:
keySizes in interface IBlockCipher
keySizes in interface gnu.javax.crypto.cipher.IBlockCipherSpi
Returns:
an Iterator over the supported key sizes.

makeKey

public Object makeKey(byte[] kb,
                      int bs)
            throws InvalidKeyException
Specified by:
makeKey in interface gnu.javax.crypto.cipher.IBlockCipherSpi

TripleDES.java -- Copyright (C) 2002, 2006 Free Software Foundation, Inc. This file is a part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.