de.flexiprovider.api
Class PaddingScheme

java.lang.Object
  |
  +--de.flexiprovider.api.PaddingScheme
Direct Known Subclasses:
NoPadding, OneAndZeroesPadding, PKCS5Padding

public abstract class PaddingScheme
extends java.lang.Object

To encrypt a plaintext with a block cipher the input is divided into blocks whose length depends on the encryption algorithm used. To ensure that the block size will divide the length of the input, the last block is padded to the needed length. Vice versa, when decrypting a ciphertext, the padding has to be removed.

To make a new padding scheme available, one has to write a subclass of de.flexiprovider.common.padding.PaddingScheme in order to allow a blockcipher to use it. This subclass has to exist in the package de.flexiprovider.common.padding and the class name must be the same as the name used in the call of Cipher.getInstance(). E.g., if you call Cipher.getInstance("SAFER+/CBC/NoPadding"), then there must exist a class de.flexiprovider.common.padding.NoPadding.

Author:
Christoph Ender, Christoph Sesterhenn, Marcus Lippert, Martin Strese

Field Summary
protected  int blockSize
          Block size used for padding
 
Constructor Summary
PaddingScheme()
           
 
Method Summary
protected abstract  void pad(byte[] input, int inOff, int inLen)
          Pad the input to make its length divisible by the the block length.
protected abstract  int padLength(int inLen)
          Return the number of bytes which will be appended to the the plaintext during padding.
(package private)  void setBlockSize(int blockSize)
          Tell the padding scheme the block size to which input will be padded.
protected abstract  int unpad(byte[] input, int inOff, int inLen)
          Given the plaintext that includes the padding bytes, unpad the plaintext and return the index indicating where the padding bytes start.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

blockSize

protected int blockSize
Block size used for padding
Constructor Detail

PaddingScheme

public PaddingScheme()
Method Detail

setBlockSize

final void setBlockSize(int blockSize)
Tell the padding scheme the block size to which input will be padded.
Parameters:
blockSize - length of one block

padLength

protected abstract int padLength(int inLen)
Return the number of bytes which will be appended to the the plaintext during padding.
Parameters:
inLen - the length of the plaintext to be padded
Returns:
the number of padding bytes (may be 0)

pad

protected abstract void pad(byte[] input,
                            int inOff,
                            int inLen)
                     throws BadPaddingException
Pad the input to make its length divisible by the the block length. The padding is written to the same buffer which is used for input. The caller has to ensure that the input array is large enough to hold the padding bytes.
Parameters:
input - byte array containing the plaintext to be padded
inOff - index where the plaintext starts
inLen - length of the plaintext
Throws:
BadPaddingException - if the input buffer is too small to hold the padding bytes.

unpad

protected abstract int unpad(byte[] input,
                             int inOff,
                             int inLen)
                      throws BadPaddingException
Given the plaintext that includes the padding bytes, unpad the plaintext and return the index indicating where the padding bytes start.
Parameters:
input - byte array containing the padded plaintext
inOff - index where the plaintext starts
inLen - size of the plaintext
Returns:
index in the array where the padding bytes start
Throws:
BadPaddingException - if unpadding fails.