Example usage for javax.crypto NoSuchPaddingException NoSuchPaddingException

List of usage examples for javax.crypto NoSuchPaddingException NoSuchPaddingException

Introduction

In this page you can find the example usage for javax.crypto NoSuchPaddingException NoSuchPaddingException.

Prototype

public NoSuchPaddingException(String msg) 

Source Link

Document

Constructs a NoSuchPaddingException with the specified detail message.

Usage

From source file:org.opensc.pkcs11.spi.PKCS11CipherSpi.java

@Override
protected void engineSetPadding(String padding) throws NoSuchPaddingException {
    if (!padding.equals("PKCS1Padding"))
        throw new NoSuchPaddingException("Only PKCS1Padding is supported.");
}

From source file:watne.seis720.project.AES_Utilities.java

/**
 * Given an array of bytes padded using the specified method, return an
 * unpadded array of bytes./*from w w w  .j  ava 2 s  .c o  m*/
 * @param padded the padded array of bytes.
 * @param padding the padding methodology used.
 * @return an unpadded array of bytes.
 * @throws NoSuchPaddingException if the padded array is not of the proper
 * length for the given Padding type.
 */
public static byte[] getUnpadded(byte[] padded, Padding padding) throws NoSuchPaddingException {
    byte[] unpadded = padded; // Initialize to padded.

    switch (padding) {
    case PKCS5PADDING:
        // Get number of bytes padding.
        if (padded.length > 0) {
            // Last padding bytes have number of padding bytes, so just
            // look at last byte in array and convert to int.
            int numPaddingBytes = padded[padded.length - 1];

            if (numPaddingBytes < 1) {
                throw new NoSuchPaddingException(
                        "AES_Utilities.getUnpadded() error: Invalid number of padding bytes: "
                                + numPaddingBytes);
            } // if (numPaddingBytes < 1)
            else if (numPaddingBytes > padded.length) {
                throw new NoSuchPaddingException("AES_Utilities.getUnpadded() error: padding " + numPaddingBytes
                        + " bytes exceeds array size " + padded.length);
            } // else if (numPaddingBytes > padded.length)

            // Create new unpadded array without numPaddingBytes
            // elements at end.
            unpadded = new byte[padded.length - numPaddingBytes];
            // Copy the elements before the padding.
            System.arraycopy(padded, 0, unpadded, 0, padded.length - numPaddingBytes);
        } // if (padded.length > 0)
        else {
            throw new NoSuchPaddingException("AES_Utilities.getUnpadded() error: array has no padding");
        } // else [padded.length <= 0]
        break; // case PKCS5PADDING
    case NOPADDING:
        /* Unpadded = padded; do nothing. */
        break;
    default:
        throw new NoSuchPaddingException("Padding mode " + padding + " is not supported.");
    } // switch (padding)

    return unpadded; // Return unpadded array.
}