Search in sources :

Example 16 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher in project samourai-wallet-android by Samourai-Wallet.

the class AESUtil method encryptWithSetMode.

public static String encryptWithSetMode(String cleartext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws DecryptionException, UnsupportedEncodingException {
    final int AESBlockSize = 4;
    if (password == null) {
        throw new DecryptionException("Password null");
    }
    // Use secure random to generate a 16 byte iv
    SecureRandom random = new SecureRandom();
    byte[] iv = new byte[AESBlockSize * 4];
    random.nextBytes(iv);
    byte[] clearbytes = cleartext.getBytes("UTF-8");
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toString().toCharArray()), iv, iterations);
    KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(256);
    CipherParameters params = new ParametersWithIV(keyParam, iv);
    BlockCipher cipherMode;
    if (mode == MODE_CBC) {
        cipherMode = new CBCBlockCipher(new AESEngine());
    } else {
        // mode == MODE_OFB
        cipherMode = new OFBBlockCipher(new AESEngine(), 128);
    }
    BufferedBlockCipher cipher;
    if (padding != null) {
        cipher = new PaddedBufferedBlockCipher(cipherMode, padding);
    } else {
        cipher = new BufferedBlockCipher(cipherMode);
    }
    cipher.reset();
    cipher.init(true, params);
    byte[] outBuf = cipherData(cipher, clearbytes);
    // Append to IV to the output
    int len1 = iv.length;
    int len2 = outBuf.length;
    byte[] ivAppended = new byte[len1 + len2];
    System.arraycopy(iv, 0, ivAppended, 0, len1);
    System.arraycopy(outBuf, 0, ivAppended, len1, len2);
    // String ret = Base64.encodeBase64String(ivAppended);
    byte[] raw = Base64.encodeBase64(ivAppended);
    return new String(raw);
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) PKCS5S2ParametersGenerator(org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) BlockCipher(org.bouncycastle.crypto.BlockCipher) OFBBlockCipher(org.bouncycastle.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher) PBEParametersGenerator(org.bouncycastle.crypto.PBEParametersGenerator)

Example 17 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher in project elastic-core-maven by OrdinaryDude.

the class Crypto method aesEncrypt.

public static byte[] aesEncrypt(byte[] plaintext, byte[] key) {
    try {
        byte[] iv = new byte[16];
        secureRandom.get().nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 18 with PaddedBufferedBlockCipher

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

Example 19 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher in project rskj by rsksmart.

the class KeyCrypterAes method decrypt.

/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param dataToDecrypt    The data to decrypt
 * @param key              The AES key to use for decryption
 * @return                 The decrypted bytes
 * @throws                 KeyCrypterException if bytes could not be decrypted
 */
@Override
public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter key) {
    checkNotNull(dataToDecrypt);
    checkNotNull(key);
    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(key.getKey()), dataToDecrypt.initialisationVector);
        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
        cipher.init(false, keyWithIv);
        byte[] cipherBytes = dataToDecrypt.encryptedBytes;
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int length2 = cipher.doFinal(decryptedBytes, length1);
        return Arrays.copyOf(decryptedBytes, length1 + length2);
    } catch (Exception e) {
        throw new KeyCrypterException("Could not decrypt bytes", e);
    }
}
Also used : ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 20 with PaddedBufferedBlockCipher

use of org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher in project gocd by gocd.

the class DESEncrypter method encrypt.

private static String encrypt(byte[] key, String plainText) throws CryptoException {
    try {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
        KeyParameter keyParameter = new KeyParameter(key);
        cipher.init(true, keyParameter);
        byte[] plainTextBytes = plainText.getBytes();
        byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)];
        int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0);
        cipher.doFinal(cipherTextBytes, outputLength);
        return ENCODER.encodeToString(cipherTextBytes).trim();
    } catch (Exception e) {
        throw new CryptoException(e);
    }
}
Also used : PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) DESEngine(org.bouncycastle.crypto.engines.DESEngine) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

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