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

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

Introduction

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

Prototype

public PaddedBufferedBlockCipher(BlockCipher cipher, BlockCipherPadding padding) 

Source Link

Document

Create a buffered block cipher with the desired padding.

Usage

From source file:ch.dissem.bitmessage.cryptography.bc.BouncyCryptography.java

License:Apache License

@Override
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());
    CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);

    cipher.init(encrypt, params);//w  w w.jav  a  2s. co  m

    byte[] buffer = new byte[cipher.getOutputSize(data.length)];
    int length = cipher.processBytes(data, 0, data.length, buffer, 0);
    try {
        length += cipher.doFinal(buffer, length);
    } catch (InvalidCipherTextException e) {
        throw new IllegalArgumentException(e);
    }
    if (length < buffer.length) {
        return Arrays.copyOfRange(buffer, 0, length);
    }
    return buffer;
}

From source file:co.lqnt.lockbox.Cipher.java

License:Open Source License

/**
 * Construct a new bi-directional cipher.
 *///  ww  w  .  ja v a  2s . c o  m
public Cipher() {
    CodecInterface base64UriCodec = new Base64UriCodec();
    AsymmetricBlockCipher rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    BufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());
    Digest sha1Digest = new SHA1Digest();
    SecureRandom random = new SecureRandom();

    this.encryptionCipher = new EncryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest, random);
    this.decryptionCipher = new DecryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest);
}

From source file:co.lqnt.lockbox.DecryptionCipher.java

License:Open Source License

/**
 * Construct a new decryption cipher.//ww  w  . ja va2  s.c  o  m
 */
public DecryptionCipher() {
    this.base64UriCodec = new Base64UriCodec();
    this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    this.sha1Digest = new SHA1Digest();
    this.asciiCharset = Charset.forName("US-ASCII");
}

From source file:co.lqnt.lockbox.EncryptionCipher.java

License:Open Source License

/**
 * Construct a new encryption cipher.//www  . java 2  s .  c  om
 */
public EncryptionCipher() {
    this.base64UriCodec = new Base64UriCodec();
    this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    this.sha1Digest = new SHA1Digest();
    this.random = new SecureRandom();
    this.asciiCharset = Charset.forName("US-ASCII");
}

From source file:com.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] encrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }/*  ww  w .  jav  a 2 s  . c  o m*/
    BufferedBlockCipher encryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);
    encryptcipher.init(true, ivAndKey);

    return callCipher(encryptcipher, data);
}

From source file:com.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] decrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }/*from   w w w .  j  ava 2 s. co m*/

    BufferedBlockCipher decryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);

    decryptcipher.init(false, ivAndKey);

    return callCipher(decryptcipher, data);
}

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

License:Apache License

@Override
public void run() {
    PaddedBufferedBlockCipher cipher;//from  w  w  w.  ja v  a 2  s .c om
    byte[] key;
    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 ww w. ja  va 2s .  com

    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 2s. 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 {//from  w  w  w. java2s.  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);
    }
}