Invokes the Cipher to perform encryption or decryption (depending on the initialized mode). - Java Security

Java examples for Security:decrypt encrypt String

Description

Invokes the Cipher to perform encryption or decryption (depending on the initialized mode).

Demo Code


//package com.java2s;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

import javax.crypto.ShortBufferException;

public class Main {
    /**/*  w w w . j  av a  2s  .com*/
     * Invokes the Cipher to perform encryption or decryption (depending on the initialized mode).
     */
    public static byte[] doFinal(Cipher cipher, byte[] input) {
        try {
            return cipher.doFinal(input);
        } catch (IllegalBlockSizeException e) {
            throw new IllegalStateException(
                    "Unable to invoke Cipher due to illegal block size", e);
        } catch (BadPaddingException e) {
            throw new IllegalStateException(
                    "Unable to invoke Cipher due to bad padding", e);
        }
    }

    /**
     * Invokes the Cipher to perform encryption or decryption (depending on the
     * initialized mode).
     */
    public static byte[] doFinal(Cipher cipher, byte[] input,
            int inputOffSet, int inputLen) {
        try {
            return cipher.doFinal(input, inputOffSet, inputLen);
        } catch (IllegalBlockSizeException e) {
            throw new IllegalStateException(
                    "Unable to invoke Cipher due to illegal block size", e);
        } catch (BadPaddingException e) {
            throw new IllegalStateException(
                    "Unable to invoke Cipher due to bad padding", e);
        }
    }

    /**
     * Invokes the Cipher to perform encryption or decryption (depending on the
     * initialized mode).
     */
    public static int doFinal(Cipher cipher, byte[] input, int inputOffSet,
            int inputLen, byte[] ouput, int outputOffSet) {
        try {
            return cipher.doFinal(input, inputOffSet, inputLen, ouput,
                    outputOffSet);
        } catch (IllegalBlockSizeException e) {
            throw new IllegalStateException(
                    "Unable to invoke Cipher due to illegal block size", e);
        } catch (BadPaddingException e) {
            throw new IllegalStateException(
                    "Unable to invoke Cipher due to bad padding", e);
        } catch (ShortBufferException e) {
            throw new IllegalStateException("Short Buffer Exception", e);
        }
    }
}

Related Tutorials