Java Code Examples for org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher#doFinal()

The following examples show how to use org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher#doFinal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: aes.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) {
    assert (key.length == 64 && iv.length == 32);
    try {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));

        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] outBuf = new byte[cipher.getOutputSize(plaintext.length)];
        int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0);
        processed += cipher.doFinal(outBuf, processed);

        ByteBuffer byteBuffer = ByteBuffer.allocate(processed);
        byteBuffer.put(outBuf, 0, processed);
        return byteBuffer;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: aes.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) {
    try {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] clear = new byte[cipher.getOutputSize(cipertext.length)];
        int len = cipher.processBytes(cipertext, 0, cipertext.length, clear, 0);
        len += cipher.doFinal(clear, len);
        ByteBuffer byteBuffer = ByteBuffer.allocate(len);
        byteBuffer.put(clear, 0, len);
        return byteBuffer;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}
 
Example 3
Source File: aes.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) {
    assert (key.length == 32 && iv.length == 16);
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));

        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] outBuf   = new byte[cipher.getOutputSize(plaintext.length)];
        int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0);
        processed += cipher.doFinal(outBuf, processed);

        ByteBuffer byteBuffer = ByteBuffer.allocate(processed);
        byteBuffer.put(outBuf, 0, processed);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;
}
 
Example 4
Source File: aes.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) {
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] clear = new byte[cipher.getOutputSize(cipertext.length)];
        int len = cipher.processBytes(cipertext, 0, cipertext.length, clear,0);
        len += cipher.doFinal(clear, len);
        ByteBuffer byteBuffer = ByteBuffer.allocate(len);
        byteBuffer.put(clear, 0, len);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;

}
 
Example 5
Source File: aes.java    From bitshares_wallet with MIT License 6 votes vote down vote up
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) {
    assert (key.length == 32 && iv.length == 16);
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));

        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] outBuf   = new byte[cipher.getOutputSize(plaintext.length)];
        int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0);
        processed += cipher.doFinal(outBuf, processed);

        ByteBuffer byteBuffer = ByteBuffer.allocate(processed);
        byteBuffer.put(outBuf, 0, processed);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
Source File: aes.java    From bitshares_wallet with MIT License 6 votes vote down vote up
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) {
    try
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
        byte[] clear = new byte[cipher.getOutputSize(cipertext.length)];
        int len = cipher.processBytes(cipertext, 0, cipertext.length, clear,0);
        len += cipher.doFinal(clear, len);
        ByteBuffer byteBuffer = ByteBuffer.allocate(len);
        byteBuffer.put(clear, 0, len);
        return byteBuffer;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;

}
 
Example 7
Source File: Crypto.java    From KeePassJava2 with Apache License 2.0 6 votes vote down vote up
/**
 * Encryption and Decryption Helper
 *
 * @param input     the candidate for transformation
 * @param base64in  true if base 64 encoded
 * @param base64out true if we require base 64 out
 * @param cipher    a Cipher initialised for Encrypt or Decrypt
 * @return the transformed result
 */
static String CryptoTransform(String input, boolean base64in, boolean base64out, PaddedBufferedBlockCipher cipher) {
    byte[] bytes;
    if (base64in) {
        bytes = Helpers.decodeBase64Content(input.getBytes(), false);
    } else {
        bytes = input.getBytes();
    }

    byte[] output = new byte[cipher.getOutputSize(bytes.length)];
    int outputlen = cipher.processBytes(bytes, 0, bytes.length, output, 0);
    try {
        int len = cipher.doFinal(output, outputlen);
        // padded buffer is required on bas64 i.e. encrypted direction
        if (base64out) {
            return Helpers.encodeBase64Content(output, false);
        }
        // trim to buffer length
        return new String(output, 0, outputlen + len);
    } catch (InvalidCipherTextException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 8
Source File: SensitiveDataPreApi23.java    From android-java-connect-rest-sample with MIT License 5 votes vote down vote up
private byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws InvalidCipherTextException {
    int minSize = cipher.getOutputSize(data.length);
    byte[] outBuf = new byte[minSize];
    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
    int length2 = cipher.doFinal(outBuf, length1);
    int actualLength = length1 + length2;
    byte[] result = new byte[actualLength];
    System.arraycopy(outBuf, 0, result, 0, result.length);
    return result;
}