org.bouncycastle.crypto.engines
Class BlowfishEngine

java.lang.Object
  extended by org.bouncycastle.crypto.engines.BlowfishEngine
All Implemented Interfaces:
BlockCipher

public final class BlowfishEngine
extends java.lang.Object
implements BlockCipher

A class that provides Blowfish key encryption operations, such as encoding data and generating keys. All the algorithms herein are from Applied Cryptography and implement a simplified cryptography interface.


Field Summary
private static int BLOCK_SIZE
           
private  boolean encrypting
           
private static int[] KP
           
private static int[] KS0
           
private static int[] KS1
           
private static int[] KS2
           
private static int[] KS3
           
private  int[] P
           
private static int P_SZ
           
private static int ROUNDS
           
private  int[] S0
           
private  int[] S1
           
private  int[] S2
           
private  int[] S3
           
private static int SBOX_SK
           
private  byte[] workingKey
           
 
Constructor Summary
BlowfishEngine()
           
 
Method Summary
private  void Bits32ToBytes(int in, byte[] b, int offset)
           
private  int BytesTo32bits(byte[] b, int i)
           
private  void decryptBlock(byte[] src, int srcIndex, byte[] dst, int dstIndex)
          Decrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset.
private  void encryptBlock(byte[] src, int srcIndex, byte[] dst, int dstIndex)
          Encrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset.
private  int F(int x)
           
 java.lang.String getAlgorithmName()
          Return the name of the algorithm the cipher implements.
 int getBlockSize()
          Return the block size for this cipher (in bytes).
 void init(boolean encrypting, CipherParameters params)
          initialise a Blowfish cipher.
 int processBlock(byte[] in, int inOff, byte[] out, int outOff)
          Process one block of input from the array in and write it to the out array.
private  void processTable(int xl, int xr, int[] table)
          apply the encryption cycle to each value pair in the table.
 void reset()
          Reset the cipher.
private  void setKey(byte[] key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KP

private static final int[] KP

KS0

private static final int[] KS0

KS1

private static final int[] KS1

KS2

private static final int[] KS2

KS3

private static final int[] KS3

ROUNDS

private static final int ROUNDS
See Also:
Constant Field Values

BLOCK_SIZE

private static final int BLOCK_SIZE
See Also:
Constant Field Values

SBOX_SK

private static final int SBOX_SK
See Also:
Constant Field Values

P_SZ

private static final int P_SZ
See Also:
Constant Field Values

S0

private final int[] S0

S1

private final int[] S1

S2

private final int[] S2

S3

private final int[] S3

P

private final int[] P

encrypting

private boolean encrypting

workingKey

private byte[] workingKey
Constructor Detail

BlowfishEngine

public BlowfishEngine()
Method Detail

init

public void init(boolean encrypting,
                 CipherParameters params)
initialise a Blowfish cipher.

Specified by:
init in interface BlockCipher
Parameters:
encrypting - whether or not we are for encryption.
params - the parameters required to set up the cipher.
Throws:
java.lang.IllegalArgumentException - if the params argument is inappropriate.

getAlgorithmName

public java.lang.String getAlgorithmName()
Description copied from interface: BlockCipher
Return the name of the algorithm the cipher implements.

Specified by:
getAlgorithmName in interface BlockCipher
Returns:
the name of the algorithm the cipher implements.

processBlock

public final int processBlock(byte[] in,
                              int inOff,
                              byte[] out,
                              int outOff)
Description copied from interface: BlockCipher
Process one block of input from the array in and write it to the out array.

Specified by:
processBlock in interface BlockCipher
Parameters:
in - the array containing the input data.
inOff - offset into the in array the data starts at.
out - the array the output data will be copied into.
outOff - the offset into the out array the output will start at.
Returns:
the number of bytes processed and produced.

reset

public void reset()
Description copied from interface: BlockCipher
Reset the cipher. After resetting the cipher is in the same state as it was after the last init (if there was one).

Specified by:
reset in interface BlockCipher

getBlockSize

public int getBlockSize()
Description copied from interface: BlockCipher
Return the block size for this cipher (in bytes).

Specified by:
getBlockSize in interface BlockCipher
Returns:
the block size for this cipher in bytes.

F

private int F(int x)

processTable

private void processTable(int xl,
                          int xr,
                          int[] table)
apply the encryption cycle to each value pair in the table.


setKey

private void setKey(byte[] key)

encryptBlock

private void encryptBlock(byte[] src,
                          int srcIndex,
                          byte[] dst,
                          int dstIndex)
Encrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize.


decryptBlock

private void decryptBlock(byte[] src,
                          int srcIndex,
                          byte[] dst,
                          int dstIndex)
Decrypt the given input starting at the given offset and place the result in the provided buffer starting at the given offset. The input will be an exact multiple of our blocksize.


BytesTo32bits

private int BytesTo32bits(byte[] b,
                          int i)

Bits32ToBytes

private void Bits32ToBytes(int in,
                           byte[] b,
                           int offset)