Search in sources :

Example 1 with AESFastEngine

use of org.bouncycastle.crypto.engines.AESFastEngine in project ProxProx by GoMint.

the class EncryptionHandler method createCipher.

private BufferedBlockCipher createCipher(boolean encryptor, byte[] key, byte[] iv) {
    BufferedBlockCipher cipher = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8));
    cipher.init(encryptor, new ParametersWithIV(new KeyParameter(key), iv));
    return cipher;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) CFBBlockCipher(org.bouncycastle.crypto.modes.CFBBlockCipher) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) AESFastEngine(org.bouncycastle.crypto.engines.AESFastEngine)

Example 2 with AESFastEngine

use of org.bouncycastle.crypto.engines.AESFastEngine in project rxlib by RockyLOMO.

the class AesCrypt method getCipher.

@Override
protected StreamBlockCipher getCipher(boolean isEncrypted) throws InvalidAlgorithmParameterException {
    AESFastEngine engine = new AESFastEngine();
    StreamBlockCipher cipher;
    if (_name.equals(CIPHER_AES_128_CFB)) {
        cipher = new CFBBlockCipher(engine, getIVLength() * 8);
    } else if (_name.equals(CIPHER_AES_192_CFB)) {
        cipher = new CFBBlockCipher(engine, getIVLength() * 8);
    } else if (_name.equals(CIPHER_AES_256_CFB)) {
        cipher = new CFBBlockCipher(engine, getIVLength() * 8);
    } else if (_name.equals(CIPHER_AES_128_OFB)) {
        cipher = new OFBBlockCipher(engine, getIVLength() * 8);
    } else if (_name.equals(CIPHER_AES_192_OFB)) {
        cipher = new OFBBlockCipher(engine, getIVLength() * 8);
    } else if (_name.equals(CIPHER_AES_256_OFB)) {
        cipher = new OFBBlockCipher(engine, getIVLength() * 8);
    } else {
        throw new InvalidAlgorithmParameterException(_name);
    }
    return cipher;
}
Also used : StreamBlockCipher(org.bouncycastle.crypto.StreamBlockCipher) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) CFBBlockCipher(org.bouncycastle.crypto.modes.CFBBlockCipher) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AESFastEngine(org.bouncycastle.crypto.engines.AESFastEngine)

Example 3 with AESFastEngine

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

the class Crypto method encryptRaw.

/**
 * Password based encryption using AES - CBC 256 bits.
 *
 * @param plainTextAsBytes
 *            The bytes to encrypt
 * @param password
 *            The password to use for encryption
 * @return SALT_LENGTH bytes of salt followed by the encrypted bytes.
 * @throws IOException
 */
private static byte[] encryptRaw(final byte[] plainTextAsBytes, final char[] password) throws IOException {
    try {
        // Generate salt - each encryption call has a different salt.
        final byte[] salt = new byte[SALT_LENGTH];
        secureRandom.nextBytes(salt);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // The following code uses an AES cipher to encrypt the message.
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, key);
        final byte[] encryptedBytes = new byte[cipher.getOutputSize(plainTextAsBytes.length)];
        final int processLen = cipher.processBytes(plainTextAsBytes, 0, plainTextAsBytes.length, encryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(encryptedBytes, processLen);
        // The result bytes are the SALT_LENGTH bytes followed by the encrypted bytes.
        return concat(salt, Arrays.copyOf(encryptedBytes, processLen + doFinalLen));
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not encrypt 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)

Example 4 with AESFastEngine

use of org.bouncycastle.crypto.engines.AESFastEngine in project faf-java-server by FAForever.

the class UniqueIdServiceTest method aesEncrypt.

private static byte[] aesEncrypt(byte[] initVector, byte[] payload, byte[] aesKey) throws InvalidCipherTextException {
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(true, new ParametersWithIV(new KeyParameter(aesKey), initVector));
    byte[] outBuf = new byte[cipher.getOutputSize(payload.length)];
    int length = cipher.processBytes(payload, 0, payload.length, outBuf, 0);
    length += cipher.doFinal(outBuf, length);
    byte[] result = new byte[length];
    System.arraycopy(outBuf, 0, result, 0, result.length);
    return result;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.bouncycastle.crypto.engines.AESFastEngine)

Example 5 with AESFastEngine

use of org.bouncycastle.crypto.engines.AESFastEngine in project faf-java-server by FAForever.

the class UniqueIdService method aesDecrypt.

private byte[] aesDecrypt(byte[] initVector, byte[] aesEncryptedJson, byte[] aesKey) throws InvalidCipherTextException {
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(aesKey), initVector));
    int plaintextSize = cipher.processBytes(aesEncryptedJson, 0, aesEncryptedJson.length, aesEncryptedJson, 0);
    plaintextSize += cipher.doFinal(aesEncryptedJson, plaintextSize);
    return Arrays.copyOf(aesEncryptedJson, plaintextSize);
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.bouncycastle.crypto.engines.AESFastEngine)

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