Search in sources :

Example 21 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher in project spring-security by spring-projects.

the class BouncyCastleAesCbcBytesEncryptor method encrypt.

@Override
@SuppressWarnings("deprecation")
public byte[] encrypt(byte[] bytes) {
    byte[] iv = this.ivGenerator.generateKey();
    PaddedBufferedBlockCipher blockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new org.bouncycastle.crypto.engines.AESFastEngine()), new PKCS7Padding());
    blockCipher.init(true, new ParametersWithIV(this.secretKey, iv));
    byte[] encrypted = process(blockCipher, bytes);
    return (iv != null) ? EncodingUtils.concatenate(iv, encrypted) : encrypted;
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) PKCS7Padding(org.bouncycastle.crypto.paddings.PKCS7Padding) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 22 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher 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)

Example 23 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher 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 24 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher in project OsmAnd-tools by osmandapp.

the class SigningUtils method decryptPassphrase.

/**
 * Steps (4) to (7): Decrypt passphrase with secret key
 * @param pass passphrase from withdrawal request
 * @param key secret key
 * @return decrypted passphrase for next steps
 * @throws BlockIOException
 */
static byte[] decryptPassphrase(byte[] pass, byte[] key) throws BlockIOException {
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new AESEngine());
    CipherParameters aesKey = new KeyParameter(key);
    aes.init(false, aesKey);
    try {
        return cipherData(aes, pass);
    } catch (InvalidCipherTextException e) {
        throw new BlockIOException("Unexpected error while signing transaction. Please file an issue report.");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter)

Example 25 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher in project OsmAnd-tools by osmandapp.

the class SigningUtils method encryptPassphrase.

/**
 * Used only for testing: encrypt secret passphrase
 * @param plain plain passphrase
 * @param key secret key
 * @return encrypted passphrase
 * @throws BlockIOException
 */
static byte[] encryptPassphrase(String plain, byte[] key) throws BlockIOException {
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new AESEngine());
    CipherParameters aesKey = new KeyParameter(key);
    aes.init(true, aesKey);
    try {
        return cipherData(aes, plain.getBytes("UTF-8"));
    } catch (InvalidCipherTextException e) {
        throw new BlockIOException("Unexpected error while signing transaction. Please file an issue report.");
    } catch (UnsupportedEncodingException e) {
        throw new BlockIOException("Your system does not seem to support UTF-8 encoding! Aborting signing process.");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)25 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)23 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)19 ParametersWithIV (org.bouncycastle.crypto.params.ParametersWithIV)19 AESEngine (org.bouncycastle.crypto.engines.AESEngine)14 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)13 CipherParameters (org.bouncycastle.crypto.CipherParameters)11 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)9 PKCS7Padding (org.bouncycastle.crypto.paddings.PKCS7Padding)6 DataLengthException (org.bouncycastle.crypto.DataLengthException)4 PBEParametersGenerator (org.bouncycastle.crypto.PBEParametersGenerator)4 AESFastEngine (org.bouncycastle.crypto.engines.AESFastEngine)4 DESEngine (org.bouncycastle.crypto.engines.DESEngine)4 PKCS5S2ParametersGenerator (org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 BlockCipherPadding (org.bouncycastle.crypto.paddings.BlockCipherPadding)3 IOException (java.io.IOException)2 SecureRandom (java.security.SecureRandom)2 BlockCipher (org.bouncycastle.crypto.BlockCipher)2 OFBBlockCipher (org.bouncycastle.crypto.modes.OFBBlockCipher)2