Search in sources :

Example 6 with AESFastEngine

use of org.bouncycastle.crypto.engines.AESFastEngine in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method decryptRaw.

/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param bytesToDecode
 *            The bytes to decrypt
 * @param password
 *            password to use for decryption
 * @return The decrypted bytes
 * @throws IOException
 */
private static byte[] decryptRaw(final byte[] bytesToDecode, final char[] password) throws IOException {
    try {
        // separate the salt and bytes to decrypt
        final byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(bytesToDecode, 0, salt, 0, SALT_LENGTH);
        final byte[] cipherBytes = new byte[bytesToDecode.length - SALT_LENGTH];
        System.arraycopy(bytesToDecode, SALT_LENGTH, cipherBytes, 0, bytesToDecode.length - SALT_LENGTH);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // decrypt the message
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, key);
        final byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int processLen = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(decryptedBytes, processLen);
        return Arrays.copyOf(decryptedBytes, processLen + doFinalLen);
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not decrypt bytes", x);
    }
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) DataLengthException(org.bouncycastle.crypto.DataLengthException) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.bouncycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException)

Aggregations

AESFastEngine (org.bouncycastle.crypto.engines.AESFastEngine)6 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)5 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)4 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)4 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)3 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)3 IOException (java.io.IOException)2 DataLengthException (org.bouncycastle.crypto.DataLengthException)2 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)2 CFBBlockCipher (org.bouncycastle.crypto.modes.CFBBlockCipher)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 StreamBlockCipher (org.bouncycastle.crypto.StreamBlockCipher)1 OFBBlockCipher (org.bouncycastle.crypto.modes.OFBBlockCipher)1 AsymmetricKeyParameter (org.bouncycastle.crypto.params.AsymmetricKeyParameter)1