Example usage for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher init

List of usage examples for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher init

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher init.

Prototype

public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException 

Source Link

Document

initialise the cipher.

Usage

From source file:at.archistar.crypto.symmetric.AESEncryptor.java

@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException {

    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
    return cipherData(cipher, data);
}

From source file:at.archistar.crypto.symmetric.AESEncryptor.java

@Override
public byte[] decrypt(byte[] data, byte[] randomKeyBytes) throws InvalidKeyException,
        InvalidAlgorithmParameterException, IOException, IllegalStateException, InvalidCipherTextException {

    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
    return cipherData(cipher, data);
}

From source file:com.github.flbaue.jcrypttool.v1.DecryptionRunnable.java

License:Apache License

@Override
public void run() {
    PaddedBufferedBlockCipher cipher;
    byte[] key;/*  w w  w  .  j  a  v  a 2  s.  co  m*/
    byte[] iv;
    byte[] salt;

    try (InputStream fileInputStream = new BufferedInputStream(
            new FileInputStream(encryptionSettings.inputFile))) {

        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        salt = extractSalt(fileInputStream);
        iv = extractIV(fileInputStream);
        key = generateKey(encryptionSettings.password, salt);
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        /*
        System.out.println(getClass().getName() + " salt:\t" + Base64.toBase64String(salt) + " (" + salt.length + " byte)");
        System.out.println(getClass().getName() + " key:\t" + Base64.toBase64String(key) + " (" + key.length + " byte)");
        System.out.println(getClass().getName() + " iv:\t\t" + Base64.toBase64String(iv) + " (" + iv.length + " byte)");
        */

        InputStream in = null;
        OutputStream out = null;
        try {
            in = new GZIPInputStream(new CipherInputStream(fileInputStream, cipher));
            out = new BufferedOutputStream(new FileOutputStream(encryptionSettings.outputFile));
            processStreams(in, out);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
            closeStream(out);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v1.EncryptionRunnable.java

License:Apache License

@Override
public void run() {
    progress.start();//from  w  w  w  .j  a v  a2 s .  c  o  m

    final byte[] salt = generateSalt();
    final byte[] key = generateKey(encryptionSettings.password, salt);
    final byte[] iv;
    final PaddedBufferedBlockCipher cipher;

    try (OutputStream fileOutputStream = new BufferedOutputStream(
            new FileOutputStream(encryptionSettings.outputFile))) {

        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        iv = generateIV();
        final KeyParameter keyParam = new KeyParameter(key);
        final CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(true, params);

        /*
        System.out.println(getClass().getName() + " salt:\t" + Base64.toBase64String(salt) + " (" + salt.length + " byte)");
        System.out.println(getClass().getName() + " key:\t" + Base64.toBase64String(key) + " (" + key.length + " byte)");
        System.out.println(getClass().getName() + " iv:\t\t" + Base64.toBase64String(iv) + " (" + iv.length + " byte)");
        */

        InputStream in = null;
        OutputStream out = null;
        try {
            writeInitBlock(fileOutputStream, salt, iv);
            in = new BufferedInputStream(new FileInputStream(encryptionSettings.inputFile));
            out = new GZIPOutputStream(new CipherOutputStream(fileOutputStream, cipher));
            processStreams(in, out);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
            closeStream(out);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    progress.setFinished();
}

From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java

License:Apache License

@Override
public OutputStream encryptedOutputStream(final Path path, final String password)
        throws IOException, EncryptionFailedException {
    try {/*ww  w. j  a v a  2  s . c o m*/
        final byte[] salt = generateSalt();
        final byte[] key = generateKey(password, salt);
        final byte[] iv = generateIV();
        final byte[] fileInitBlock = generateOutputInitBlock(salt, iv);

        final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESEngine()), new PKCS7Padding());

        final KeyParameter keyParam = new KeyParameter(key);
        final CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(true, params);

        final BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path));
        out.write(fileInitBlock);

        return new CipherOutputStream(out, cipher);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new EncryptionFailedException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java

License:Apache License

@Override
public String encryptString(final String string, final String password) throws EncryptionFailedException {
    try {//w w  w .j  a  v a  2 s .co m
        final byte[] salt = generateSalt();
        final byte[] key = generateKey(password, salt);
        final byte[] iv = generateIV();
        final byte[] outputInitBlock = generateOutputInitBlock(salt, iv);

        final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESEngine()), new PKCS7Padding());

        final KeyParameter keyParam = new KeyParameter(key);
        final CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(true, params);

        final byte in[] = string.getBytes();
        final byte out[] = new byte[cipher.getOutputSize(in.length)];
        final int len1 = cipher.processBytes(in, 0, in.length, out, 0);

        cipher.doFinal(out, len1);

        final byte[] result = Arrays.concatenate(outputInitBlock, out);

        return Base64.toBase64String(result);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidCipherTextException e) {
        throw new EncryptionFailedException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java

License:Apache License

@Override
public InputStream decryptedInputStream(final Path path, final String password)
        throws IOException, DecryptionFailedException {
    try {// w  w w .ja  v a2 s  .co  m
        InputStream in = new BufferedInputStream(Files.newInputStream(path));
        byte[] initBlock = readInitBlock(in);
        byte[] salt = extractSalt(initBlock);
        byte[] iv = extractIV(initBlock);
        byte[] key = generateKey(password, salt);

        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
                new PKCS7Padding());
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        return new CipherInputStream(in, cipher);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new DecryptionFailedException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java

License:Apache License

@Override
public String decryptString(final String string, final String password) throws DecryptionFailedException {
    try {/*w  w w .jav  a2s. c o m*/
        byte[] inputBytes = Base64.decode(string);
        byte[] salt = extractSalt(inputBytes);
        byte[] iv = extractIV(inputBytes);
        byte[] key = generateKey(password, salt);
        byte[] encryptedData = extractEncryptedData(inputBytes);

        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
                new PKCS7Padding());
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        final byte out[] = new byte[cipher.getOutputSize(encryptedData.length)];
        int length = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0);
        byte[] result;

        length += cipher.doFinal(out, length);
        result = new byte[length];
        System.arraycopy(out, 0, result, 0, length);

        return new String(result);

    } catch (InvalidCipherTextException | InvalidKeySpecException | NoSuchAlgorithmException e) {
        throw new DecryptionFailedException(e);
    }
}

From source file:com.github.horrorho.inflatabledonkey.crypto.AESCBC.java

License:Open Source License

public static byte[] decryptAESCBC(byte[] key, byte[] iv, byte[] data) {
    // AES CBC PKCS7 decrypt
    try {/*from  w ww . j a  v a  2  s . co m*/
        CipherParameters cipherParameters = new ParametersWithIV(new KeyParameter(key), iv);
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding());
        cipher.init(false, cipherParameters);

        byte[] buffer = new byte[cipher.getOutputSize(data.length)];

        int pos = cipher.processBytes(data, 0, data.length, buffer, 0);
        pos += cipher.doFinal(buffer, pos);

        return Arrays.copyOf(buffer, pos);

    } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ex) {
        throw new IllegalArgumentException("decrypt failed", ex);
    }
}

From source file:com.googlecode.jsendnsca.encryption.AESEncryptor.java

License:Apache License

public void encrypt(byte[] passiveCheckBytes, byte[] initVector, String password) {
    RijndaelEngine engine = new RijndaelEngine(_keyByteLength * 8);
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8),
            new ZeroBytePadding());

    try {/*from  w ww. j  av  a 2  s .  c o  m*/
        byte[] sessionKey = new byte[_keyByteLength];
        byte[] passwordBytes = password.getBytes("US-ASCII");
        System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(_keyByteLength, passwordBytes.length));

        byte[] iv = new byte[_keyByteLength];
        System.arraycopy(initVector, 0, iv, 0, Math.min(_keyByteLength, initVector.length));

        cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv));

        byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)];
        int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0);
        cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength);

        int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength);
        System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}