//============================================================================== // NullCipherSpi.java //============================================================================== package tribble.crypto; // System imports import java.lang.Exception; import java.lang.String; import java.lang.System; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import javax.crypto.NoSuchPaddingException; /******************************************************************************* * Null block cipher. * Implements a null cryptographic cipher which does not do any actual * encrypting of data. * * * @version API 2, $Revision: 1.2 $ $Date: 2005/09/11 04:08:24 $ * @since 2005-04-25 * @author * David R. Tribble * (david@tribble.com). * *
* Copyright ©2005 by David R. Tribble, all rights reserved.
*
* Permission is granted to freely use and distribute this source code
* provided that the original copyright and authorship notices remain
* intact.
*/
public final class NullCipherSpi
extends tribble.crypto.BlockCipherSpi
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/crypto/NullCipherSpi.java $Revision: 1.2 $ $Date: 2005/09/11 04:08:24 $\n";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Constants
/** Algorithm name. */
private static final String ALGORITHM = "Null";
/** Block size, in bytes. */
private static final int BLOCK_SIZE = 8;
//----------------------------------
// Test vectors (see #main)
private static final byte[] TEST_KEY =
{
(byte) 0x44, (byte) 0x61, (byte) 0x76, (byte) 0x69,
(byte) 0x64, (byte) 0x20, (byte) 0x52, (byte) 0x2E,
(byte) 0x20, (byte) 0x54, (byte) 0x72, (byte) 0x69,
(byte) 0x62, (byte) 0x62, (byte) 0x6C, (byte) 0x65,
};
private static final byte[] TEST_PLAINTEXT =
{
(byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
(byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF,
(byte) 0x00, (byte) 0xFF, (byte) 0x00, (byte) 0xFF,
(byte) 0xA5, (byte) 0xC3, (byte) 0xB4, (byte) 0x81,
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Static methods
/***************************************************************************
* Test driver.
*
*
* @param args
* Command line arguments.
*
* @throws Exception
* Thrown if an error occurs in the encryption engine.
*
* @since 1.1, 2005-04-25
*/
public static final void main(String[] args)
throws Exception
{
// Test this cipher SPI
test(new NullCipherSpi(), TEST_KEY, TEST_PLAINTEXT);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Variables
// (None)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Constructors
/***************************************************************************
* Default constructor.
*
* @throws NoSuchAlgorithmException
* Never thrown.
*
* @throws NoSuchPaddingException
* Never thrown.
*
* @since 1.1, 2005-04-25
*/
public NullCipherSpi()
throws NoSuchAlgorithmException, NoSuchPaddingException
{
// Initialize
super(new NullCipher(), MODE_ECB, PADDING_NONE);
m_alg = ALGORITHM;
m_blockLen = BLOCK_SIZE;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Methods
/***************************************************************************
* Initialize this cipher.
*
* @param key
* The symmetric (secret) key for this cipher.
*
* @param decrypt
* If true, this indicates that this cipher is to be initialized for
* decrypting, otherwise it is to be initialized for encrypting.
*
* @since 1.1, 2005-04-25
*/
protected void initialize(byte[] key, boolean decrypt)
throws InvalidKeyException
//overrides tribble.crypto.SymmetricCipherSpi
{
/*+++NOT USED
if (key == null)
throw new InvalidKeyException("Key missing");
if (key.length < 1)
throw new InvalidKeyException("Invalid key length");
this.m_keyLen = key.length;
+++*/
this.m_decrypt = decrypt;
}
/***************************************************************************
* Encrypt exactly one block of plaintext.
*
* @param in
* Array containing a single block of plaintext bytes to encrypt.
* The length of the block is exactly equal to the cipher block size.
*
* @param inOff
* Index of the first byte of the plaintext block within array in to
* encrypt.
*
* @param out
* Array that will be filled with the encrypted ciphertext block.
* The length of the block is exactly equal to the cipher block size.
*
* @param outOff
* Index of the first byte of array out to fill with the encrypted
* ciphertext block.
*
* @see #blockDecrypt blockDecrypt()
* @see #initialize initialize()
*
* @since 1.1, 2005-04-25
*/
protected void blockEncrypt(/*const*/ byte[] in, int inOff,
byte[] out, int outOff)
//overrides tribble.crypto.SymmetricCipherSpi
{
// This cipher algorithm does nothing
for (int i = BLOCK_SIZE; i > 0; i--)
out[outOff++] = in[inOff++];
}
/***************************************************************************
* Decrypt exactly one block of plaintext.
*
* @param in
* Array containing a single block of ciphertext bytes to decrypt.
* The length of the block is exactly equal to the cipher block size.
*
* @param inOff
* Index of the first byte of the ciphertext block within array in
* to decrypt.
*
* @param out
* Array that will be filled with the decrypted plaintext block.
* The length of the block is exactly equal to the cipher block size.
*
* @param outOff
* Index of the first byte of array out to fill with the decrypted
* plaintext block.
*
* @see #blockEncrypt blockEncrypt()
* @see #initialize initialize()
*
* @since 1.1, 2005-04-25
*/
protected void blockDecrypt(/*const*/ byte[] in, int inOff,
byte[] out, int outOff)
//overrides tribble.crypto.SymmetricCipherSpi
{
// This cipher algorithm does nothing
for (int i = BLOCK_SIZE; i > 0; i--)
out[outOff++] = in[inOff++];
}
}
// End NullCipherSpi.java