Example usage for javax.crypto Cipher doFinal

List of usage examples for javax.crypto Cipher doFinal

Introduction

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

Prototype

public final byte[] doFinal(byte[] input, int inputOffset, int inputLen)
        throws IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Usage

From source file:Main.java

/**
 * Invokes the Cipher to perform encryption or decryption (depending on the
 * initialized mode)./*from ww  w  .j  a v a  2  s. c  o m*/
 */
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);
    }
}

From source file:com.bytecode.util.Crypto.java

private static byte[] decrypt(String keystring, byte[] message, int bits) throws Exception {
    byte[] decValue = null;
    byte[] nonceBytes = Arrays.copyOf(Arrays.copyOf(message, 8), 16);
    IvParameterSpec nonce = new IvParameterSpec(nonceBytes);

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key, nonce);
    decValue = c.doFinal(message, 8, message.length - 8);

    return decValue;
}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Encrypts a data blob using the given key and cipher.
 * //from   w  w  w.j  a  v a 2  s . com
 * @param data
 * @param offset
 * @param length
 * @param key
 * @param cipher
 * @return
 * @throws GeneralSecurityException
 */
public static byte[] encrypt(byte[] data, int offset, int length, Key key, String cipherTransformation,
        String cipherProvider) throws GeneralSecurityException {
    Cipher cipher = loadCipher(cipherTransformation, cipherProvider);
    cipher.init(Cipher.ENCRYPT_MODE, key);

    return cipher.doFinal(data, offset, length);
}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Decrypts a data blob using the given key and cipher.
 * // w w w . j  a  v a  2  s.co  m
 * @param data
 * @param offset
 * @param length
 * @param key
 * @param cipher
 * @return
 * @throws GeneralSecurityException
 */
public static byte[] decrypt(byte[] data, int offset, int length, Key key, String cipherTransformation,
        String cipherProvider) throws GeneralSecurityException {
    Cipher cipher = loadCipher(cipherTransformation, cipherProvider);
    cipher.init(Cipher.DECRYPT_MODE, key);

    return cipher.doFinal(data, offset, length);
}

From source file:com.cl.roadshow.crypto.AESCtr.java

/**
 * Private decryption method.//  w  w w . ja  v a 2 s.  c  om
 * 
 * @param keystring
 * @param message
 * @param bits
 * @return bytearray containing decrypted message
 * @throws Exception
 */
private static byte[] decrypt(String keystring, byte[] message, int bits) throws Exception {
    byte[] decValue = null;
    byte[] nonceBytes = Arrays.copyOf(Arrays.copyOf(message, 8), 16);
    IvParameterSpec nonce = new IvParameterSpec(nonceBytes);

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key, nonce);
    decValue = c.doFinal(message, 8, message.length - 8);

    return decValue;
}

From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static String rsaEncrypt(String string) throws Exception {
    StringWriter resultWriter = new StringWriter();
    byte[] bytes = string.getBytes("UTF-8");
    PublicKey pubKey = (PublicKey) readKeyFromFile(Config.getProperty("escidoc.aa.public.key.file"), true);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    int blockSize = 245;
    for (int i = 0; i < bytes.length; i += blockSize) {
        byte[] result = cipher.doFinal(bytes, i, (i + blockSize < bytes.length ? blockSize : bytes.length - i));
        if (i > 0) {
            resultWriter.write("&");
        }/*from   w ww  .  jav a 2  s.  com*/
        resultWriter.write("auth=");
        resultWriter.write(URLEncoder.encode(new String(Base64.encodeBase64(result)), "ISO-8859-1"));
    }
    return resultWriter.toString();

}

From source file:net.firejack.platform.web.security.x509.KeyUtils.java

public static byte[] decrypt(PrivateKey key, byte[] bytes) throws Exception {
    byte[] result = new byte[0];

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, key);
    int length = cipher.getOutputSize(bytes.length);

    for (int i = 0; i < bytes.length; i += length) {
        byte[] bytes1 = cipher.doFinal(bytes, i, Math.min(bytes.length - i, length));
        result = ArrayUtils.addAll(result, bytes1);
    }//from  w  ww .j a  v  a 2 s  .c  o m

    return result;
}

From source file:net.firejack.platform.web.security.x509.KeyUtils.java

public static byte[] encrypt(PublicKey key, byte[] bytes) throws Exception {
    byte[] result = new byte[0];

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, key);

    int length = cipher.getOutputSize(bytes.length) - 11;
    for (int i = 0; i < bytes.length; i += length) {
        byte[] buffer = cipher.doFinal(bytes, i, Math.min(bytes.length - i, length));
        result = ArrayUtils.addAll(result, buffer);
    }// w w w  . j  a  v a 2s  .  com

    return result;
}