FileDocCategorySizeDatePackage
PKCS5Padding.javaAPI DocphoneME MR2 API (J2ME)2699Wed May 02 18:00:24 BST 2007com.sun.midp.crypto

PKCS5Padding

public final class PKCS5Padding extends Object implements Padder
This class implements padding as specified in the PKCS#5 standard.

Fields Summary
private int
blockSize
Contains the block size.
Constructors Summary
public PKCS5Padding(int blockSize)
Constructor.

param
blockSize block size

        this.blockSize = blockSize;
    
Methods Summary
public intpad(byte[] queue, int count)
Pads the input according to the PKCS5 padding scheme.

param
queue containing the last bytes of data that must be padded
param
count number of data bytes
return
the number of padding bytes added

        int len = blockSize - count;

        for (int i = count; i < blockSize; i++) {
            queue[i] = (byte) len;
        }
        return len;
    
public intunPad(byte[] outBuff, int size)
Removes padding bytes that were added to the input.

param
outBuff the output buffer
param
size size of data
return
the number of padding bytes, allowing them to be removed prior to putting results in the users output buffer and -1 if input is not properly padded
exception
BadPaddingException if not properly padded

        int padValue = outBuff[size - 1] & 0xff;

        if (padValue < 1 || padValue > blockSize) {
            throw new BadPaddingException();
        }

        for (int i = 0; i < padValue; i++) {
            if (outBuff[--size] != padValue)
                throw new BadPaddingException();
        }
        return padValue;